Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

obtain user groups of the current user programatically.

Avatar

Level 3

hai developers , i have a question i want to know user belong to which groups through java program.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
4 Replies

Avatar

Correct answer by
Community Advisor

Avatar

Community Advisor

Hi, 

Something like this should work:

 

UserManager userManager = resourceResolver.adaptTo(UserManager.class);
Authorizable currentUser = userManager.getAuthorizable(resourceResolver.getUserID());
Iterator<Group> groupsIt = currentUser.memberOf();
 while (groupsIt.hasNext()) {
       Group group = groupsIt.next();
       String groupId = group.getID();
       log.info("User belongs to this group {}", groupId);
}

References:

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/get-the-users-from-aem-gro...

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-fetch-users-of-a-gr... 

 

Hope this helps



Esteban Bustamante

Avatar

Community Advisor

@EstebanBustamante has already written a nice code snip to help but would still like you to go through the original interface documentation at Interface Authorizable and Interface UserManager

 

Authorizable authorizable = resolver.adaptTo(Authorizable.class);
if (authorizable != null) {
GroupMembership groupMembership = authorizable.getGroupMembership();
List < String > groups = new ArrayList < > ();
Iterator < Authorizable > groupIter = groupMembership.iterator();
while (groupIter.hasNext()) {
groups.add(groupIter.next().getID());
}
response.getWriter().write("User groups: " + groups.toString());
}

 

You can also get the list of groups for a given username using the below OTB servlet - 

Use a GET request to the following endpoint, replacing {username} with the actual username:

http://{aem_instance}:4502/bin/user/{username}/groups

 

Hope this helps!

Avatar

Community Advisor

by passing the userid to this method, you can get all the uaergroups for that user id:

 

import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.ResourceResolver;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public Map<String, String> getAllUserGroups(String userId, ResourceResolver resourceResolver) {
Map<String, String> userGroups = new HashMap<>();
try {
UserManager userManager = resourceResolver.adaptTo(UserManager.class);
if (userManager != null) {
Authorizable authorizable = userManager.getAuthorizable(userId);
if (authorizable instanceof User) {
Iterator<Group> groups = ((User) authorizable).memberOf();
while (groups.hasNext()) {
Group group = groups.next();
userGroups.put(group.getID(), group.getProperty("./profile/givenName")[0].getString());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return userGroups;
}

 

 

-Get the UserManager instance from the ResourceResolver.
-Use the UserManager to get the Authorizable object for the user.
-Check if the Authorizable is a User.
-If it is a User, get the groups it is a member of.