added PContext setUserInsession method for custom servlets
git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/portal-manager@141606 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
aa60ad1c38
commit
4adfa6b890
|
@ -9,6 +9,8 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.portlet.PortletSession;
|
||||||
|
import javax.portlet.RenderRequest;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||||
|
@ -59,7 +61,6 @@ public class PortalContext {
|
||||||
protected static final String SCOPE_SEPARATOR = "/";
|
protected static final String SCOPE_SEPARATOR = "/";
|
||||||
|
|
||||||
private final static String DEFAULT_ROLE = "OrganizationMember";
|
private final static String DEFAULT_ROLE = "OrganizationMember";
|
||||||
|
|
||||||
private static final String CONFIGURATION_FOLDER = "conf";
|
private static final String CONFIGURATION_FOLDER = "conf";
|
||||||
private static final String INFRA_PROPERTY_FILENAME = "infrastructure.properties";
|
private static final String INFRA_PROPERTY_FILENAME = "infrastructure.properties";
|
||||||
private static final String GCUBE_DEV__CONTEXT_PROPERTY_FILENAME = "gcube-dev-context.properties";
|
private static final String GCUBE_DEV__CONTEXT_PROPERTY_FILENAME = "gcube-dev-context.properties";
|
||||||
|
@ -118,6 +119,7 @@ public class PortalContext {
|
||||||
catch(IOException e) {
|
catch(IOException e) {
|
||||||
infra = DEFAULT_INFRA_NAME;
|
infra = DEFAULT_INFRA_NAME;
|
||||||
vos = DEFAULT_VO_NAME;
|
vos = DEFAULT_VO_NAME;
|
||||||
|
userManager = new LiferayUserManager();
|
||||||
_log.error("infrastructure.properties file not found under $CATALINA_HOME/conf/ dir, setting default infrastructure Name " + infra + " and VO Name " + vos);
|
_log.error("infrastructure.properties file not found under $CATALINA_HOME/conf/ dir, setting default infrastructure Name " + infra + " and VO Name " + vos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,21 +154,29 @@ public class PortalContext {
|
||||||
*/
|
*/
|
||||||
public GCubeUser getCurrentUser(HttpServletRequest httpServletRequest) {
|
public GCubeUser getCurrentUser(HttpServletRequest httpServletRequest) {
|
||||||
String userIdNo = httpServletRequest.getHeader(USER_ID_ATTR_NAME);
|
String userIdNo = httpServletRequest.getHeader(USER_ID_ATTR_NAME);
|
||||||
|
long userId = -1;
|
||||||
if (userIdNo != null) {
|
if (userIdNo != null) {
|
||||||
long userId = -1;
|
|
||||||
try {
|
try {
|
||||||
|
_log.debug("The userIdNo is " + userIdNo);
|
||||||
userId = Long.parseLong(userIdNo);
|
userId = Long.parseLong(userIdNo);
|
||||||
return userManager.getUserById(userId);
|
return userManager.getUserById(userId);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
_log.error("The userId is not a number -> " + userId);
|
_log.error("The userId is not a number -> " + userId);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
_log.error("Could not read the current userid, either session expired or user not logged in, exception: " + e.getMessage());
|
_log.error("Could not read the current userid from header param, either session expired or user not logged in, exception: " + e.getMessage());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!isWithinPortal()) {
|
if (!isWithinPortal()) {
|
||||||
GCubeUser toReturn = readUserFromPropertyFile();
|
GCubeUser toReturn = readUserFromPropertyFile();
|
||||||
_log.debug("getCurrentUser devMode into IDE detected, returning testing user: " + toReturn.toString());
|
_log.debug("getCurrentUser devMode into IDE detected, returning testing user: " + toReturn.toString());
|
||||||
return toReturn;
|
return toReturn;
|
||||||
|
} else { //must be a custom servlet
|
||||||
|
try {
|
||||||
|
userId = (long) httpServletRequest.getSession().getAttribute(USER_ID_ATTR_NAME);
|
||||||
|
return userManager.getUserById(userId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
_log.error("Could not read the current userid from the http session, either session expired or user not logged in, exception: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -198,6 +208,18 @@ public class PortalContext {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* This method sets the current userid in the session so that it is possible to get the current user in custom servlets of the same WAR.
|
||||||
|
* Must be added in Portlet's doView() method before dispatching the request.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param request the portlet {@link RenderRequest} object
|
||||||
|
*/
|
||||||
|
public static void setUserInSession(RenderRequest request) {
|
||||||
|
long userid = Long.parseLong(request.getRemoteUser());
|
||||||
|
request.getPortletSession().setAttribute(USER_ID_ATTR_NAME, userid, PortletSession.APPLICATION_SCOPE);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @param scopeGroupId the liferay groupid number (as String) of the VRE/VO
|
* @param scopeGroupId the liferay groupid number (as String) of the VRE/VO
|
||||||
* @return the scope (context)
|
* @return the scope (context)
|
||||||
|
|
Loading…
Reference in New Issue