storagehub/src/main/java/org/gcube/data/access/storagehub/services/UserManager.java

213 lines
6.6 KiB
Java
Raw Normal View History

package org.gcube.data.access.storagehub.services;
import java.util.ArrayList;
import java.util.List;
2020-01-22 16:41:12 +01:00
import javax.jcr.RepositoryException;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.gcube.common.authorization.control.annotations.AuthorizationControl;
import org.gcube.common.gxrest.response.outbound.GXOutboundErrorResponse;
import org.gcube.common.storagehub.model.exceptions.BackendGenericError;
2021-02-08 12:30:58 +01:00
import org.gcube.common.storagehub.model.exceptions.IdNotFoundException;
2020-01-22 16:41:12 +01:00
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
2022-10-03 17:24:53 +02:00
import org.gcube.common.storagehub.model.types.SHUBUser;
import org.gcube.data.access.storagehub.Constants;
2020-03-16 16:55:26 +01:00
import org.gcube.data.access.storagehub.StorageHubAppllicationManager;
import org.gcube.data.access.storagehub.handlers.UserHandler;
2020-03-16 16:55:26 +01:00
import org.gcube.smartgears.annotations.ManagedBy;
2020-01-22 16:41:12 +01:00
import org.gcube.smartgears.utils.InnerMethodName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2023-05-30 10:18:47 +02:00
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
2024-03-15 14:26:05 +01:00
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
@Path("users")
2020-03-16 16:55:26 +01:00
@ManagedBy(StorageHubAppllicationManager.class)
2023-05-30 10:18:47 +02:00
@RequestHeaders({
@RequestHeader(name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"), })
public class UserManager {
2020-01-22 16:41:12 +01:00
private static final String INFRASTRUCTURE_MANAGER_ROLE = "Infrastructure-Manager";
private static final Logger log = LoggerFactory.getLogger(UserManager.class);
2022-06-27 15:34:24 +02:00
RepositoryInitializer repository = StorageHubAppllicationManager.getRepository();
@Inject
UserHandler userHandler;
2022-10-03 17:24:53 +02:00
2023-05-22 11:02:21 +02:00
@GET
@Path("")
@Produces(MediaType.APPLICATION_JSON)
public List<SHUBUser> getUsers() {
2024-03-15 14:26:05 +01:00
InnerMethodName.set("getUsers");
JackrabbitSession session = null;
2023-05-22 11:02:21 +02:00
try {
session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS);
2024-03-28 08:35:15 +01:00
return userHandler.getAllUsers(session);
} catch (Throwable e) {
2023-05-22 11:02:21 +02:00
log.error("jcr error getting users", e);
GXOutboundErrorResponse.throwException(new BackendGenericError(e));
} finally {
if (session != null)
session.logout();
2023-05-22 11:02:21 +02:00
}
return null;
}
2021-02-08 12:30:58 +01:00
@GET
@Path("{user}")
public SHUBUser getUser(@PathParam("user") String user) {
2024-03-15 14:26:05 +01:00
InnerMethodName.set("getUser");
2021-02-08 12:30:58 +01:00
JackrabbitSession session = null;
try {
2024-03-15 14:26:05 +01:00
session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS);
userHandler.getUser(session, user);
} catch (StorageHubException se) {
log.error("error getting user", se);
GXOutboundErrorResponse.throwException(se);
} catch (Exception e) {
2021-02-08 12:30:58 +01:00
log.error("jcr error getting user", e);
GXOutboundErrorResponse.throwException(new BackendGenericError(e));
} finally {
if (session != null)
2021-02-08 12:30:58 +01:00
session.logout();
}
2021-02-08 12:30:58 +01:00
GXOutboundErrorResponse.throwException(new IdNotFoundException(user));
2021-02-08 12:30:58 +01:00
return null;
}
@POST
@Path("")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@AuthorizationControl(allowedRoles = { INFRASTRUCTURE_MANAGER_ROLE })
public String createUser(@FormParam("user") String user, @FormParam("password") String password) {
2024-03-15 14:26:05 +01:00
InnerMethodName.set("createUser");
JackrabbitSession session = null;
String userId = null;
try {
2024-03-15 14:26:05 +01:00
session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS);
userId = userHandler.createUser(session, user, password);
2022-10-03 17:24:53 +02:00
session.save();
} catch (StorageHubException she) {
2022-10-03 17:24:53 +02:00
log.error(she.getErrorMessage(), she);
GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus()));
} catch (RepositoryException re) {
2022-10-03 17:24:53 +02:00
log.error("jcr error creating item", re);
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating item", re));
} finally {
if (session != null)
2022-10-03 17:24:53 +02:00
session.logout();
}
return userId;
}
@PUT
@Path("{user}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@AuthorizationControl(allowedRoles = { INFRASTRUCTURE_MANAGER_ROLE })
public String updateHomeUserToLatestVersion(@PathParam("user") String user) {
2022-10-03 17:24:53 +02:00
2024-03-15 14:26:05 +01:00
InnerMethodName.set("updateHomeUserToLatestVersion");
2022-10-03 17:24:53 +02:00
JackrabbitSession session = null;
String userId = null;
try {
2024-03-15 14:26:05 +01:00
session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS);
2022-10-03 17:24:53 +02:00
userId = userHandler.updateHomeUserToLatestVersion(session, userId);
session.save();
} catch (StorageHubException she) {
2020-01-22 16:41:12 +01:00
log.error(she.getErrorMessage(), she);
GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus()));
} catch (RepositoryException re) {
2020-01-22 16:41:12 +01:00
log.error("jcr error creating item", re);
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating item", re));
} finally {
if (session != null)
session.logout();
}
return userId;
}
@DELETE
2020-01-22 16:41:12 +01:00
@Path("{user}")
@AuthorizationControl(allowedRoles = { INFRASTRUCTURE_MANAGER_ROLE })
public String deleteUser(@PathParam("user") final String user) {
2024-03-15 14:26:05 +01:00
InnerMethodName.set("deleteUser");
JackrabbitSession session = null;
try {
2024-03-15 14:26:05 +01:00
session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS);
userHandler.deleteUser(session, user);
session.save();
} catch (StorageHubException she) {
2020-01-22 16:41:12 +01:00
log.error(she.getErrorMessage(), she);
GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus()));
} catch (RepositoryException re) {
2020-01-22 16:41:12 +01:00
log.error("jcr error creating item", re);
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating item", re));
} finally {
if (session != null)
session.logout();
}
2021-03-16 00:04:54 +01:00
return user;
}
2023-05-14 12:57:32 +02:00
@GET
@Path("{user}/groups")
@Produces(MediaType.APPLICATION_JSON)
public List<String> getGroupsPerUser(@PathParam("user") final String user) {
2023-05-14 12:57:32 +02:00
2024-03-15 14:26:05 +01:00
InnerMethodName.set("getGroupsPerUser");
2023-05-14 12:57:32 +02:00
JackrabbitSession session = null;
List<String> groups = new ArrayList<>();
2023-05-14 12:57:32 +02:00
try {
2024-03-15 14:26:05 +01:00
session = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS);
userHandler.getGroupsPerUser(session, user);
} catch (RepositoryException re) {
2023-05-14 12:57:32 +02:00
log.error("jcr error creating item", re);
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating item", re));
} finally {
if (session != null)
2023-05-14 12:57:32 +02:00
session.logout();
}
return groups;
}
2022-10-03 17:24:53 +02:00
}