Foros de discusión

How can I get the Screen Name of current user?

Adam Causey, modificado hace 16 años.

How can I get the Screen Name of current user?

Junior Member Mensajes: 70 Fecha de incorporación: 13/06/07 Mensajes recientes
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, modificado hace 16 años.

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

Regular Member Mensajes: 220 Fecha de incorporación: 7/02/07 Mensajes recientes
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, modificado hace 16 años.

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

Regular Member Mensajes: 110 Fecha de incorporación: 3/07/06 Mensajes recientes
But, is class User JSR-168 compliant?
thumbnail
Rich Sezov, modificado hace 16 años.

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

Regular Member Mensajes: 220 Fecha de incorporación: 7/02/07 Mensajes recientes
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, modificado hace 15 años.

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

New Member Mensajes: 4 Fecha de incorporación: 17/04/08 Mensajes recientes
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, modificado hace 15 años.

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

Expert Mensajes: 462 Fecha de incorporación: 17/09/08 Mensajes recientes
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, modificado hace 15 años.

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

New Member Mensajes: 4 Fecha de incorporación: 17/04/08 Mensajes recientes
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, modificado hace 15 años.

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

Expert Mensajes: 462 Fecha de incorporación: 17/09/08 Mensajes recientes
awesome... that works like a charm
Ekansh Khandelwal, modificado hace 11 años.

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

New Member Mensajes: 15 Fecha de incorporación: 7/03/13 Mensajes recientes
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é, modificado hace 15 años.

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

Liferay Legend Mensajes: 1197 Fecha de incorporación: 8/02/05 Mensajes recientes
Have you considered using the spec provided USER_INFO?

See [HOWTO] Personalization - Getting current user attributes .
thumbnail
Chris Whittle, modificado hace 15 años.

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

Expert Mensajes: 462 Fecha de incorporación: 17/09/08 Mensajes recientes
ooooo! even cooler... thanks Ray!
thumbnail
kanakaraj cheram, modificado hace 15 años.

How can I get the Screen Name of current user?

New Member Mensajes: 20 Fecha de incorporación: 8/04/08 Mensajes recientes
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, modificado hace 14 años.

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

New Member Mensajes: 15 Fecha de incorporación: 18/10/08 Mensajes recientes
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, modificado hace 15 años.

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

Expert Mensajes: 462 Fecha de incorporación: 17/09/08 Mensajes recientes
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, modificado hace 15 años.

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

Expert Mensajes: 462 Fecha de incorporación: 17/09/08 Mensajes recientes
nevermind it we nickname
Ekansh Khandelwal, modificado hace 11 años.

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

New Member Mensajes: 15 Fecha de incorporación: 7/03/13 Mensajes recientes
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, modificado hace 14 años.

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

New Member Mensajes: 15 Fecha de incorporación: 18/10/08 Mensajes recientes
Hi this link is not working now. Could you update this link. It looks like it has benifited a lot of guys
Gildas Bescond, modificado hace 15 años.

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

New Member Mensajes: 7 Fecha de incorporación: 17/11/08 Mensajes recientes
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, modificado hace 14 años.

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

New Member Mensajes: 15 Fecha de incorporación: 18/10/08 Mensajes recientes
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, modificado hace 11 años.

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

New Member Mensajes: 15 Fecha de incorporación: 7/03/13 Mensajes recientes
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, modificado hace 9 años.

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

New Member Mensaje: 1 Fecha de incorporación: 30/05/14 Mensajes recientes
Just use this "PortalUtil.getUser(httprequest);"
you are done with it.