How can I get the current user in Liferay?
Asked Answered
I

3

16

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

I'm using Liferay 6.0.6

Inexpungible answered 9/5, 2012 at 20:10 Comment(2)
See this: #10448693Incrassate
This is possible duplicate of the issue mentioned by @dragon66. This might also help #971486Daglock
B
28

Simply:

User currentUser = PortalUtil.getUser(request);
Botanist answered 10/5, 2012 at 7:38 Comment(0)
B
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

Battle answered 11/5, 2012 at 16:51 Comment(0)
P
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.
Perplex answered 10/5, 2012 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.