April 27, 2020
Estimated Post Reading Time ~

How to craete/manage the groups and users through Java API in AEM/Adobe CQ5?

This post will explain how to create/manage the groups and users through Java API.

import java.io.IOException;
import java.security.Principal;

import javax.jcr.PropertyType;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.servlet.Servlet;

import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
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.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service(value = Servlet.class)
@Component(immediate = true, metatype = true)
@Properties({
 @Property(name = "sling.servlet.paths", value = "/services/manageGroupsAndUsers"),
 @Property(name = "service.description", value = "ManageGroupsAndUsers"),
 @Property(name = "label", value = "ManageGroupsAndUsers") })

public class ManageGroupsAndUsers extends SlingAllMethodsServlet{

 /**
  *
  */
 private static final long serialVersionUID = 1L;

 @Reference
 ResourceResolverFactory resolverFactory;

 private static Logger logger = LoggerFactory.getLogger(ModifyCartServlet.class);

 protected final void doPost(final SlingHttpServletRequest request,
 final SlingHttpServletResponse response) throws IOException {

 String groupName="sampleGroup";
 String userName="sampleUser";
 String password="sampleUser";

 ResourceResolver adminResolver = null;
 Session adminSession=null;
 try {
        adminResolver = resolverFactory.getAdministrativeResourceResolver(null);
        adminSession = adminResolver.adaptTo(Session.class);
        final UserManager userManager= adminResolver.adaptTo(UserManager.class);
            
       if(null==userManager.getAuthorizable(groupName)){
      
        Group group=userManager.createGroup(groupName,new SimplePrincipal(groupName),"/home/groups/test");
      
        Value value=adminSession.getValueFactory().createValue("Sample Group", PropertyType.STRING);
       group.setProperty("./profile/givenName", value);
      
       value=adminSession.getValueFactory().createValue("Test Group", PropertyType.STRING);
       group.setProperty("./profile/aboutMe", value);
      
       value=adminSession.getValueFactory().createValue("albin@gmail.com", PropertyType.STRING);
       group.setProperty("./profile/email", value);      
             
        }else{
        response.getWriter().write("Group already exist..");
        }

     
       if(userManager.getAuthorizable(userName)==null){
        User user=userManager.createUser(userName, password,new SimplePrincipal(userName),"/home/users/test");
        Value value=adminSession.getValueFactory().createValue("Issac", PropertyType.STRING);
        user.setProperty("./profile/familyName", value);
      
        value=adminSession.getValueFactory().createValue("Albin", PropertyType.STRING);
        user.setProperty("./profile/givenName", value);
      
        value=adminSession.getValueFactory().createValue("Test User", PropertyType.STRING);
        user.setProperty("./profile/aboutMe", value);
      
        value=adminSession.getValueFactory().createValue("albin@gmail.com", PropertyType.STRING);
        user.setProperty("./profile/email", value);      
      
        }else
        {
         response.getWriter().write("User already exist..");
        }
     
     
       Group group = (Group)(userManager.getAuthorizable(groupName));
       group.addMember(userManager.getAuthorizable(userName));
                       
 } catch (Exception e) {
 e.printStackTrace();
 response.getWriter().write("Not able to perform User Management.."+resolverFactory);
 } finally {
        if (adminResolver != null) adminResolver.close();
             
 }

 }

 protected final void doGet(final SlingHttpServletRequest request,
 final SlingHttpServletResponse response) throws IOException {
 doPost(request, response);
 }



 private static class SimplePrincipal implements Principal {
        protected final String name;

        public SimplePrincipal(String name) {
            if (StringUtils.isBlank(name)) {
                throw new IllegalArgumentException("Principal name cannot be blank.");
            }
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public int hashCode() {
            return name.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof Principal) {
                return name.equals(((Principal) obj).getName());
            }
            return false;
        }
    }

}





By aem4beginner

No comments:

Post a Comment

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