How to set username while replicating page in AEM | Community
Skip to main content
Mario248
Level 7
September 8, 2022
Solved

How to set username while replicating page in AEM

  • September 8, 2022
  • 2 replies
  • 1262 views

I am replicating the page using a custom workflow. There is no issue in replicating the page but AEM shows Published By as workflow-process-service in sites.html. I think it uses system users to replicate the pages. But I want to pass the user (who approved the workflow) in the replication API but there is no way of sending username in the replicator API. 

 

@3214626

private Replicator replicator;

...

replicator.replicate(session, ReplicationActionType.ACTIVATE, path, replicationOptions);

 

Is there any way to pass the username in replicator or any other API is available in AEM?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

Hi @mario248,

In general information about user who replicated page is passed via session object you are using in replicate method of replicator. You will need specific user session and use it in replicate method to achieve what you want. You can get the session using impersonation, here are some code snippets that could be useful.

import com.day.cq.replication.Replicator;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationOptions;
import javax.jcr.SimpleCredentials;

@Reference
private Replicator replicator;

//...

Session impersonatedSession = getImpersonatedSession(session, "test-user");

replicator.replicate(impersonatedSession, ReplicationActionType.ACTIVATE, path, replicationOptions)

// session - workflow-process-service user session
// userId - id of user you want to impersonate
private Session getImpersonatedSession(Session session, String userId) {
return session.impersonate(new SimpleCredentials(userId, new char[0]))
}

or

import com.day.cq.replication.Replicator;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationOptions;
import javax.jcr.SimpleCredentials;

import org.apache.sling.jcr.api.SlingRepository;
@Reference private Replicator replicator;

@Reference
private SlingRepository repository; //... Session impersonatedSession = getImpersonatedSession("test-user"); replicator.replicate(impersonatedSession, ReplicationActionType.ACTIVATE, path, replicationOptions) // userId - id of user you want to impersonate private Session getImpersonatedSession(String userId) { return session.repository.impersonateFromService("workflow-process-service", new SimpleCredentials("author1", new char[0]), null) }

!Please remember that user you will impersonate needs to have permission to replicate given page, in other case this will not work. So it's always good to have proper handling for this case, e.g. fallback to service user session.

2 replies

Jagadeesh_Prakash
Community Advisor
Community Advisor
September 8, 2022

@mario248  Why not you add a custom property in the jcr and save it as a session ? 

 

for ex: Approved by 

Mario248
Mario248Author
Level 7
September 9, 2022

Yeah, we can add custom property but this will not be used in Sites.html. When author check the status the custom property will not get picked. Below is the screen I am talking about

Need to overlay OOTB behavior to change to custom property. It would be good if I can simply pass the user in replicator 

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
September 9, 2022

Hi @mario248,

In general information about user who replicated page is passed via session object you are using in replicate method of replicator. You will need specific user session and use it in replicate method to achieve what you want. You can get the session using impersonation, here are some code snippets that could be useful.

import com.day.cq.replication.Replicator;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationOptions;
import javax.jcr.SimpleCredentials;

@Reference
private Replicator replicator;

//...

Session impersonatedSession = getImpersonatedSession(session, "test-user");

replicator.replicate(impersonatedSession, ReplicationActionType.ACTIVATE, path, replicationOptions)

// session - workflow-process-service user session
// userId - id of user you want to impersonate
private Session getImpersonatedSession(Session session, String userId) {
return session.impersonate(new SimpleCredentials(userId, new char[0]))
}

or

import com.day.cq.replication.Replicator;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationOptions;
import javax.jcr.SimpleCredentials;

import org.apache.sling.jcr.api.SlingRepository;
@Reference private Replicator replicator;

@Reference
private SlingRepository repository; //... Session impersonatedSession = getImpersonatedSession("test-user"); replicator.replicate(impersonatedSession, ReplicationActionType.ACTIVATE, path, replicationOptions) // userId - id of user you want to impersonate private Session getImpersonatedSession(String userId) { return session.repository.impersonateFromService("workflow-process-service", new SimpleCredentials("author1", new char[0]), null) }

!Please remember that user you will impersonate needs to have permission to replicate given page, in other case this will not work. So it's always good to have proper handling for this case, e.g. fallback to service user session.

Mario248
Mario248Author
Level 7
September 9, 2022

Thank you so much for your response. I am expecting something like this. I will use impersonate session. Again thanks for the code snippet.