Highlighted

How do we get current AEM author logged in user id in osgi service/handler

rajank91603111

04-03-2019

is there way to get current author logged in user in OSGI service/handler  where we don't have slingHttpRequest object.

sling servlet  easily current user id can get via below code (if i have logged to AEM author instance as testuser, USER id gives here testuser)

ResourceResolver resourceResolver = request.getResourceResolver();

session = resourceResolver.adaptTo(Session.class);

String userId = session.getUserID()

LOGGER.info("USER ID :" + userID);

but if same things trying to get in customhandler class which implement AuthenticationFeedbackHandler ,where we don't have slinghttpservletrequest, we have HttpServletRequest.

public boolean authenticationSucceeded(HttpServletRequest request,

HttpServletResponse response, AuthenticationInfo authInfo) {

tried below code, t's give system user id i.e test

Map<String, Object> param = new HashMap<String, Object>();

        param.put(ResourceResolverFactory.SUBSERVICE, "test");

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

-------------------------------------------------------------------------------------------------------------------------

If i use getAdministractiveresoolver method, it's give user id as admin

adminResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);

Session adminsession = adminResolver.adaptTo(Session.class);

userID = adminsession.getUserID();

LOGGER.info("USER ID :" + userID);

}

Replies

Highlighted

rajank91603111

04-03-2019

To use  this Session session = resourceResolver.adaptTo(Session.class);

need resourceResolver object which can get it via ResourceResolverFactory ,

below source code to get resource resolver , need to pass param object which will have system user.

resourcerResolver = resourceResolverFactory.getServiceResourceResolver(param);

Session session = resourcerResolver.adaptTo(Session.class);

session.getUserID();  // this give system user id

below code  getAdministrativeResourceResolver method to get resourceResolver,

resourceResolver  = resourceResolverFactory.getAdministrativeResourceResolver(null);

Session session = resourcerResolver.adaptTo(Session.class);

session.getUserID(); // this gives admin user id

any other alternative solution where method have HttpServletRequest