How to access the Liferay User Details inside the Vaadin Source Code?

Hi,

I am developping Vaadin Portlet using Liferay5.2.3.



I have one requirement like how to access the Liferay User Details inside the Vaadin Portlet Source Code? Is this possible?

If somebody knows , please help me regarding this.

Thanks & Regards,
Sanjay

You have to use some Liferay specific APIs to do this.

From the portlet request, you can obtain the liferay user id and company id. These are integers that can then be used as keys when calling e.g. com.liferay.portal.service.UserLocalServiceUtil methods.

Something like (completely untested):


PortletRequest request = ...;
Map uinfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
int userId = Integer.valueOf(uinfo.get("liferay.user.id").toString());
int companyId = Integer.valueOf(uinfo.get("liferay.company.id").toString());
User liferayUser = UserLocalServiceUtil.getUserById(info.liferayUserId);
String firstName = liferayUser.getFirstName();
...

You can access the portlet request in a PortletRequestListener or a PortletListener and use the ThreadLocal pattern to store any user details you need.

Hi Henry,

Thanks for quick response.

I am trying to get the PortletSession/PortletRequest in my Vaadin Application.
While running the application it says "

Not running in Portal

".

I am giving the following code.

public void init() {

    bundle = getSupportedResourceBundle(getLocale());
    //Spring configuration for invoking WebServices
    initSpring();

    rootWindow = new Window("Hello Vaadin");
    //rootWindow.setStyleName(styleNameMainWindow);
    setMainWindow(rootWindow);

    rootWindow.addComponent(loadTaskTable());

    //R&D

    try{
    if(getContext() instanceof PortletApplicationContext2){

        PortletApplicationContext2 ctx = (PortletApplicationContext2)getContext();
        ctx.addPortletListener(this, this);
        PortletSession session = (PortletSession)ctx.getPortletSession();
        Map uinfo = (Map)session.getAttribute(PortletRequest.USER_INFO);
        int userId = Integer.valueOf(uinfo.get("liferay.user.id").toString());
        int companyId = Integer.valueOf(uinfo.get("liferay.company.id").toString());
        User liferayUser = UserLocalServiceUtil.getUserById(userId);
        String firstName = liferayUser.getFirstName();
        System.out.println("Init method === firstName : "+firstName);
        String mailid = liferayUser.getEmailAddress();
        System.out.println("Init method === mailid : "+mailid);
        String uUid = liferayUser.getUuid();
        System.out.println("Init method === uUid : "+uUid);
        //String userId = liferayUser.getUserId();
    }else{
        rootWindow.showNotification("[color=#FF0000]

Not running in Portal
[/color]", Notification.TYPE_ERROR_MESSAGE);

    }
    }catch(Exception e){
        e.printStackTrace();
    }
}

Here I am not able to get PortletRequest/PortletSession from my Vaadin Application.


Could you please send me some piece of code syntax regarding how to get the PortletRequest from Vaadin Application?
.

Thanks & Regards,
Sanjay

You cannot obtain the request inside your init method directly.

If you need the user information everywhere including in Application.init(), the simplest alternative might be to simply have your application implement PortletRequestListener, which can extract the user information and save it in the application instance. Registering the listener is not necessary, the methods are called automatically if your application implements this interface.

The class Application has an unused property “user” of type Object that can be used for storing the user information you need. The onRequestStart(PortletRequest, PortletResponse) method should extract the user id from the request, ask Liferay for any necessary information, construct your own user object and call Application.setUser(Object) with that user object. Likewise, onRequestEnd(PortletRequest, PortletResponse) should call Application.setUser(null).

In your init method and elsewhere when you need user information, you should then simply call Application.getUser() and cast it to the correct type (preferably with a wrapper method in your application).

Unfortunately I did not have time to write and test complete code this time either.

A clean easy that I have implemented is the following

import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;

public class PortletApplication extends Application implements PortletListener {

@Override
public void handleRenderRequest(RenderRequest request, RenderResponse response) {
    
    User user = null;

    try {
        user = PortalUtil.getUser(request);
    } catch (PortalException ex) {
        // error handling
    } catch (SystemException ex) {
        // error handling
    }
    
    setUser(user);  // the Vaadin Application USER to liferay's user. Gives your application easy access

    // rest of your code
}

//  rest of your application goes here

}

Hi Henri,

I implemented ‘PortletRequestListener’ however the methods are not executed.

there is also something strange with my portlet, if I execute “getContext()” it return WepApplicationContext instead of “PortletApplicationContext2”.

I’m using vaadin-6.3.0

Portlets conforming to the JSR-168 specification (Portlet 1.0) use a servlet and a portlet (ApplicationPortlet) on top of that. For them, the context is normally a PortletApplicationContext (a subclass of WebApplicationContext) and only PortletApplicationContext.PortletListener is supported. However, the application can implement HttpServletRequestListener if necessary.

Portlets conforming to the JSR-286 specification (Portlet 2.0) are not based on a (developer-visible) servlet but only have a portlet class, typically ApplicationPortlet2. The context is PortletApplicationContext2, and both PortletApplicationContext2.PortletListener and PortletRequestListener are supported.

To use Portlet 2.0, you can remove (even completely if it is not needed for other purposes such as in testing) web.xml and define your portlet in portlet.xml as
described in the book
. Note that both the portlet-class and the application parameters change. The book chapter also describes more on the limitations of portlet 2.0 etc.

Hello,

Sorry but I do not understand if it is possible with JSR168 or only with JSR286 ?

Best regards,

Most of this should be possible with either one - you just need to implement the correct interface for each case (PortletApplicationContext.PortletListener vs. PortletApplicationContext2.PortletListener) and note that the portlet context class is different for JSR-168 and JSR-286.

This way, the user can be obtained when processing a render request (full page reload), but the application might not clear the user field when logging out from the portal. A user with access to the browser (e.g. on a public computer where session data was not cleared) might then be able to continue to use the Vaadin application even after portal logout (with the same privileges as the original user) by connecting directly to its servlet instead of going through the portal. Depending on the application and its use, this might or might not be a problem, and can often be alleviated with other security measures.

The lower level interface PortletRequestListener (called for any request, not only when reloading the page) is only available for JSR-286. With a correctly configured JSR-286 portlet, it is not possible to bypass the portal when making requests to the portlet.

Thank for your answer. I will try to do it and fill up the wiki after.

Best regards,

@Sanjay Kumar Das

I have find the any solution. I am also facin the same issue.I am also unale to get the current logged in user details in my portlet.I have created servlet in my portlet and in myservlet how to access the current loggedin username/userid.If it is possible please let me know.

Best Regards,
Ramesh