Foren

How can I get the Screen Name of current user?

Adam Causey, geändert vor 16 Jahren.

How can I get the Screen Name of current user?

Junior Member Beiträge: 70 Beitrittsdatum: 13.06.07 Neueste Beiträge
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).

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


Thanks!
thumbnail
Rich Sezov, geändert vor 16 Jahren.

RE: How can I get the Screen Name of current user?

Regular Member Beiträge: 220 Beitrittsdatum: 07.02.07 Neueste Beiträge
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:

User user = UserServiceUtil.getUserById(Long.parseLong(remoteUserId));
Luis Colorado, geändert vor 16 Jahren.

RE: How can I get the Screen Name of current user?

Regular Member Beiträge: 110 Beitrittsdatum: 03.07.06 Neueste Beiträge
But, is class User JSR-168 compliant?
thumbnail
Rich Sezov, geändert vor 16 Jahren.

RE: How can I get the Screen Name of current user?

Regular Member Beiträge: 220 Beitrittsdatum: 07.02.07 Neueste Beiträge
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, geändert vor 15 Jahren.

RE: How can I get the Screen Name of current user?

New Member Beiträge: 4 Beitrittsdatum: 17.04.08 Neueste Beiträge
We use something similar to the following to get the user (screen) name from Liferay and still remain JSR-168 compliant:

String username = request.getRemoteUser();

try
{
  Class usuClass = request.getClass().getClassLoader().loadClass( "com.liferay.portal.service.UserServiceUtil" );
  Method uidMethod = usuClass.getMethod( "getUserById", long.class );
  Object userObj = uidMethod.invoke( usuClass, Long.parseLong( username ) );
  Method loginMethod = userObj.getClass().getMethod( "getLogin", new Class[] {} );
  Object loginObj = loginMethod.invoke( userObj, new Object[] {} );
  username = loginObj.toString();
}
catch ( ClassNotFoundException e )
{
  System.out.println( "This is not running in Liferay" );
}
catch ( Exception e )
{
  throw new RuntimeException( e );
}


Paul S.
thumbnail
Chris Whittle, geändert vor 15 Jahren.

RE: How can I get the Screen Name of current user?

Expert Beiträge: 462 Beitrittsdatum: 17.09.08 Neueste Beiträge
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, geändert vor 15 Jahren.

RE: How can I get the Screen Name of current user?

New Member Beiträge: 4 Beitrittsdatum: 17.04.08 Neueste Beiträge
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.
thumbnail
Chris Whittle, geändert vor 15 Jahren.

RE: How can I get the Screen Name of current user?

Expert Beiträge: 462 Beitrittsdatum: 17.09.08 Neueste Beiträge
awesome... that works like a charm
Ekansh Khandelwal, geändert vor 11 Jahren.

RE: How can I get the Screen Name of current user?

New Member Beiträge: 15 Beitrittsdatum: 07.03.13 Neueste Beiträge
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
thumbnail
Ray Augé, geändert vor 15 Jahren.

RE: How can I get the Screen Name of current user?

Liferay Legend Beiträge: 1197 Beitrittsdatum: 08.02.05 Neueste Beiträge
Have you considered using the spec provided USER_INFO?

See [HOWTO] Personalization - Getting current user attributes .
thumbnail
Chris Whittle, geändert vor 15 Jahren.

RE: How can I get the Screen Name of current user?

Expert Beiträge: 462 Beitrittsdatum: 17.09.08 Neueste Beiträge
ooooo! even cooler... thanks Ray!
thumbnail
kanakaraj cheram, geändert vor 15 Jahren.

How can I get the Screen Name of current user?

New Member Beiträge: 20 Beitrittsdatum: 08.04.08 Neueste Beiträge
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
sancho sebastine, geändert vor 14 Jahren.

RE: How can I get the Screen Name of current user?

New Member Beiträge: 15 Beitrittsdatum: 18.10.08 Neueste Beiträge
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
thumbnail
Chris Whittle, geändert vor 15 Jahren.

RE: How can I get the Screen Name of current user?

Expert Beiträge: 462 Beitrittsdatum: 17.09.08 Neueste Beiträge
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!
thumbnail
Chris Whittle, geändert vor 15 Jahren.

RE: How can I get the Screen Name of current user?

Expert Beiträge: 462 Beitrittsdatum: 17.09.08 Neueste Beiträge
nevermind it we nickname
Ekansh Khandelwal, geändert vor 11 Jahren.

RE: How can I get the Screen Name of current user?

New Member Beiträge: 15 Beitrittsdatum: 07.03.13 Neueste Beiträge
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...
sancho sebastine, geändert vor 14 Jahren.

RE: How can I get the Screen Name of current user?

New Member Beiträge: 15 Beitrittsdatum: 18.10.08 Neueste Beiträge
Hi this link is not working now. Could you update this link. It looks like it has benifited a lot of guys
Gildas Bescond, geändert vor 15 Jahren.

RE: How can I get the Screen Name of current user?

New Member Beiträge: 7 Beitrittsdatum: 17.11.08 Neueste Beiträge
Which Framework do you use ?
Using JSF, you can get the current user via the Faces Context :
	protected User getCurrentUser(){
		User u = null;
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext externalContext = fc.getExternalContext();
		if (externalContext.getUserPrincipal() == null) {
			logger.info("current principal is null");
		} else {
			Long id = Long.parseLong(externalContext.getUserPrincipal().getName());
			try {
				u = UserLocalServiceUtil.getUserById(id);
			} catch (PortalException ex) {
				Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
			} catch (SystemException ex) {
				Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return u;
	}
sancho sebastine, geändert vor 14 Jahren.

RE: How can I get the Screen Name of current user?

New Member Beiträge: 15 Beitrittsdatum: 18.10.08 Neueste Beiträge
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
Ekansh Khandelwal, geändert vor 11 Jahren.

RE: How can I get the Screen Name of current user?

New Member Beiträge: 15 Beitrittsdatum: 07.03.13 Neueste Beiträge
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).

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


Thanks!


I want to know the user id of current user that is logged in into blog
thumbnail
Rejoice R, geändert vor 9 Jahren.

RE: How can I get the Screen Name of current user?

New Member Beitrag: 1 Beitrittsdatum: 30.05.14 Neueste Beiträge
Just use this "PortalUtil.getUser(httprequest);"
you are done with it.