How to get the CQ5 userInfo in java or jsp by using jackrabbit

VAr picture VAr · Mar 21, 2014 · Viewed 10.5k times · Source

How to get the CQ5 userInfo by using org.apache.jackrabbit.api.security.user like name and group information in java or jsp .?

Answer

rakhi4110 picture rakhi4110 · Mar 21, 2014

In JSP / Java you can adapt your resource to UserManager class and get the current user or list down all the users and groups as per your requirement.

    Session session = resourceResolver.adaptTo(Session.class);
    UserManager userManager = resourceResolver.adaptTo(UserManager.class);
    /* to get the current user */
    Authorizable auth = userManager.getAuthorizable(session.getUserID());
    /* to get the property of the authorizable. Use relative path */
    Value[] names = auth.getProperty("./profile/familyName");
    /* to get the groups it is member of */
    Iterator<Group> groups = auth.memberOf(); 

To list all the users or groups, you can use findAuthorizables() method available in the UserManger.

You can also obtain the User info in JS using CQ.User.getCurrentUser() which would return an instance of the current User, with which you can access the user's properties.