hai developers , i have a question i want to know user belong to which groups through java program.
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
@AliSyed1 : You can make user of these APIs, please refer :
For more details:
https://jackrabbit.apache.org/api/2.12/org/apache/jackrabbit/api/security/user/User.html
https://jackrabbit.apache.org/api/2.12/org/apache/jackrabbit/api/security/user/Authorizable.html
@AliSyed1 : You can make user of these APIs, please refer :
For more details:
https://jackrabbit.apache.org/api/2.12/org/apache/jackrabbit/api/security/user/User.html
https://jackrabbit.apache.org/api/2.12/org/apache/jackrabbit/api/security/user/Authorizable.html
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:
Hope this helps
@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!
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.
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies