4.7. Accessing user profile

The following code retrieves the details for a logged-in user:

// Alternative context: WebuiRequestContext context = WebuiRequestContext.getCurrentInstance() ;

PortalRequestContext context = PortalRequestContext.getCurrentInstance() ;
// Get the id of the user logged
String userId = context.getRemoteUser();
// Retrieve OrganizationService but it works only from WebUI code. See variants below in documentation
OrganizationService orgService = getApplicationComponent(OrganizationService.class) ;
// Request the information from OrganizationService:
if (userId != null)
{
  User user = orgService.getUserHandler().findUserByName(userId) ;
  if (user != null)
  {
    String firstName = user.getFirstName();
    String lastName = user.getLastName();
    String email = user.getEmail();
  }
}

Below are two alternatives for retrieving the Organization Service:

  1. OrganizationService service = (OrganizationService)
    
       ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(OrganizationService.class);
  2. OrganizationService service = (OrganizationService)
    
       PortalContainer.getInstance().getComponentInstanceOfType(OrganizationService.class);

Both alternatives are probably better than OrganizationService orgService = getApplicationComponent(OrganizationService.class) because you can use them from your own portlets or servlet/portlet filters. Variant with the getApplicationComponent variant works only from WebUI.

See also

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus