Expand my Community achievements bar.

SOLVED

How can I access the user login details in publish?

Avatar

Employee

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?

1 Accepted Solution

Avatar

Correct answer by
Employee

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....

 

[1] https://experienceleague.adobe.com/docs/experience-manager-65/administering/security/security-servic...

View solution in original post

5 Replies

Avatar

Community Advisor

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());

 

 

Avatar

Community Advisor

Hi,

try with 

User currentUser = request.getResourceResolver().adaptTo(User.class);



Arun Patidar

Avatar

Community Advisor

Hi @Keerthana_H_N 

 

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.

Avatar

Correct answer by
Employee

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....

 

[1] https://experienceleague.adobe.com/docs/experience-manager-65/administering/security/security-servic...

Avatar

Employee Advisor

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.