Sabrina
Sabrina

Reputation: 143

Get the current user Liferay using a simple Java code

I'm working with : Liferay 6.0.6 with JBoss 5.1 and Struts2.

My question is, how to get the current user in Liferay once logged in, using a Java code.

Upvotes: 14

Views: 28258

Answers (3)

Charaf JRA
Charaf JRA

Reputation: 8334

This is an other possible way to do it :

private LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
User currentUser=liferayFacesContext.getUser()

Upvotes: 6

Felix Christy
Felix Christy

Reputation: 2193

Liferay provides Util class

com.liferay.portal.util.PortalUtil

This class contains all utility methods to get the frequently used attributes.

Try using PortalUtil.getUser(PortletRequest portletRequest) method to avoid creating new objects and references.

Upvotes: 6

František Hartman
František Hartman

Reputation: 15076

In your doView/processAction method do following

User user = (User) request.getAttribute(WebKeys.USER);

or use the ThemeDisplay object. It contains another information like companyId, groupId, ...

ThemeDisplay td  =(ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
User user = td.getUser();

Classes ThemeDisplay, User nad WebKeys are part of portal-service.jar.

If you need just some id to identify current user you can also use

String userId = request.getRemoteUser();

This solution is not Liferay specific and should be portable among jsr-286 portals.

Upvotes: 28

Related Questions