Search users in membership

Hi.
I try to search the list of users with a certain membership.
I looked in the IdentityAPI but there is no method “getUsersInMembership”.

Currently, I do this :

List usersInGroup = apiAccessor.getIdentiyAPI().getUserInGroup(mygroup.getId());
List usersInRole = apiAccessor.getIdentityAPI().getUserInRole(myRole.getId());

List usersInMembership = new ArrayList();
for(User user : usersInGroup) {
if(userInRole.contains(user)) usersInMembership.add(user);
}

This works fine but I want to know if there is a single method to do this. Did a “getUsersInMembership” method exists ?

I also found a method “searchUsers()” in the IdentityAPI ( here ), but I don’t understand what the parameter is. Maybe it can help me.
Did you have an exemple on how to use this method ?

Hi,
You should indeed use the method searchUsers() for that with a searchOption filtering on both the group ID and the Role ID.

Something like:

SearchOptionsBuilder optionsBuilder = new SearchOptionsBuilder(startIndex, maxResults);
optionsBuilder.filter(UserSearchDescriptor.GROUP_ID, groupId);
optionsBuilder.filter(UserSearchDescriptor.ROLE_ID, roleId);
searchUsers(optionsBuilder.done());

HTH

Hi,
The problem is that I don’t know how to use the SearchOptions object.
It doesn’t have any constructor.

EDIT : Thanks for adding code. I will try this.

It works fine.
I use this code :

import org.bonitasoft.engine.identity.User;
import org.bonitasoft.engine.identity.Role;
import org.bonitasoft.engine.identity.Group;
import org.bonitasoft.engine.search.SearchOptionsBuilder;
import org.bonitasoft.engine.search.SearchResult;
import org.bonitasoft.engine.identity.UserSearchDescriptor;

SearchResult searchList = null;

try {
Role RM = apiAccessor.getIdentityAPI().getRoleByName(“Release Manager”);
int nbRM = apiAccessor.getIdentityAPI().getNumberOfUsersInRole(RM.getId());

Group releaseGroup = apiAccessor.getIdentityAPI().getGroupByPath(idRelease);

SearchOptionsBuilder optionsBuilder = new SearchOptionsBuilder(0, nbRM);
optionsBuilder.filter(UserSearchDescriptor.GROUP_ID, releaseGroup.getId());
optionsBuilder.filter(UserSearchDescriptor.ROLE_ID, RM.getId());
searchList = apiAccessor.getIdentityAPI().searchUsers(optionsBuilder.done());

} catch (Exception e) {
return new ArrayList();;
}

return new ArrayList(searchList.getResult());