diff --git a/src/main/java/fr/igred/omero/Client.java b/src/main/java/fr/igred/omero/Client.java index 8b098402..072c66ba 100644 --- a/src/main/java/fr/igred/omero/Client.java +++ b/src/main/java/fr/igred/omero/Client.java @@ -29,10 +29,13 @@ import fr.igred.omero.repository.FolderWrapper; import fr.igred.omero.repository.ImageWrapper; import fr.igred.omero.repository.ProjectWrapper; +import omero.ApiUsageException; import omero.gateway.Gateway; import omero.gateway.SecurityContext; import omero.gateway.model.ExperimenterData; import omero.gateway.model.GroupData; +import omero.model.Experimenter; +import omero.model.ExperimenterGroup; import java.util.ArrayList; import java.util.Collection; @@ -282,7 +285,7 @@ public void deleteTables(Collection tables) * @throws ServiceException Cannot connect to OMERO. * @throws AccessException Cannot access data. * @throws ExecutionException A Facility can't be retrieved or instantiated. - * @throws NoSuchElementException The requested user does not exist. + * @throws NoSuchElementException The requested user cannot be found. */ public ExperimenterWrapper getUser(String username) throws ExecutionException, ServiceException, AccessException { @@ -298,6 +301,29 @@ public ExperimenterWrapper getUser(String username) } + /** + * Returns the user which matches the user ID. + * + * @param userId The ID of the user. + * + * @return The user matching the user ID, or null if it does not exist. + * + * @throws ServiceException Cannot connect to OMERO. + * @throws OMEROServerError Server error. + * @throws NoSuchElementException The requested user cannot be found. + */ + public ExperimenterWrapper getUser(long userId) + throws ServiceException, OMEROServerError { + Experimenter user = ExceptionHandler.of(getGateway(), g -> g.getAdminService(getCtx()).getExperimenter(userId)) + .rethrow(ApiUsageException.class, + (m, e) -> new NoSuchElementException(m), + "User not found: " + userId) + .handleServiceOrServer("Cannot retrieve user: " + userId) + .get(); + return new ExperimenterWrapper(new ExperimenterData(user)); + } + + /** * Returns the group which matches the name. * @@ -308,7 +334,7 @@ public ExperimenterWrapper getUser(String username) * @throws ServiceException Cannot connect to OMERO. * @throws AccessException Cannot access data. * @throws ExecutionException A Facility can't be retrieved or instantiated. - * @throws NoSuchElementException The requested group does not exist. + * @throws NoSuchElementException The requested group cannot be found. */ public GroupWrapper getGroup(String groupName) throws ExecutionException, ServiceException, AccessException { @@ -323,6 +349,29 @@ public GroupWrapper getGroup(String groupName) } + /** + * Returns the group which matches the group ID. + * + * @param groupId The ID of the group. + * + * @return The group with the appropriate group ID, if it exists. + * + * @throws ServiceException Cannot connect to OMERO. + * @throws OMEROServerError Server error. + * @throws NoSuchElementException The requested group cannot be found. + */ + public GroupWrapper getGroup(long groupId) + throws ServiceException, OMEROServerError { + ExperimenterGroup group = ExceptionHandler.of(getGateway(), g -> g.getAdminService(getCtx()).getGroup(groupId)) + .rethrow(ApiUsageException.class, + (m, e) -> new NoSuchElementException(m), + "User not found: " + groupId) + .handleServiceOrServer("Cannot retrieve group: " + groupId) + .get(); + return new GroupWrapper(new GroupData(group)); + } + + /** * Gets the client associated with the username in the parameters. The user calling this function needs to have * administrator rights. All action realized with the client returned will be considered as his. diff --git a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java index a8e58139..6a0dbadd 100644 --- a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java +++ b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java @@ -18,10 +18,15 @@ package fr.igred.omero.meta; +import fr.igred.omero.Client; import fr.igred.omero.GenericObjectWrapper; +import fr.igred.omero.exception.AccessException; +import fr.igred.omero.exception.ExceptionHandler; +import fr.igred.omero.exception.ServiceException; import omero.gateway.model.ExperimenterData; import java.util.List; +import java.util.concurrent.ExecutionException; /** @@ -212,4 +217,36 @@ public boolean isLDAP() { return data.isLDAP(); } + + /** + * Returns {@code true} if the user owns the specified group. + * + * @param group The group. + * + * @return See above. + */ + public boolean isLeader(GroupWrapper group) { + return group.getLeaders().stream().anyMatch(l -> l.getId() == data.getId()); + } + + + /** + * Returns {@code true} if the user is a full admin. + * + * @param client {@link Client} that handles the connection + * + * @return See above. + * + * @throws ServiceException Cannot connect to OMERO. + * @throws AccessException Cannot access data. + * @throws ExecutionException A Facility can't be retrieved or instantiated. + */ + public boolean isAdmin(Client client) + throws ServiceException, AccessException, ExecutionException { + return !ExceptionHandler.of(client.getAdminFacility(), a -> a.getAdminPrivileges(client.getCtx(), data)) + .handleServiceOrAccess("Cannot retrieve admin privileges.") + .get() + .isEmpty(); + } + } diff --git a/src/test/java/fr/igred/omero/meta/ExperimenterTest.java b/src/test/java/fr/igred/omero/meta/ExperimenterTest.java index 8616703f..fce13742 100644 --- a/src/test/java/fr/igred/omero/meta/ExperimenterTest.java +++ b/src/test/java/fr/igred/omero/meta/ExperimenterTest.java @@ -39,6 +39,18 @@ void testGetWrongUser() { } + @Test + void testGetWrongUserId() { + assertThrows(NoSuchElementException.class, () -> client.getUser(859L)); + } + + + @Test + void testGetUserById() throws Exception { + assertEquals(USER1.name, client.getUser(USER1.id).getUserName()); + } + + @Test void testSudoWrongUser() { assertThrows(NoSuchElementException.class, () -> client.sudoGetUser("nonexistent")); @@ -121,4 +133,34 @@ void testGetGroups() throws Exception { assertEquals(GROUP1.name, experimenter.getDefaultGroup().getName()); } + + @Test + void testIsNotGroupLeader() throws Exception { + ExperimenterWrapper experimenter = client.getUser(USER1.name); + GroupWrapper group = client.getGroup("testGroup3"); + assertFalse(experimenter.isLeader(group)); + } + + + @Test + void testIsGroupLeader() throws Exception { + ExperimenterWrapper experimenter = client.getUser("testUser4"); + GroupWrapper group = client.getGroup("testGroup3"); + assertTrue(experimenter.isLeader(group)); + } + + + @Test + void testIsNotAdmin() throws Exception { + ExperimenterWrapper experimenter = client.getUser(USER1.name); + assertFalse(experimenter.isAdmin(client)); + } + + + @Test + void testIsAdmin() throws Exception { + ExperimenterWrapper experimenter = client.getUser(); + assertTrue(experimenter.isAdmin(client)); + } + } diff --git a/src/test/java/fr/igred/omero/meta/GroupTest.java b/src/test/java/fr/igred/omero/meta/GroupTest.java index aa8b5b43..a2e35a3e 100644 --- a/src/test/java/fr/igred/omero/meta/GroupTest.java +++ b/src/test/java/fr/igred/omero/meta/GroupTest.java @@ -37,6 +37,18 @@ void testGetWrongGroup() { } + @Test + void testGetWrongGroupId() { + assertThrows(NoSuchElementException.class, () -> client.getGroup(859L)); + } + + + @Test + void testGetGroupById() throws Exception { + assertEquals(GROUP1.name, client.getGroup(GROUP1.id).getName()); + } + + @Test void testSetGroupName() throws Exception { GroupWrapper group = client.getGroup("testGroup2");