April 27, 2020
Estimated Post Reading Time ~

How to get the UserInfo through Java API in AEM/Adobe CQ5

The below Java API helps to get the UserInfo details in AEM

@Reference
ResourceResolverFactory resolverFactory;

ResourceResolver adminResolver = null;
try {
       adminResolver = resolverFactory.getAdministrativeResourceResolver(null);
       final Session adminSession = adminResolver.adaptTo(Session.class);
       final UserManager userManager = adminResolver.adaptTo(UserManager.class);
       final User user = (User) userManager.getAuthorizable(adminSession.getUserID());
         
       logger.info("user.getID().."+user.getID());
       logger.info("user.isAdmin().."+user.isAdmin());
       logger.info("user.getPrincipal().getName().."+user.getPrincipal().getName());

       String lastName=user.getProperty("./profile/familyName")!=null?user.getProperty("./profile/familyName")[0].getString():null;
      String firstName=user.getProperty("./profile/givenName")!=null?user.getProperty("./profile/givenName")[0].getString():null;
      String aboutMe=user.getProperty("./profile/aboutMe")!=null?user.getProperty("./profile/aboutMe")[0].getString():null;
      String email=user.getProperty("./profile/email")!=null?user.getProperty("./profile/email")[0].getString():null;

     
      logger.info("lastName.."+lastName);
      logger.info("firstName.."+firstName);
      logger.info("aboutMe.."+aboutMe);
      logger.info("email.."+email);
         
       Iterator<Group> itr=user.memberOf();
       while(itr.hasNext())
       {
          Group group=(Group)itr.next();
           logger.info("group.getID().."+group.getID());
           logger.info("group.getPrincipal().getName().."+group.getPrincipal().getName());
        }        
                             
} catch (Exception e) {
        e.printStackTrace();
} finally {
       if (adminResolver != null) adminResolver.close();
}



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.