Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Impersonate user in AEM 6.2

Avatar

Level 1

How to impersonate the user from code in AEM 6.2?

I understand it is not the same way as AEM 6.0. Please help.

1 Accepted Solution

Avatar

Correct answer by
Level 1

This worked for us:

@Reference

    private ResourceResolverFactory resourceResolverFactory;

@Reference

    private SlingRepository repository;

   

public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {

        Session session = workflowSession.getSession();

        ResourceResolver resourceResolver = resourceResolver = getResourceResolver(resourceResolverFactory, session);

        final String initiator = workItem.getWorkflow().getInitiator();

        Session userSession = repository.impersonateFromService("workflow-process-service", new SimpleCredentials(initiator, "".toCharArray()), null);

//other logic

}

View solution in original post

4 Replies

Avatar

Level 8

Hi,

The below should work in AEM 6.2

(repository.impersonateFromService(Constants.WORKFLOW_USER,

  new SimpleCredentials(item.getWorkflow().getInitiator();, "".toCharArray()), null), resolverFactory)

Avatar

Correct answer by
Level 1

This worked for us:

@Reference

    private ResourceResolverFactory resourceResolverFactory;

@Reference

    private SlingRepository repository;

   

public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {

        Session session = workflowSession.getSession();

        ResourceResolver resourceResolver = resourceResolver = getResourceResolver(resourceResolverFactory, session);

        final String initiator = workItem.getWorkflow().getInitiator();

        Session userSession = repository.impersonateFromService("workflow-process-service", new SimpleCredentials(initiator, "".toCharArray()), null);

//other logic

}