Expand my Community achievements bar.

SOLVED

Get the users from AEM group

Avatar

Level 4

Hi All,

 

Appreciate your comments here.

 

Use case:   get the users from aem group then send the mails..  

 

This code is written in aem custom workflow.

 

Working fine :  when click on asset and initiate custom workflow . I am getting group and able to get users.

 

Not working case :  when adding asset to folder and using luncher/event handler initiate the custom workflow. In this case below group is coming null

 

JackrabbitSession jrSession=(JackrabbitSession) session;

                     UserManager userManager=jrSession.getUserManager();

                     Group group = (Group) userManager.getAuthorizable("GroupId");

                     log.info("group >>> "+group);

                   final Iterator<Authorizable> memberslist = group.getMembers();

                  

                           while (memberslist.hasNext()) {

                           final Authorizable user = memberslist.next();

                                  if (user != null && !user.isGroup() && user.getProperty("./profile/email") != null) {

                                         //EMAIL ADDRESS FOR MEMBERS

                                         Value[] usrEmail = user.getProperty("./profile/email");

                                         for(Value emailGroup : usrEmail) {

                                                log.info("emailGroup line 152>>> "+emailGroup);

                                         }

                                  }

                           }

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 6

@chetan001 : Are you using any system user for the workflow? If you are already using then you can provide the permission on that user for the folders which is required for the workflow. Please check this.

 

Thank You.

 

Keshav Chaurasiya

 

View solution in original post

5 Replies

Avatar

Correct answer by
Level 6

@chetan001 : Are you using any system user for the workflow? If you are already using then you can provide the permission on that user for the folders which is required for the workflow. Please check this.

 

Thank You.

 

Keshav Chaurasiya

 

Avatar

Community Advisor

Hi @chetan001,

From the code snippet you have shared, suggest to make below changes

  • Way we get UserManager Object:
  • Authorizable first before typecasting it to Group.

UserManager :

  • If you are using com.adobe.granite.workflow.WorkflowSession (extends Adaptable),

 

 

ResourceResolver rescResolver = wfSession.adaptTo(ResourceResolver.class);
UserManager userManager = rescResolver.adaptTo(UserManager.class);

 

 

  • If you are using com.day.cq.workflow.WorkflowSession (Doesn't extends Adaptable)
    • We need to create ResourceResolver(using System user with desired permissions/Service Resolver) from ResourceResolverFactory Service Reference

 

 

@Reference
private ResourceResolverFactory resFactory;

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "demoService");
ResourceResolver rescResolver = null;
try {
   rescResolver = resFactory.getServiceResourceResolver(param);
} catch (LoginException e) {
   LOG.error("Login Exception={}", e.getMessage());
}

 

 

Authorizable First: (Check if authorizable is a group or no)

 

 

String userOrGroupId = "admin";
        try {
            Authorizable authorizable = userManagerResc.getAuthorizable(userOrGroupId);
            if (authorizable.isGroup()) {
                LOG.info("Id is a group");
                Group userGroup = (Group) authorizable;
                Iterator<Authorizable> declaredMembers = userGroup.getDeclaredMembers();
                while (declaredMembers.hasNext()) {
                    declaredMembers.next();
                }
            }
            else{
                LOG.info("It is a user !!");
            }
        } catch (RepositoryException e) {
            e.printStackTrace();
        }