Skip to content

Commit

Permalink
Add methods for user / group management (#74)
Browse files Browse the repository at this point in the history
* Add isAdmin / isLeader methods to ExperimenterWrapper
* Add getUser and getGroup with userID and groupID arguments
* Add tests

---------

Co-authored-by: Pierre Pouchin <[email protected]>
  • Loading branch information
Rdornier and ppouchin authored Sep 22, 2023
1 parent 7b34d85 commit 33bac4f
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/main/java/fr/igred/omero/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -282,7 +285,7 @@ public void deleteTables(Collection<? extends TableWrapper> 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 {
Expand All @@ -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.
*
Expand All @@ -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 {
Expand All @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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();
}

}
42 changes: 42 additions & 0 deletions src/test/java/fr/igred/omero/meta/ExperimenterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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));
}

}
12 changes: 12 additions & 0 deletions src/test/java/fr/igred/omero/meta/GroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 33bac4f

Please sign in to comment.