Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Adam C
How can I get the Screen Name of current user?
November 8, 2007 9:36 AM
Answer

Adam C

Rank: Junior Member

Posts: 70

Join Date: June 13, 2007

Recent Posts

Hi, I need to get the Screen Name (i.e. username) of the current user that is logged in. I will mostly need access inside of my Java code, but also in JSPs. Preferably a way that is compliant with JSR-168 and not specific to Liferay. I tried using the following, but it returns the User ID, which is a number (and useless to me).

1// returns the Liferay User ID
2String user = renderRequest.getRemoteUser();


Thanks!
Richard Sezov
RE: How can I get the Screen Name of current user?
November 8, 2007 10:24 AM
Answer

Richard Sezov

LIFERAY STAFF

Rank: Regular Member

Posts: 211

Join Date: February 7, 2007

Recent Posts

Hey Adam,

The JSR-168 remote user is mapped to the user ID in Liferay. To obtain the User object (which holds the field for the screen name), you have to make another call once you've retrieved the JSR-168 remote user:

1User user = UserServiceUtil.getUserById(Long.parseLong(remoteUserId));
Luis Colorado
RE: How can I get the Screen Name of current user?
April 16, 2008 3:03 PM
Answer

Luis Colorado

Rank: Regular Member

Posts: 110

Join Date: July 2, 2006

Recent Posts

But, is class User JSR-168 compliant?
Richard Sezov
RE: How can I get the Screen Name of current user?
April 21, 2008 5:28 AM
Answer

Richard Sezov

LIFERAY STAFF

Rank: Regular Member

Posts: 211

Join Date: February 7, 2007

Recent Posts

Hey Luis,

You're right: the User class is not a JSR-168 standard class. As far as I know, the standard simply calls for a user ID, and the portal vendors implement that in different ways. Liferay returns you the user number, which then makes it easy to get all of the rest of the information about the user through its own API. I have some experience with another vendor's portal, and they do it a little bit differently: they'll return you the login ID, but again, if you want any other information about the user, you need to use other methods to get at the data.

Rich
Paul R Saxman
RE: How can I get the Screen Name of current user?
May 13, 2008 10:32 AM
Answer

Paul R Saxman

Rank: New Member

Posts: 4

Join Date: April 17, 2008

Recent Posts

We use something similar to the following to get the user (screen) name from Liferay and still remain JSR-168 compliant:

 1String username = request.getRemoteUser();
 2
 3try
 4{
 5  Class usuClass = request.getClass().getClassLoader().loadClass( "com.liferay.portal.service.UserServiceUtil" );
 6  Method uidMethod = usuClass.getMethod( "getUserById", long.class );
 7  Object userObj = uidMethod.invoke( usuClass, Long.parseLong( username ) );
 8  Method loginMethod = userObj.getClass().getMethod( "getLogin", new Class[] {} );
 9  Object loginObj = loginMethod.invoke( userObj, new Object[] {} );
10  username = loginObj.toString();
11}
12catch ( ClassNotFoundException e )
13{
14  System.out.println( "This is not running in Liferay" );
15}
16catch ( Exception e )
17{
18  throw new RuntimeException( e );
19}


Paul S.
Chris Whittle
RE: How can I get the Screen Name of current user?
September 24, 2008 11:44 AM
Answer

Chris Whittle

Rank: Expert

Posts: 462

Join Date: September 17, 2008

Recent Posts

Hi Paul I tried this and got a number exception on line 7 is this working with a specific version of Liferay? I'm trying to use it with 5.11
Paul R Saxman
RE: How can I get the Screen Name of current user?
September 24, 2008 1:43 PM
Answer

Paul R Saxman

Rank: New Member

Posts: 4

Join Date: April 17, 2008

Recent Posts

After digging around a bit more, we found that you can get Liferay to return a screen name for getRemoteUser() instead of their internal Liferay identifier (UID). It appears that the simplest solution is to add <user-principal-strategy>screenName</user-principal-strategy>, "documented" at http://support.liferay.com/browse/LEP-4670, to a portlet's liferay-portlet.xml file in order to get Liferay to pass the users' screen names instead of their UID's to the portlets.

What I posted previously still seems to be the best solution, that I know of, for getting access to a user's information, i.e. full name, screen name, birth date, gender, etc., from Liferay directly. The code that we wrote was for Liferay 4.2, so it may have changed since.
Chris Whittle
RE: How can I get the Screen Name of current user?
September 24, 2008 5:23 PM
Answer

Chris Whittle

Rank: Expert

Posts: 462

Join Date: September 17, 2008

Recent Posts

awesome... that works like a charm
Kanakaraj Cheram
How can I get the Screen Name of current user?
November 17, 2008 6:56 AM
Answer

Kanakaraj Cheram

Rank: New Member

Posts: 20

