Skip to main content
Adobe Employee
November 19, 2021
Resuelto

How can I access the user login details in publish?

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?

Este tema ha sido cerrado para respuestas.
Mali001Respuesta
Adobe Employee
November 19, 2021

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-service-users.html?lang=en

5 respuestas

Siva_Sogalapalli
Community Advisor
Community Advisor
November 19, 2021

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

 

 

Traducir
arunpatidar
Community Advisor
Community Advisor
November 19, 2021

Hi,

try with 

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

Traducir
Arun Patidar
Kiran_Vedantam
Community Advisor
Community Advisor
November 19, 2021

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.

Traducir
Mali001Adobe EmployeeRespuesta
Adobe Employee
November 19, 2021

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-service-users.html?lang=en

Traducir
joerghoh
Adobe Employee
Adobe Employee
November 20, 2021

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.

Traducir