2

my goal is to recover the user after authentication

I can do this retreive through a jsp page

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<liferay-theme:defineObjects />
<portlet:defineObjects />

<liferay-ui:user-display userId="<%= user.getUserId() %>" />

but I want to do the same thing but through java code

I show you why I want to retrieve the logged in user after authentication through liferay

my Java EE application is developed with struts2 and contains two profiles (or roles) : Consultant and Director

and in this application I do not create jsp page for authentication because I use liferay authentication (the concept of SSO)

and I work with the user table that is generated automatically by liferay

So in my Java EE application I haveto retrieve the connected user and I have to test its role: if it is Director so I have only seen the director jsp pages and in the same way for the consultant role

So I must do this retreive in the java code : the user logged on without any action in my portlet deployed

the user object which is declared in my Java class must be filled across the page

login.jsp of liferay (this jsp page is for authentication)

So the final test senario:

I have two users in my database:

Name: Franco, email: franco@test.org, login: Franco, Role: Director Name: adam Email: f@test.org, login: Adamm, Role: Consultant

and after deployment of my Java EE apllication in liferay

if I made the authentication with franco@test.org and for login: franco

I need to find in the application deployed only the jsp pages for the Director

to make this senarion I do in a java class of my apllication

import com.liferay.portal.model.User;
public class extends testAction ActionSupport {


      private User user;
public void processAction (
ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {
try {
     user = PortalUtil.getUser (actionRequest);

} Catch (e PortalException) {
    / / TODO Auto-generated catch block
    e.printStackTrace ();
} Catch (SystemException e) {
    / / TODO Auto-generated catch block
    e.printStackTrace ();
}
}



public String initial() throws Exception {

user = PortalUtil.getUser (actionRequest);
if (user.getRole (). equals ("director"))
return "AuthDirector";

return "AuthoConsultant";
    }


}

for the file struts.xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC
    "- / / Apache Software Foundation / / DTD Struts Configuration 2.0 / / EN"
    "Http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="view" extends="struts-portlet-default" namespace="/view">

<action name="initial class="com.test.action.testAction" method="">

<result name="AuthDirector"> / WEB-INF/view/HomeDirector.jsp </ result>

<result name="AuthoConsultantr"> / WEB-INF/view/HomeConsultant.jsp </ result>
</ Action>

</ Package>
</ Struts>

and I do another test without success

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import com.liferay.portal.model.User;
public class extends testAction ActionSupport {


  private User user;

FacesContext fc = FacesContext.getCurrentInstance();
         ExternalContext externalContext = fc.getExternalContext();
         Long id = Long.parseLong(externalContext.getUserPrincipal().getName());

public String initial() throws Exception {

user = UserLocalServiceUtil.getUserById(id);
if (user.getRole (). equals ("director"))
return "AuthDirector";

return "AuthoConsultant";
    }


}
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
franco
  • 1,829
  • 6
  • 42
  • 75
  • maybe duplicate with this question http://stackoverflow.com/questions/970986/accessing-the-user-from-a-liferay-portlet – Isa Hekmat Jun 28 '12 at 17:02

0 Answers0