Forums de discussion

Getting the user object using java

thumbnail
Sam Cleveland, modifié il y a 13 années.

Getting the user object using java

New Member Publications: 2 Date d'inscription: 06/07/10 Publications récentes
I've searched pretty heavily on the message board here and on google for a solution to my problem, but have yet to find any clear answers or documentation that explains how this is done.

I need to get the current user's organization name so that I can query a database to only grab info for them to see. I have tried a couple of different ways to get the user information and managed to get the users name and id number, but when I try to load the user object to call getOrganizations() I get a null pointer exception associated with the UserLocalServiceUtil.getUser(id).

Here is the code I tried:


                Long id = Long.parseLong(usrid);
		User u = null;
		try {
			u = UserLocalServiceUtil.getUser(id);
		} catch (PortalException e1) {
			e1.printStackTrace();
		} catch (SystemException e1) {
			e1.printStackTrace();
		}


usrid is coming from my jsp here:


<% Map userInfo = (Map)renderRequest.getAttribute(PortletRequest.USER_INFO);
   String usrid = (userInfo != null) ? (String)userInfo.get("liferay.user.id") : "";
%>

<%= pd.getData(usrid) %>


Can someone give me a complete example of getting a user and then his organization name?

I am using Liferay 6.0 also with Tomcat if it matters.
thumbnail
Amos Fong, modifié il y a 13 années.

RE: Getting the user object using java (Réponse)

Liferay Legend Publications: 2047 Date d'inscription: 07/10/08 Publications récentes
Hi Sam,

Do you have this in your init.jsp?

<liferay-theme:defineobjects />


if not add it. Then you access the current user simply by using "user" like so:

<%
String curUserFirstName = user.getFirstName();
%>

You can tell if the current user is signed in like so:

<%
if (themeDisplay.isSignedIn()) {
...
}
%>
thumbnail
Sam Cleveland, modifié il y a 13 années.

RE: Getting the user object using java (Réponse)

New Member Publications: 2 Date d'inscription: 06/07/10 Publications récentes
I got it working by doing this in my view.jsp:


&lt;% 
   Map userInfo = (Map)renderRequest.getAttribute(PortletRequest.USER_INFO);
   String userid = (userInfo != null) ? (String)userInfo.get("liferay.user.id") : "";
   User user = UserServiceUtil.getUserById(Long.parseLong(userid));
   List<organization> orgs = user.getOrganizations();
%&gt;

&lt;%= pd.getData(orgs.get(0).getName()) %&gt;
</organization>


I'll try what you suggest though. Seems like the more simple route.

Thanks for the reply.

Sam