How can I get get user details in publish intance?
@SlingObject
private SlingHttpServletRequest request;
private User user = null;
/**
* Initial method to get the user id
*/
@PostConstruct
public void initModel() throws ParseException {
ResourceResolver resourceResolver = this.request.getResourceResolver();
final Session adminSession = resourceResolver.adaptTo(Session.class);
final UserManager userManager = resourceResolver.adaptTo(UserManager.class);
try {
user = (User) userManager.getAuthorizable(adminSession.getUserID());
} catch (RepositoryException e) {
LOGGER.error("RepositoryException While getting userID");
}
}
I tried getting user using current request and get details
user.getProperty("./profile/city")[0].getString(); which will return the string value. I tried in my author instance which was working fine. But same when I tried in publish instance not able to get the value.
Is there any other way to get the user login details in publish?
Solved! Go to Solution.
Views
Replies
Total Likes
Suppose you have created a service user and mapped it to your bundle like in [1]. The code snippet should work for you.
Session session =repository.loginAdministrative(repository.getDefaultWorkspace());
UserManager uMgr = ((org.apache.jackrabbit.api.JackrabbitSession)session).getUserManager();
....
...rest of your code....
please try to get the resource resolver using system user and then get the user id:
something like :
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "systemuser");
ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(param);
Session session = resolver.adaptTo(Session.class);
userId = resolver.getUserID();
LOGGER.info("USER ID :" + userId);
LOGGER.info("Session USER ID :" + session.getUserID());
Hi,
try with
User currentUser = request.getResourceResolver().adaptTo(User.class);
If you are querying something in AEM, you need a user who has the access to that path to fetch the results. But, in the publisher instance, you always access the content as an anyonymus user.
As per your question, if you want to get some properties from the content, get the content path, create a resource and access the properties.
Hope this helps!
Thanks,
Kiran Vedantam.
Suppose you have created a service user and mapped it to your bundle like in [1]. The code snippet should work for you.
Session session =repository.loginAdministrative(repository.getDefaultWorkspace());
UserManager uMgr = ((org.apache.jackrabbit.api.JackrabbitSession)session).getUserManager();
....
...rest of your code....
On publish it works the same way if you have that information stored there. Be aware, that by default (if you are not authenticated) all requests are executed using the "anonymous" user.
Views
Likes
Replies