Expand my Community achievements bar.

SOLVED

How to loop through a group to validate if a user is part of the group using ECMAScript

Avatar

Level 2

Hi Team,

 

I am working on a workflow requirement, where i am using the ECMA Script on a OR Split. The ask is to fetch the user who initiated the workflow and check if the user is part of the specified group.

 

Part of the 2 requirement statements, we are able to fetch the user info , but looking to find the code snippet to loop through a group and list the users.

 

OR If there is any other solution all together address the complete requirement, please let me know.

 

Any leads on this are appreciated!!

 

Thanks,

Raghu

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @raghud1980,

In general ECMA script in OR split is a bit limited as it only supports following object workflowData, jcrSession, workflowSession. However it still should be possible to achieve what you want.

Sadly workItem or graniteWorkItem is not available under OR split, in that case workflowData object can be utilize like this:

 

var initiator =  workflowData.getMetaDataMap().get("userId", "");

 

Next having information about workflow initiator you have to use UserManager class to first get Authorizable object that will expose information about groups given user is member of.

Last step, is to check if set of groups contains specif group that is main condition for OR split - in below script it is represented by groupMemberID.

Here is a complete script:

 

function check() {
  // put id of specifc group under groupMemberID variable
  var groupMemberID = "your-specific-group-id";
  var isGroupMember = false;
  var initiator =  workflowData.getMetaDataMap().get("userId", "");
  var resourceResolver = graniteWorkflowSession.adaptTo(Packages.org.apache.sling.api.resource.ResourceResolver);
  if (resourceResolver != null) {
    var userManager = resourceResolver.adaptTo(Packages.org.apache.jackrabbit.api.security.user.UserManager);
    if (userManager != null) {
      var authorizable = userManager.getAuthorizable(initiator);
      if (authorizable != null) {
        var groups = authorizable.memberOf();
        while(groups.hasNext()) {
	  var group = groups.next();
          if (group.getID().equals(groupMemberID)) {
	    isGroupMember = true;
            break;
          }
        }
      }
    }
  }
  return isGroupMember;
}

 

View solution in original post

3 Replies

Avatar

Community Advisor

Below java code snippet will help to start

 

ResourceResolver resolver = wfsession.adaptTo(ResourceResolver.class);
  UserManager userManager = resolver.adaptTo(UserManager.class);
 
  Workflow workflow = workitem.getWorkflow();
  String initiator = workitem.getWorkflow().getInitiator();   // Gives who initiated the workflow
  
  Authorizable authorizable = null;
  try {
   authorizable = userManager.getAuthorizable(initiator);
  // you can use memberOf() to get list of groups associated

} catch (RepositoryException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }

Avatar

Correct answer by
Community Advisor

Hi @raghud1980,

In general ECMA script in OR split is a bit limited as it only supports following object workflowData, jcrSession, workflowSession. However it still should be possible to achieve what you want.

Sadly workItem or graniteWorkItem is not available under OR split, in that case workflowData object can be utilize like this:

 

var initiator =  workflowData.getMetaDataMap().get("userId", "");

 

Next having information about workflow initiator you have to use UserManager class to first get Authorizable object that will expose information about groups given user is member of.

Last step, is to check if set of groups contains specif group that is main condition for OR split - in below script it is represented by groupMemberID.

Here is a complete script:

 

function check() {
  // put id of specifc group under groupMemberID variable
  var groupMemberID = "your-specific-group-id";
  var isGroupMember = false;
  var initiator =  workflowData.getMetaDataMap().get("userId", "");
  var resourceResolver = graniteWorkflowSession.adaptTo(Packages.org.apache.sling.api.resource.ResourceResolver);
  if (resourceResolver != null) {
    var userManager = resourceResolver.adaptTo(Packages.org.apache.jackrabbit.api.security.user.UserManager);
    if (userManager != null) {
      var authorizable = userManager.getAuthorizable(initiator);
      if (authorizable != null) {
        var groups = authorizable.memberOf();
        while(groups.hasNext()) {
	  var group = groups.next();
          if (group.getID().equals(groupMemberID)) {
	    isGroupMember = true;
            break;
          }
        }
      }
    }
  }
  return isGroupMember;
}