3 Replies Latest reply on Jul 11, 2012 12:53 AM by mkiti

    How do I get the user information for a page?

    stewa52

      I would like to display information about the person that created a page in CQ.  I know I can get the createdBy information from the page properties, but how can I turn that into a user object where I can get more detialed information about that user?

        • 1. Re: How do I get the user information for a page?
          mkiti

          You can use the userManager to get an Authorizable object. With authorizable you are able to get the user profile and other information from the user.

           

          Example:

           

          final UserManager userManager = userManagerFactory.createUserManager(adminSession);

          final Authorizable authorizable = userManager.get(modifiedPage.getLastModifiedBy());

          final String lastModifiedByFullName = authorizable.getProfile().getName();

           

           

          For getting the UserManager you can also use a resource and adapt it to the UserMangager.

          e.g. resource.adaptTo(UserManager.class). This is based on jcr session and the loged in user should have permissions to access the UserManager.

           

          An other option is to get a UserManager with an admin session (see example). The UserManagerFactory is accessible as an Osgi service.

          • 2. Re: How do I get the user information for a page?
            stewa52 Level 1

            How would you get access to an admin session?

            • 3. Re: How do I get the user information for a page?
              mkiti Level 1

              If you are within an OSGi bundle you can use the SlingRepository Service. Now it depends how you develop your OSGi services and components. I use the maven scr plugin for this. With this plugin it is possible to get references to services with java annotaions.

               

               

              Example:

               

               

              @Component(name="SampleService",

                      label = "SampleService",

                      description = "SampleService",

                      immediate = true, enabled = true, metatype = true)

              @Services({ @Service(value = SampleService.class) })

              @Properties({

              @Property(name = "service.description", value = "SampleService"),

              @Property(name = "service.vendor", value = ") })

              public class SampleServiceImpl implements SampleService

               

               

              @Reference(policy = ReferencePolicy.STATIC)

              private SlingRepository repository;

               

               

                  public void openAdminSession() {

                      Session session = null;

                      if (repository != null) {

               

               

                          try {

                              session = repository.loginAdministrative(null);

                              //do something here

                          } catch (RepositoryException e) {

                              LOG.error("Unable to open admin session:" + e.toString());

                          } finally{

                                                        if(session != null){

                                    session.logout();

                                    }

                                }

                      }

                  }

              }

               

               

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

              To access a service from a JSP page you can use the reference of the SlingScriptHelper. You have to use the <cq:defineObjects/> tag.

               

               

              Example jsp:

               

               

              <cq:defineObjects/>

              SlingRepository repository = sling.getService(SlingRepository.class);

              Session session = null;

                      if (repository != null) {

               

               

                          try {

                              session = repository.loginAdministrative(null);

                              //do something here

                          } catch (RepositoryException e) {

                              LOG.error("Unable to open admin session:" + e.toString());

                          } finally{

                                                        if(session != null){

                                    session.logout();

                                    }

                                }

                      }