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.
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?
Solved! Go to Solution.
Views
Replies
Total Likes
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) }
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
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) }
Thank you so much for your response. I am expecting something like this. I will use impersonate session. Again thanks for the code snippet.
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?
Views
Replies
Total Likes