掲示板

How can I get the Screen Name of current user?

16年前 に Adam Causey によって更新されました。

How can I get the Screen Name of current user?

Junior Member 投稿: 70 参加年月日: 07/06/13 最新の投稿
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
16年前 に Rich Sezov によって更新されました。

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

Regular Member 投稿: 220 参加年月日: 07/02/07 最新の投稿
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));
16年前 に Luis Colorado によって更新されました。

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

Regular Member 投稿: 110 参加年月日: 06/07/03 最新の投稿
But, is class User JSR-168 compliant?
thumbnail
16年前 に Rich Sezov によって更新されました。

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

Regular Member 投稿: 220 参加年月日: 07/02/07 最新の投稿
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
15年前 に Paul R Saxman によって更新されました。

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

New Member 投稿: 4 参加年月日: 08/04/17 最新の投稿
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
15年前 に Chris Whittle によって更新されました。

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

Expert 投稿: 462 参加年月日: 08/09/17 最新の投稿
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
15年前 に Paul R Saxman によって更新されました。

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

New Member 投稿: 4 参加年月日: 08/04/17 最新の投稿
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
15年前 に Chris Whittle によって更新されました。

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

Expert 投稿: 462 参加年月日: 08/09/17 最新の投稿
awesome... that works like a charm
11年前 に Ekansh Khandelwal によって更新されました。

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

New Member 投稿: 15 参加年月日: 13/03/07 最新の投稿
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
15年前 に Ray Augé によって更新されました。

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

Liferay Legend 投稿: 1197 参加年月日: 05/02/08 最新の投稿
Have you considered using the spec provided USER_INFO?

See [HOWTO] Personalization - Getting current user attributes .
thumbnail
15年前 に Chris Whittle によって更新されました。

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

Expert 投稿: 462 参加年月日: 08/09/17 最新の投稿
ooooo! even cooler... thanks Ray!
thumbnail
15年前 に kanakaraj cheram によって更新されました。

How can I get the Screen Name of current user?

New Member 投稿: 20 参加年月日: 08/04/08 最新の投稿
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
14年前 に sancho sebastine によって更新されました。

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

New Member 投稿: 15 参加年月日: 08/10/18 最新の投稿
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
15年前 に Chris Whittle によって更新されました。

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

Expert 投稿: 462 参加年月日: 08/09/17 最新の投稿
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
15年前 に Chris Whittle によって更新されました。

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

Expert 投稿: 462 参加年月日: 08/09/17 最新の投稿
nevermind it we nickname
11年前 に Ekansh Khandelwal によって更新されました。

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

New Member 投稿: 15 参加年月日: 13/03/07 最新の投稿
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...
14年前 に sancho sebastine によって更新されました。

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

New Member 投稿: 15 参加年月日: 08/10/18 最新の投稿
Hi this link is not working now. Could you update this link. It looks like it has benifited a lot of guys
15年前 に Gildas Bescond によって更新されました。

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

New Member 投稿: 7 参加年月日: 08/11/17 最新の投稿
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;
	}
14年前 に sancho sebastine によって更新されました。

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

New Member 投稿: 15 参加年月日: 08/10/18 最新の投稿
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
11年前 に Ekansh Khandelwal によって更新されました。

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

New Member 投稿: 15 参加年月日: 13/03/07 最新の投稿
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
9年前 に Rejoice R によって更新されました。

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

New Member 投稿: 1 参加年月日: 14/05/30 最新の投稿
Just use this "PortalUtil.getUser(httprequest);"
you are done with it.