Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How can I access the user login details in publish?

Keerthana_H_N
Level 2
Level 2

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
Mali001
Correct answer by
Employee
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
SivaPrasadReddyS
Level 8
Level 8

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

 

 

Arun_Patidar
Community Advisor
Community Advisor

Hi,

try with 

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

Kiran_Vedantam
Community Advisor
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.

Mali001
Correct answer by
Employee
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

Jörg_Hoh
Employee
Employee

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.