Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

EstebanBustamante
Adobe Champion and Community Advisor
Adobe Champion and Community Advisor
September 19, 2024

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-group/m-p/418283

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-fetch-users-of-a-group-through-java-code/m-p/168443 

 

Hope this helps

Esteban Bustamante
Rohan_Garg
Community Advisor
Community Advisor
September 20, 2024

@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!

Rohan Garg
SureshDhulipudi
Community Advisor
Community Advisor
September 23, 2024

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.