User Group Management


Get User manager 

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.jcr.base.util.AccessControlUtil;
UserManager userManager = AccessControlUtil.getUserManager(jcrSession);
Authorizable authorizable = userManager.getAuthorizable(name);
if (authorizable instanceof User) {
  user = (User)authorizable;
}  
if (authorizable instanceof Group) {
            user = (Group)authorizable;
} 
In JSP:

<%@page import="com.day.cq.security.UserManager"%>

<%@page import="com.day.cq.security.UserManagerFactory"%>

<%@page import="org.apache.jackrabbit.api.security.user.Authorizable"%>

<%@page import="org.apache.jackrabbit.api.security.user.Group"%>

Session session = resourceResolver.adaptTo(Session.class);
UserManagerFactory factory = sling.getService(UserManagerFactory.class);

UserManager userManager = factory.createUserManager(session);

Or

//This is org.apache.jackrabbit.api.security.user.UserManager which is recommeded

UserManager userManager = AccessControlUtil.getUserManager(session);

Group group = (Group)((<Resource>).adaptTo(Authorizable.class));

User user = (User)((<Resource>).adaptTo(Authorizable.class));


Comments