Hello All,
How can we get the logged in user name or id in AEM service impl ?
Solved! Go to Solution.
Views
Replies
Total Likes
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();
}
}
}
}
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();
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
}
}
// ...
}
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();
}
}
}
}
Uhm, I want to use this way and my IDE blames the line "session.getUserPrincipal()". Reason: no method found. What does I miss?
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies