Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Get Logged in username in AEM6.5

Avatar

Level 3

Hello All,

 

How can we get the logged in user name or id in AEM service impl ?

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi @sateeshreddy 

 

To get the logged-in user's name or ID in an AEM service impl .you can use the ResourceResolver and Request objects available in the service context.

 

Here's a code snippet demonstrating how to retrieve the logged-in user's information in an AEM service impl.

 

 

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.LoginException;
import javax.jcr.Session;

@Component(immediate = true, service = YourService.class)
public class YourServiceImpl implements YourService {

@Reference
private ResourceResolverFactory resourceResolverFactory;

public void yourServiceMethod() {
ResourceResolver resolver = null;
try {
// Get the ResourceResolver
resolver = resourceResolverFactory.getServiceResourceResolver(null);

// Access the current user's session
Session session = resolver.adaptTo(Session.class);

if (session != null && session.isLive()) {
// Get the User object representing the logged-in user
org.apache.jackrabbit.api.security.user.User user = (org.apache.jackrabbit.api.security.user.User) session.getUserPrincipal();

// Retrieve user ID and name
String userId = user.getID();
String userName = user.getPrincipal().getName();

// Now you can use userId and userName as needed
// ...

}
} catch (LoginException e) {
// Handle login exception
} finally {
// Close the ResourceResolver when done to release resources
if (resolver != null && resolver.isLive()) {
resolver.close();
}
}
}
}

 

View solution in original post

4 Replies

Avatar

Community Advisor

You can get userId by adapt resourceResolver to Session.

Session session = resourceResolver.adaptTo(Session.class);
session.getUserID();

 

To get User Name, Email, Groups, You need adapt resourceResolver to UserManager

Session session = resourceResolver.adaptTo(Session.class);
UserManager userManager = resourceResolver.adaptTo(UserManager.class);
/* to get the current user */
Authorizable auth = userManager.getAuthorizable(session.getUserID());
/* to get the property of the authorizable. Use relative path */
Value[] names = auth.getProperty("./profile/familyName");
Value[] emails = authorizable.getProperty("profile/email");
/* to get the groups it is member of */
Iterator<Group> groups = auth.memberOf();

 Reference: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/what-s-the-best-way-to-get...

 

Avatar

Employee

you can obtain the logged-in user's name or ID in a service implementation by using the ResourceResolver and UserManager classes. Here's a sample code on how to achieve this:

 

 

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.LoginException;
import org.apache.jackrabbit.api.security.user.UserManager;
import javax.jcr.Session;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.ResourceResolverFactory;

@Component(service = ExampleService.class)
public class ExampleService {

@Reference
    private ResourceResolverFactory resourceResolverFactory;

    // ...

    public void exampleMethod() {
        try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(null)) {
            // Get the User ID and Name
            Session session = resourceResolver.adaptTo(Session.class);
            UserManager userManager = resourceResolver.adaptTo(UserManager.class);

            String userId = session.getUserID();
            String userName = userManager.getAuthorizable(session.getUserID()).getPrincipal().getName();

            // Use the userId and userName as needed
            // ...
        } catch (LoginException e) {
            // Handle the LoginException if there's an issue getting the ResourceResolver
        } catch (RepositoryException e) {
            // Handle the RepositoryException if there's an issue getting the UserManager
        }
    }

    // ...
}

 

 

Avatar

Correct answer by
Employee

Hi @sateeshreddy 

 

To get the logged-in user's name or ID in an AEM service impl .you can use the ResourceResolver and Request objects available in the service context.

 

Here's a code snippet demonstrating how to retrieve the logged-in user's information in an AEM service impl.

 

 

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.LoginException;
import javax.jcr.Session;

@Component(immediate = true, service = YourService.class)
public class YourServiceImpl implements YourService {

@Reference
private ResourceResolverFactory resourceResolverFactory;

public void yourServiceMethod() {
ResourceResolver resolver = null;
try {
// Get the ResourceResolver
resolver = resourceResolverFactory.getServiceResourceResolver(null);

// Access the current user's session
Session session = resolver.adaptTo(Session.class);

if (session != null && session.isLive()) {
// Get the User object representing the logged-in user
org.apache.jackrabbit.api.security.user.User user = (org.apache.jackrabbit.api.security.user.User) session.getUserPrincipal();

// Retrieve user ID and name
String userId = user.getID();
String userName = user.getPrincipal().getName();

// Now you can use userId and userName as needed
// ...

}
} catch (LoginException e) {
// Handle login exception
} finally {
// Close the ResourceResolver when done to release resources
if (resolver != null && resolver.isLive()) {
resolver.close();
}
}
}
}

 

Avatar

Level 7

Uhm, I want to use this way and my IDE blames the line "session.getUserPrincipal()". Reason: no method found. What does I miss?