15

How can I get the current user connected to a Liferay portal with a simple Java code?

I'm using Liferay 6.0.6

Jeremy
  • 21,351
  • 4
  • 61
  • 77
med
  • 151
  • 1
  • 1
  • 3
  • See this: http://stackoverflow.com/questions/10448193/get-the-current-user-liferay-using-a-simple-java-code – dragon66 May 09 '12 at 20:35
  • This is possible duplicate of the issue mentioned by @dragon66. This might also help http://stackoverflow.com/questions/970986/accessing-the-user-from-a-liferay-portlet – Prakash K May 10 '12 at 07:03

3 Answers3

24

Simply:

User currentUser = PortalUtil.getUser(request);
Mark
  • 14,967
  • 11
  • 61
  • 90
10

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 and 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.

Source: Get the current user Liferay using a simple Java code

Community
  • 1
  • 1
Ravi Kumar Gupta
  • 866
  • 1
  • 11
  • 30
3

In Java Code:

  • UserLocalServiceUtil.getXXX methods are there, choose as you want.

In JSP Code:

  • themeDisplay.getUserId() will give you the current user id
  • themeDisplay.getUser() will give you the object of current User.
Flexo
  • 82,006
  • 22
  • 174
  • 256
Vipin Bardia
  • 179
  • 5