From aa60ad1c38ea3501591e2350b9d95637865f95f1 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Thu, 12 Jan 2017 10:52:20 +0000 Subject: [PATCH] added method for getting the user form PortalDelegateServlets git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/portal-manager@141523 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../gcube/common/portal/PortalContext.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/gcube/common/portal/PortalContext.java b/src/main/java/org/gcube/common/portal/PortalContext.java index 1a7e9c7..f897d67 100644 --- a/src/main/java/org/gcube/common/portal/PortalContext.java +++ b/src/main/java/org/gcube/common/portal/PortalContext.java @@ -26,6 +26,8 @@ import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.liferay.portal.kernel.exception.PortalException; +import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.model.Group; import com.liferay.portal.model.VirtualHost; import com.liferay.portal.service.CompanyLocalServiceUtil; @@ -82,7 +84,7 @@ public class PortalContext { private static PortalContext singleton = new PortalContext(); private UserManager userManager; - + private String infra; private String vos; @@ -169,6 +171,33 @@ public class PortalContext { } return null; } + /** + *

+ * Please note that this method works with Liferay's PortalDelegateServlet feature {@link com.liferay.portal.kernel.servlet.PortalDelegateServlet}. + * For documentation on PortalDelegateServlet @see gCube WIKI PortalDelegateServlet page + *

+ * + * @param httpServletRequest the {@link HttpServletRequest} object + * @return the current user, or null if a current user could not be found + * @see {@link GCubeUser} + */ + public GCubeUser getCurrentUserPortalDelegateServlet(HttpServletRequest httpServletRequest) { + try { + if (PortalUtil.getUser(httpServletRequest) != null) { + long userId = PortalUtil.getUser(httpServletRequest).getUserId(); + return userManager.getUserById(userId); + } + else { + //the user is not logged in or session expired + _log.debug("Can't find the user, session expired or your not using "); + return null; + } + } catch (PortalException | SystemException | UserManagementSystemException | UserRetrievalFault e) { + _log.error("Could not read the current userid, either session expired or user not logged in, exception: " + e.getMessage()); + e.printStackTrace(); + } + return null; + } /** * @param scopeGroupId the liferay groupid number (as String) of the VRE/VO * @return the scope (context)