April 10, 2020
Estimated Post Reading Time ~

Working with User and Groups in AEM

Today we are going to look into the Users/Groups API that can be used to get the information about all the users and groups present in the JCR.

For this, we will be using org.apache.jackrabbit.api.security.user.UserManager API which looks for all the users and groups in the JCR.

User Component

To list all the users and groups on the page, we will be creating a cq:Component and it will search in the JCR to get all the data.
  • Create component User with the following configuration
User Component
  • Rename the user.jsp to user.html and paste the following code in it.
<h1>Users/Groups in the AEM JCR are : </h1>
<sly
data-sly-use.object="org.redquark.demo.core.cqcomponents.UserComponent" />
<div>
<h2>Users</h2>
<ol data-sly-list="${object.users}">
<li>${item}</li>
</ol>
<h2>Groups</h2>
<ol data-sly-list="${object.groups}">
<li>${item}</li>
</ol>
</div>
  • Here we are iterating the list of users and groups and displaying their IDs.
  • Now create a class named UserComponent and paste the following code in it.
package org.redquark.demo.core.cqcomponents;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.jcr.Session;

import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.sightly.WCMUsePojo;

/**
 * @author Anirudh Sharma
 * 
 * This component lists all the users and groups present in the JCR
 */
public class UserComponent extends WCMUsePojo {

 /**
  * Logger
  */
 private static final Logger log = LoggerFactory.getLogger(UserComponent.class);

 private List < String > users, groups;

 private Session session;

 @Override
 public void activate() throws Exception {

  try {

   log.info("----------< Processing starts >----------");

   ResourceResolver resourceResolver = getResourceResolver();

   session = resourceResolver.adaptTo(Session.class);

   UserManager userManager = ((JackrabbitSession) session).getUserManager();

   Iterator < Authorizable > userIterator = userManager.findAuthorizables("jcr:primaryType", "rep:User");
   Iterator < Authorizable > groupIterator = userManager.findAuthorizables("jcr:primaryType", "rep:Group");

   users = new LinkedList < > ();
   groups = new LinkedList < > ();

   while (userIterator.hasNext()) {

    log.info("Getting user");

    Authorizable user = userIterator.next();

    if (!user.isGroup()) {
     log.info("User found: {}", user.getID());
     users.add(user.getID());
    }
   }

   while (groupIterator.hasNext()) {

    log.info("Getting group");

    Authorizable group = groupIterator.next();

    if (group.isGroup()) {
     log.info("Group found {}", group.getID());
     groups.add(group.getID());
    }
   }
  } catch (Exception e) {

   log.error(e.getMessage(), e);
  }
 }


 /**
  * @return the users
  */
 public List < String > getUsers() {
  return users;
 }

 /**
  * @return the groups
  */
 public List < String > getGroups() {
  return groups;
 }

}
  • Here we are creating two lists containing users' and groups' IDs
  • Deploy the code and drag & drop the component on the page. All the IDs will be listed as follows
Users and Groups


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.