Is it possible to get the current user session from osgi service like from slingrequest. I need to get the user session so that I can use the current user permission to replicate some assets.
Solved! Go to Solution.
Views
Replies
Total Likes
@thatsmeadarsh, I think that the simplest option will be to have/use service user for replication. So it could look like this, assuming you have access to resource resolver for system user:
In general, I do not think there will be an easy way to get session object for specific user in your use case. You can try to use impersonation functionality, but to do this you will need session of admin user anyway.
The best practice is to use a system user with the correct permissions to make such replication actions, but however, for your use case of acquiring the current, logged in user (from aem author), from a Sling Servlet, try using the code below:
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { Session session = request.getResourceResolver().adaptTo(Session.class); String userId = Objects.requireNonNull(session).getUserID(); }
Thanks for your response. Unfortunately I am not in servlet but in a service implementing ResourceChangelistner. The reason why I am not using system user is by taking current user, I'll be able to check if the current user have replication permission. If they have the permission, I can use the same session to replicate.
In servlet we used the same approach but now looking to do this within an osgi service.
Thanks
Hi @thatsmeadarsh, you can get user ID that initiates specific change directly from ResourceChange object, i.e.
@Override public void onChange(List<ResourceChange> changes) { for (ResourceChange change : changes) { String userId = change.getUserId(); // ... } }
@thatsmeadarsh, I think that the simplest option will be to have/use service user for replication. So it could look like this, assuming you have access to resource resolver for system user:
In general, I do not think there will be an easy way to get session object for specific user in your use case. You can try to use impersonation functionality, but to do this you will need session of admin user anyway.
there is nothing like "current user" for an OSGI service. The only concept in AEM which knows about the "current user" is the request, and derived from that a resourceResolver.
That means, to activate something in the context of the current user, it's the easiest if you use a session owned by "current user" to replicate things.
We have one requirement where in a metadata has an attribute Product Manager = "somebody@company.com". If the current logged in user's email ID matches that of an asset's Product Manager's value, we need to provide the current user jcr:all to the asset. This is logged in https://issues.apache.org/jira/browse/OAK-9763. A critical requirement for us and working with the concerned teams to have this prioritized
Views
Replies
Total Likes
If this is a critical requirement for you and you want to have this feature request prioritized, please raise a ticket with Adobe support. The forums are not the right place to get the proper attention to it.
(As this comment is not really related to the general topic of this thread in the forums, please raise it as a new thread. Thanks!)
Views
Replies
Total Likes