Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How to set username while replicating page in AEM

Avatar

Level 9

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. 

 

@reference

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

5 Replies

Avatar

Community Advisor

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

 

for ex: Approved by 

Avatar

Level 9

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

Mario248_0-1662702206953.png

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

Avatar

Correct answer by
Community Advisor

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.

Avatar

Level 9

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

Avatar

Level 4

I tried these codes. But it occured error 'Caused by: javax.security.auth.login.FailedLoginException: Impersonation not allowed.' I cannot use workflow-process-service to impersonate other accounts. How can I solve this problem?