added method PortalContext.setUserInSesssion for allowing custom servlet to read the current user, removed portal delegate

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/portal-manager@141609 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2017-01-17 16:24:44 +00:00
parent 4adfa6b890
commit ef73230461
1 changed files with 13 additions and 36 deletions

View File

@ -9,9 +9,9 @@ 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 javax.servlet.http.HttpSession;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
@ -28,8 +28,6 @@ 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;
@ -143,13 +141,12 @@ public class PortalContext {
/**
* <p>
* Please note that this method works with AJAX calls only (i.e. XMLHttpRequest to exchange data with a server behind the scenes).
* If you use standard http servlet GET or POST to exchange data with a server, you must use Liferay's PortalDelegateServlet feature {@link com.liferay.portal.kernel.servlet.PortalDelegateServlet}.
* If you use standard http servlet GET or POST to exchange data with a server, you must call the methos PortalContext#setUserInSession in your portlet doView method.
*
* For documentation on PortalDelegateServlet @see <a href="https://wiki.gcube-system.org/gcube/Portal_Context#Liferay.27s_PortalDelegateServlet_.28Custom_servlets_running_in_the_ROOT_context.29">gCube WIKI PortalDelegateServlet page</a>
* </p>
*
* @param httpServletRequest the {@link HttpServletRequest} object
* @return the current user, or <code>null</code> if a current user could not be found
* @return the current user, or <code>null</code> if a current user could not be found or session expired
* @see {@link GCubeUser}
*/
public GCubeUser getCurrentUser(HttpServletRequest httpServletRequest) {
@ -172,42 +169,18 @@ public class PortalContext {
return toReturn;
} else { //must be a custom servlet
try {
userId = (long) httpServletRequest.getSession().getAttribute(USER_ID_ATTR_NAME);
HttpSession session = httpServletRequest.getSession(false);
userId = (long) session.getAttribute(USER_ID_ATTR_NAME);
_log.debug("read the current userid from the http session userId: " + userId + " sessionid="+session.getId());
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;
}
/**
* <p>
* Please note that this method works with Liferay's PortalDelegateServlet feature {@link com.liferay.portal.kernel.servlet.PortalDelegateServlet}.
* For documentation on PortalDelegateServlet @see <a href="https://wiki.gcube-system.org/gcube/Portal_Context#Liferay.27s_PortalDelegateServlet_.28Custom_servlets_running_in_the_ROOT_context.29">gCube WIKI PortalDelegateServlet page</a>
* </p>
*
* @param httpServletRequest the {@link HttpServletRequest} object
* @return the current user, or <code>null</code> 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;
}
/**
* <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.
@ -217,8 +190,12 @@ public class PortalContext {
* @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);
long userId = Long.parseLong(request.getRemoteUser());
HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(request);
HttpSession session = httpServletRequest.getSession();
_log.debug("HttpSession#setUserInSession, set userId: " + userId + " sessionid="+session.getId());
session.setAttribute(USER_ID_ATTR_NAME, userId);
}
/**
* @param scopeGroupId the liferay groupid number (as String) of the VRE/VO