Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

How to identify if the user is admin user or have admin privileges?

Avatar

Level 7

How to check if the user is admin or having administrator privilages. 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

It's great that the response from this thread showcases solutions for how to programmatically check if a user belongs to the administrator's group, but however, you can manually check this in the UI as well.

1. visit: localhost:4502/useradmin

2. search for the group administrators

3. click on members; you should be able to see all the members here who are associated with the administrator's group.

admin.PNG

View solution in original post

5 Replies

Avatar

Community Advisor

Hi @akshaybhujbale,

Assuming you would like to do it programmatically you can use classes from org.apache.jackrabbit.api.security.user package. Mainly it will be UserManager, Authorizable, User and Group.

  • To check if user is an admin use isAdmin() method like this:
    import org.apache.jackrabbit.api.security.user.*;

    UserManager userManager = resourceResolver.adaptTo(UserManager.class); User user = (User) userManager.getAuthorizable("test-user"); user.isAdmin();
  • To verify if user is part of administrators group you can try one of below alternative options:
    import org.apache.jackrabbit.api.security.user.*;
    
    UserManager userManager = resourceResolver.adaptTo(UserManager.class);
    User user = (User) userManager.getAuthorizable("test-user");
    
    Group group = (Group) userManager.getAuthorizable("administrators");
    group.isMember(user);
    second option for the same
    import org.apache.jackrabbit.api.security.user.*;
    
    UserManager userManager = resourceResolver.adaptTo(UserManager.class);
    User user = (User) userManager.getAuthorizable("test");
    
    Iterator<Group> groups = user.memberOf();
    while (groups.hasNext()) {
        Group group = groups.next();
        if (group.getID().equals("administrators")) {
            break;
        }
    } 

In general memberOf will return all groups hierarchy so even if user is not directly assigned to administrators group but is member of other group that is added to administrators group you will get appropriate result.

Avatar

Community Advisor

To check user is admin

UserManager userManager = resourceResolver.adaptTo(UserManager.class);
User user = (User) userManager.getAuthorizable("myuser");
user.isAdmin(); // returns true if myuser is admin

To check user is part of administrative group

Group group = (Group) userManager.getAuthorizable("administrators");
group.isMember(user);// true if the Authorizable to test is a direct or indirect member

Avatar

Level 7

Hi @HeenaMadan 

 

Do you what is logic behind 

user.isAdmin();

like what is check it check if the user is admin user or not. 

Avatar

Correct answer by
Community Advisor

It's great that the response from this thread showcases solutions for how to programmatically check if a user belongs to the administrator's group, but however, you can manually check this in the UI as well.

1. visit: localhost:4502/useradmin

2. search for the group administrators

3. click on members; you should be able to see all the members here who are associated with the administrator's group.

admin.PNG