Expand my Community achievements bar.

SOLVED

AEM6.5 How to get the workflow initator user-group

Avatar

Level 2

Hi All,
I have to check a condition where I need to get the user who initiated the workflow and then find it's user group.

if user-group belongs to "allowed-managers" then execute the piece of code, so how to get the user-group.??

 

public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
    String payload = workItem.getWorkflowData().getPayload().toString();
    ResourceResolver resourceResolver = resourceResolverUtil.getResourceResolver();
    Session session = resourceResolver.adaptTo(Session.class);
    String workflowInitiator = workItem.getWorkflow().getInitiator();
    //get the User-group for the person who has trigered the workflow.
    //check if user belongs to "allowed-managers" then execute the piece of code
    if(usergroup == "allowed-managers"){
    	//execute code
    }
}

 

 

I have tried a way getting null:

 

public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
    String payload = workItem.getWorkflowData().getPayload().toString();
    ResourceResolver resourceResolver = resourceResolverUtil.getResourceResolver();
    Session session = resourceResolver.adaptTo(Session.class);
    String workflowInitiator = workItem.getWorkflow().getInitiator();
    String usergroup = getUserGroup(resourceResolver, workflowInitiator);
    if(usergroup == "allowed-managers"){
    	//execute code
    }
}

private String getUserGroup(ResourceResolver resourceResolver, String userId) {
    String userGroup = null;

    try {
        UserManager userManager = resourceResolver.adaptTo(UserManager.class);
        Authorizable authorizable = userManager.getAuthorizable(userId);

        if (authorizable != null) {
            Iterator<Group> groups = authorizable.memberOf();

            while (groups.hasNext()) {
                Group group = groups.next();
                if (group.getID().equals("allowed-managers")) {
                    userGroup = group.getID();
                    break;
                }
            }
        }
    } catch (RepositoryException e) {
        // Handle exception
   
    }

    return userGroup;
}

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Marcos_aem ,

Below code is working fine with Servlet, You can update the code to work in WorkflowPorcess.

In WorkflowProcess, Get ResourceResolver from ResourceResolverFactory as below.

@Reference
ResourceResolverFactory resourceResolverFactory;

 

Session session = workflowSession.getSession();
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(Collections.singletonMap("user.jcr.session", (Object) session));

 

Servlet Class

//Getting resourceResolver from request
ResourceResolver resourceResolver = request.getResourceResolver();

//Getting userId from request parameter
String userId = request.getParameter("userId");

//UserManager Object from resourceResolver
UserManager userManager = resourceResolver.adaptTo(UserManager.class);

//Getting user from userId
User user = (User) userManager.getAuthorizable(userId);
if (user != null) {
//List of all groups
Iterator<Group> groups = user.memberOf();
while (groups.hasNext()) {
Group currentGroup = groups.next();
String groupID = currentGroup.getID();
//Comparing the groups with given groupId(Replace administrators with allowed-managers groupId)
if (groupID.equalsIgnoreCase("administrators")) {
//Custom Code
}
}
}

Hope this will help you !!

Regards,

Shiv

 

 

 

Shiv Prakash

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @Marcos_aem ,

Below code is working fine with Servlet, You can update the code to work in WorkflowPorcess.

In WorkflowProcess, Get ResourceResolver from ResourceResolverFactory as below.

@Reference
ResourceResolverFactory resourceResolverFactory;

 

Session session = workflowSession.getSession();
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(Collections.singletonMap("user.jcr.session", (Object) session));

 

Servlet Class

//Getting resourceResolver from request
ResourceResolver resourceResolver = request.getResourceResolver();

//Getting userId from request parameter
String userId = request.getParameter("userId");

//UserManager Object from resourceResolver
UserManager userManager = resourceResolver.adaptTo(UserManager.class);

//Getting user from userId
User user = (User) userManager.getAuthorizable(userId);
if (user != null) {
//List of all groups
Iterator<Group> groups = user.memberOf();
while (groups.hasNext()) {
Group currentGroup = groups.next();
String groupID = currentGroup.getID();
//Comparing the groups with given groupId(Replace administrators with allowed-managers groupId)
if (groupID.equalsIgnoreCase("administrators")) {
//Custom Code
}
}
}

Hope this will help you !!

Regards,

Shiv

 

 

 

Shiv Prakash

Avatar

Community Advisor

Hello @Marcos_aem 

 

The code doesn't say much about how ResourceResolver  was retrieved.

Could you possibly check, which user does it belong to? Does that user have access to /home/users and /home/groups to read the info about the Workflow Initiator and also the groups that he belongs to?


Aanchal Sikka