From 4adfa6b89009b07655836eae741c0dd2ed849f85 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Tue, 17 Jan 2017 12:59:37 +0000 Subject: [PATCH] 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 --- .../gcube/common/portal/PortalContext.java | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gcube/common/portal/PortalContext.java b/src/main/java/org/gcube/common/portal/PortalContext.java index f897d67..96892ab 100644 --- a/src/main/java/org/gcube/common/portal/PortalContext.java +++ b/src/main/java/org/gcube/common/portal/PortalContext.java @@ -9,6 +9,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; +import javax.portlet.PortletSession; +import javax.portlet.RenderRequest; import javax.servlet.http.HttpServletRequest; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; @@ -59,7 +61,6 @@ public class PortalContext { protected static final String SCOPE_SEPARATOR = "/"; private final static String DEFAULT_ROLE = "OrganizationMember"; - private static final String CONFIGURATION_FOLDER = "conf"; private static final String INFRA_PROPERTY_FILENAME = "infrastructure.properties"; private static final String GCUBE_DEV__CONTEXT_PROPERTY_FILENAME = "gcube-dev-context.properties"; @@ -118,6 +119,7 @@ public class PortalContext { catch(IOException e) { infra = DEFAULT_INFRA_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); } @@ -152,21 +154,29 @@ public class PortalContext { */ public GCubeUser getCurrentUser(HttpServletRequest httpServletRequest) { String userIdNo = httpServletRequest.getHeader(USER_ID_ATTR_NAME); + long userId = -1; if (userIdNo != null) { - long userId = -1; try { + _log.debug("The userIdNo is " + userIdNo); userId = Long.parseLong(userIdNo); return userManager.getUserById(userId); } catch (NumberFormatException e) { _log.error("The userId is not a number -> " + userId); } 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 { if (!isWithinPortal()) { GCubeUser toReturn = readUserFromPropertyFile(); _log.debug("getCurrentUser devMode into IDE detected, returning testing user: " + toReturn.toString()); 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; @@ -198,6 +208,18 @@ public class PortalContext { } return null; } + /** + *

+ * 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. + *

+ * + * @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 * @return the scope (context)