Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Ashish Sarin
PortalUtil.getUserId(request) always returns 0
April 26, 2009 1:39 AM
Answer

Ashish Sarin

Rank: New Member

Posts: 14

Join Date: April 19, 2009

Recent Posts

Hi,

I created a portlet using Spring Portlet MVC, Hibernate and DWR. I was trying to get the user id of the logged in user in the DWR bean. But the problem I am facing is that the method PortalUtil.getUserId(HttpServletRequest request) always returns 0, irrespective of whether I am logged in or not. The only configuration I did was to add the portal-kernel.jar and portal-service.jar in the classpath and installed the WAR file containing my portlet using the 'Add Application' option.

Any idea why this could be possibly happening ? Infact once I was debugging the code and at that time also I saw that the userId returned is 0 all the times. Is there any problem because I am accessing the userId from the HttpServletRequest and not from PortletRequest ?

thanks
Ashish
Victor Zorin
RE: PortalUtil.getUserId(request) always returns 0
April 26, 2009 2:22 AM
Answer

Victor Zorin

Rank: Liferay Legend

Posts: 1174

Join Date: April 14, 2008

Recent Posts

First, check if request.getRemoteUser() returns ID (numeric value) of logged in user. If it has null or empty value, your DWR servlet is not on liferay authentication path. And this would explain 0 coming from PortalUtil.
Ashish Sarin
RE: PortalUtil.getUserId(request) always returns 0
April 26, 2009 5:33 AM
Answer

Ashish Sarin

Rank: New Member

Posts: 14

Join Date: April 19, 2009

Recent Posts

Hi Victor,

I checked the return value from getRemoteUser and is coming null. I think this is because I am using a plugin portlet ( as I wanted the development of portlet to be as independent as possible from the Liferay ) and I saw some posts in the forum where its mentioned that in case of Plugin portlets, the servlets that form part of that portlet are outside the authentication path of Liferay. So does this mean that I can't in anyway use DWR in plugin portlets without bypassing Liferay authentication ?

thanks
Ashish
Ashish Sarin
RE: PortalUtil.getUserId(request) always returns 0
April 26, 2009 6:02 AM
Answer

Ashish Sarin

Rank: New Member

Posts: 14

Join Date: April 19, 2009

Recent Posts

To add to my previous question....how easy it is to move from a plugin development model to EXT environment. I think it will be useless to create a portal application without using AJAX.

thanks
Ashish
Victor Zorin
RE: PortalUtil.getUserId(request) always returns 0
April 26, 2009 7:42 PM
Answer

Victor Zorin

Rank: Liferay Legend

Posts: 1174

Join Date: April 14, 2008

Recent Posts

The are plenty of ways to make it happen, and the most rude one is modifying web.xml within liferay (RROT) deployment directory by inserting your servlet definition into it. Try, just to see that it works, do not forget to place your DWR servlet jar into common library path, so liferay will be able to see it.
For my own portlet development I've got quite old legacy devel. environment (from early 3+ versions of LR), it is not using SDK nor EXT, and my servlets and filters can see userIds without problems. At the moment, I can not really tell you why this does not work for you. Try to play with your war configuration files, check your web.xml, you can enforce authentication exactly the same way as liferay does it for itself.
Ashish Sarin
RE: PortalUtil.getUserId(request) always returns 0
April 26, 2009 7:44 PM
Answer

Ashish Sarin

Rank: New Member

Posts: 14

Join Date: April 19, 2009

Recent Posts

Just for people who might find this helpful....

I tried switching to EXT environment.....and i got issues related to class conflicts as i was using Spring 2.0.4 release and Liferay 5.2.2 uses release 2.5.6 of Spring, so there were class loader issues. I also faced problems with multiple ContextLoaderListener.....because my own Spring application uses Context:LoaderListener ( so it finally got merged with the web.xml of the ROOT webapp ) and the web.xml already had PortalContextLoaderListener ( which extends ContextLoaderListener) ....and you can't have two context loader listener.

I was able to get rid of classloader issues by copying all the JARs from Spring 2.5.6 distribution to WEB-INF/lib. Liferay by default only uses Spring.jar but I copied all other jars that come with Spring distribution and removed the Spring.jar file. I didnt try solving the issue related to mutiple context loader listeners as I was not comfortable with playing around with the spring jar file in the WEB-INF/lib directory.

I moved back to the plugin portlet development, which was my very first approach. I found a better way to deal with the problem of not being able to access the user id in the DWR spring bean by getting the user id in the JSP and passing it as a parameter to the method in the DWR spring bean. This seems to work properly because the portlet are in the authentication path of the Liferay portal server. I guess this will help ensure checking permissions in the DWR spring beans before allowing users to perform any action, though I haven't tried it yet.

I had been looking for examples where the Spring MVC portlet is shown to work with Hibernate ( connecting to some other database ) and DWR but I didnt find any concrete examples. So, once I am done with the base architecture, I will try to upload the plugin to the Community Plugins section.

Let me know if anyone would like to discuss any of these points.

thanks
Ashish
Ashish Sarin
RE: PortalUtil.getUserId(request) always returns 0
April 26, 2009 10:15 PM
Answer

Ashish Sarin

Rank: New Member

Posts: 14

Join Date: April 19, 2009

Recent Posts

Hi Victor,

Thanks for the reply. I did try using EXT development environment but I started hitting issues with ClassLoaders and Context Loader Listeners.....and so I dropped the idea of going the EXT environment route. I also thought about modifying the web.xml to include DWR servlet but then i felt it was a very crude way to make plugin portlets work.
I think using JSPs to get user ids (using PortalUtils) and then passing the user id as parameter to DWR method will help in achieving the same functionality....with some extra work, but it looks more cleaner approach to development.

Any thoughts on this approach ?

thanks
Ashish
Victor Zorin
RE: PortalUtil.getUserId(request) always returns 0
April 26, 2009 10:33 PM
Answer

Victor Zorin

Rank: Liferay Legend

Posts: 1174

Join Date: April 14, 2008

Recent Posts

That means your servlet becomes dependent on JSP, which I do not think is right. [And every jsp will have to do it].

One way, you can create own PostLoginEventAction and declare it in your portal-ext file. In this action class, fetch user id
and place it into http session. Then, your dwr servlet will be taking it from the request.getSession().getAttribute("..").
This setting will be happening only once, upon user login, so performance will not be affected at all.

I am using this approach for all non-liferay user-specific settings.
Sanya Presich
RE: PortalUtil.getUserId(request) always returns 0
April 28, 2009 7:48 AM
Answer
Omar Rosales .
RE: PortalUtil.getUserId(request) always returns 0
May 11, 2009 3:50 PM
Answer

Omar Rosales .

Rank: New Member

Posts: 16

Join Date: March 18, 2009

Recent Posts

Hi Victor I used the method request.getRemoteUser() and obtain the number but how obtain the login or email from the user loggued?

Thank so much
Victor Zorin
RE: PortalUtil.getUserId(request) always returns 0
May 11, 2009 7:31 PM
Answer

Victor Zorin

Rank: Liferay Legend

Posts: 1174

Join Date: April 14, 2008

Recent Posts

Omar, class com.liferay.portal.util.PortalUtil is going to be your best friend here.
Have a look at its methods, e.g. to get email, use com.liferay.portal.util.PortalUtil.getUser(req).getEmailAddress()