How to get All user by group id ?

cancel
Showing results for 
Search instead for 
Did you mean: 
Active Member

How to get All user by group id ?

TYPE_CU  this group i have created and added one user .

but i am not getting user.

there is any api  to get direct user node using serviceRegistry.getAuthorityService()

Set<String> person = serviceRegistry.getAuthorityService().getContainedAuthorities(null, "TYPE_CU", true);
for(String username : person) {
     logger.info(username);
      NodeRef node = serviceRegistry.getPersonService().getPerson(username);
      logger.info(nodeService.getProperties(node));
}

i want to get all user with properties and value based on group id . 

3 Replies
Senior Member

Re: How to get All user by group id ?

There is a JavaScript API to get the users of a group.

var users = mygroup.getAllUsers();

Regards,

Kintu

ContCentric

Regards,
Kintu | ContCentric
Established Member

Re: How to get All user by group id ?

Hi,

You can use javascript api as well as Java api for getting users of group.

In your code you need add the type of authority insted of null and prefix "GROUP_" as below : 

Set<String> groupMembers = authorityService.getContainedAuthorities(AuthorityType.USER,
"GROUP_TYPE_CU", true);

Regards,

Vidhi

Regards,
Vidhi | ContCentric
Moderator
Moderator

Re: How to get All user by group id ?

Hi Dharmraj Gurjar

You can use this method method for your use case, it is java backed: 

//Get all immediate and sub group details. Find authorities at any depth. This will fetch person information from sub groups of ADMINISTRATOR group as well.

authorityService.getContainedAuthorities(AuthorityType.USER, "GROUP_ADMINISTRATOR", false);

//Get only immediate authorities 

authorityService.getContainedAuthorities(AuthorityType.USER, "GROUP_ADMINISTRATOR", true);

Refer docs here: https://dev.alfresco.com/resource/docs/java/org/alfresco/service/cmr/security/AuthorityService.html#...

Here is an example of a custom service which returns person information based on Group short name. You can choose to pass group name prefixed by "GROUP_" or just the shortName. example: GROUP_ADMINISTRATOR OR ADMINISTRATOR

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.PersonService;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GroupService {

private static final Logger LOGGER = LoggerFactory.getLogger(GroupService.class);

private static final String NAME = "name";
private static final String FIRST_NAME = "firstName";
private static final String LAST_NAME = "lastName";
private static final String EMAIL_ID = "emailId";
private static final String PERSON = "person";
private static final String PERSONS = "persons";

private static final String UNDERSCORE = "_";
private static final String GROUP_ = "GROUP_";

private final AuthorityService authorityService;

private final PersonService personService;

private final NodeService nodeService;

public GroupService(final ServiceRegistry serviceRegistry) {
super();
this.authorityService = serviceRegistry.getAuthorityService();
this.personService = serviceRegistry.getPersonService();
this.nodeService = serviceRegistry.getNodeService();
}

public JSONObject getUserInfoByGroup(final String groupShortName)
throws ServiceException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Started executing getUserInfoByGroup..");
}
final JSONObject userInfoByGroupName = new JSONObject();
if (StringUtils.isNotBlank(groupShortName)) {
try {
final String groupFQN = groupShortName.contains(GROUP_) ? groupShortName : GROUP_ + groupShortName;
//Get all users from any depth
final Set<String> users = authorityService.getContainedAuthorities(AuthorityType.USER, groupFQN, false);
if (users != null && !users.isEmpty()) {
final JSONArray userInfoArray = new JSONArray();
for (final String user : users) {
if(personService.personExists(user)) {
final Map<String, String> eachUser = new ConcurrentHashMap<String, String>();
final NodeRef userRef = personService.getPerson(userName);
//Populate more information as required in future. For now just need name and email for this example

final String firstName = (String) nodeService.getProperty(userRef, ContentModel.PROP_FIRSTNAME);
final String lastName = (String) nodeService.getProperty(userRef, ContentModel.PROP_LASTNAME);
final String email = (String) nodeService.getProperty(userRef, ContentModel.PROP_EMAIL);

eachUser.put(FIRST_NAME, firstName);
eachUser.put(LAST_NAME, lastName);
eachUser.put(NAME, insertSeparator(firstName, lastName, UNDERSCORE));
eachUser.put(EMAIL_ID, email);
userInfoArray.put(eachUser);
}
}
final JSONObject userInfo = new JSONObject();
userInfo.put(PERSON, userInfoArray);
userInfoByGroupName.put(PERSONS, userInfo);
}
} catch (JSONException | AlfrescoRuntimeException excp) {
final String errorMsg = String.format("Error occurred while fetching the User information due to: %s",
excp.getMessage());
LOGGER.error(errorMsg, excp);
throw new ServiceException(errorMsg, excp);
}
}
return userInfoByGroupName;
}

private String insertSeparator(final String str1, final String str2,
final String separator) {
final StringBuilder strBldr = new StringBuilder(str1);
strBldr.append(separator).append(str2);
return strBldr.toString();
}
}
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Hope this helps you.

~Abhinav
(ACSCE, AWS SAA-C02, GAIQ)