Join Date: April 8, 2008

Recent Posts

User user = UserLocalServiceUtil.getUserById(pass liferay userid);

eg:


User user = UserLocalServiceUtil.getUserById(10129);

user.getScreenName(); --> this will give you screen name of the loggedin user

have a fun
Gildas Bescond
RE: How can I get the Screen Name of current user?
November 17, 2008 11:39 AM
Answer

Gildas Bescond

Rank: New Member

Posts: 7

Join Date: November 17, 2008

Recent Posts

Which Framework do you use ?
Using JSF, you can get the current user via the Faces Context :
 1    protected User getCurrentUser(){
 2        User u = null;
 3        FacesContext fc = FacesContext.getCurrentInstance();
 4        ExternalContext externalContext = fc.getExternalContext();
 5        if (externalContext.getUserPrincipal() == null) {
 6            logger.info("current principal is null");
 7        } else {
 8            Long id = Long.parseLong(externalContext.getUserPrincipal().getName());
 9            try {
10                u = UserLocalServiceUtil.getUserById(id);
11            } catch (PortalException ex) {
12                Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
13            } catch (SystemException ex) {
14                Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
15            }
16        }
17        return u;
18    }
Chris Whittle
RE: How can I get the Screen Name of current user?
December 17, 2008 6:55 AM
Answer

Chris Whittle

Rank: Expert

Posts: 462

Join Date: September 17, 2008

Recent Posts

Ray, how about the screen name? I see userid and other names in the user attributes but not screen name...
Thanks again for the post!
Chris Whittle
RE: How can I get the Screen Name of current user?
December 17, 2008 7:12 AM
Answer

Chris Whittle

Rank: Expert

Posts: 462

Join Date: September 17, 2008

Recent Posts

nevermind it we nickname
sancho sebastine
RE: How can I get the Screen Name of current user?
July 7, 2009 4:32 PM
Answer

sancho sebastine

Rank: New Member

Posts: 15

Join Date: October 18, 2008

Recent Posts

Kanakaraj Cheram:
User user = UserLocalServiceUtil.getUserById(pass liferay userid);

eg:


User user = UserLocalServiceUtil.getUserById(10129);

user.getScreenName(); --> this will give you screen name of the loggedin user

have a fun



Which package does UserLocalServiceUtil belong to ? Is it part of Liferay.

I want to access the username from a different web application which runs as a IFRAME
sancho sebastine
RE: How can I get the Screen Name of current user?
July 7, 2009 5:29 PM
Answer

sancho sebastine

Rank: New Member

Posts: 15

Join Date: October 18, 2008

Recent Posts

This is another method for accessing user information. May look like a little out of the usual, but nothing else was working for me.

The method is the query the User_ table in the lportal database. I am using the useruid to query for the user.

I took the userUid by

String useruid = request.getRemoteUser();


Once i have the uid i search the database with that, Here i am using Hibernate. You can also write a simple sql query.
UserDAO udao = new UserDAO();
User user = udao.findById(Long.parseLong(username));

It worked out well for me as i needed the list of all users and their information and not just the signed in user.


Hope it helps
sancho sebastine
RE: How can I get the Screen Name of current user?
July 14, 2009 10:00 PM
Answer

sancho sebastine

Rank: New Member

Posts: 15

Join Date: October 18, 2008

Recent Posts

Hi this link is not working now. Could you update this link. It looks like it has benifited a lot of guys
Ekansh Khandelwal
RE: How can I get the Screen Name of current user?
March 15, 2013 3:54 AM
Answer

Ekansh Khandelwal

Rank: New Member

Posts: 15

Join Date: March 6, 2013

Recent Posts

I want to know the user id of a user who posted on a blog....

How can i get .... please and body can tell me...
Ekansh Khandelwal
RE: How can I get the Screen Name of current user?
March 15, 2013 4:07 AM
Answer

Ekansh Khandelwal

Rank: New Member

Posts: 15

Join Date: March 6, 2013

Recent Posts

Chris Whittle:
Hi Paul I tried this and got a number exception on line 7 is this working with a specific version of Liferay? I'm trying to use it with 5.11



how can i get the user id of a user who posted an a blog
Ekansh Khandelwal
RE: How can I get the Screen Name of current user?
March 15, 2013 4:10 AM
Answer

Ekansh Khandelwal

Rank: New Member

Posts: 15

Join Date: March 6, 2013

Recent Posts

Adam C:
Hi, I need to get the Screen Name (i.e. username) of the current user that is logged in. I will mostly need access inside of my Java code, but also in JSPs. Preferably a way that is compliant with JSR-168 and not specific to Liferay. I tried using the following, but it returns the User ID, which is a number (and useless to me).

1// returns the Liferay User ID
2String user = renderRequest.getRemoteUser();


Thanks!


I want to know the user id of current user that is logged in into blog