From 29cac83598d2cf737be3a9ca3015418b32fcdfc7 Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Tue, 29 Nov 2016 16:27:32 +0000 Subject: [PATCH] Fixed how PortalContext retrieves the token git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/data-miner-manager@135055 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../dataminermanager/server/SessionUtil.java | 147 ++++++++++++------ 1 file changed, 97 insertions(+), 50 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/dataminermanager/server/SessionUtil.java b/src/main/java/org/gcube/portlets/user/dataminermanager/server/SessionUtil.java index 5457fe1..80a3757 100644 --- a/src/main/java/org/gcube/portlets/user/dataminermanager/server/SessionUtil.java +++ b/src/main/java/org/gcube/portlets/user/dataminermanager/server/SessionUtil.java @@ -17,6 +17,9 @@ import org.gcube.portlets.user.dataminermanager.server.dmservice.SClientDirector import org.gcube.portlets.user.dataminermanager.server.util.ServiceCredentials; import org.gcube.portlets.user.dataminermanager.shared.Constants; import org.gcube.portlets.user.dataminermanager.shared.exception.ServiceException; +import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; +import org.gcube.vomanagement.usermanagement.model.GCubeGroup; +import org.gcube.vomanagement.usermanagement.model.GCubeUser; /** * @@ -29,19 +32,20 @@ public class SessionUtil { private static final Logger logger = Logger.getLogger(SessionUtil.class); public static ServiceCredentials getServiceCredentials( - HttpServletRequest httpServletRequest) - throws ServiceException { + HttpServletRequest httpServletRequest) throws ServiceException { return getServiceCredentials(httpServletRequest, null); } - + public static ServiceCredentials getServiceCredentials( HttpServletRequest httpServletRequest, String scopeGroupId) throws ServiceException { - + ServiceCredentials sCredentials = null; String userName = null; String scope = null; String token = null; + String groupId = null; + String groupName = null; if (Constants.DEBUG_MODE) { logger.info("No credential found in session, use test user!"); @@ -54,55 +58,100 @@ public class SessionUtil { } else { logger.info("Retrieving credential in session!"); PortalContext pContext = PortalContext.getConfiguration(); - if(scopeGroupId!=null&&!scopeGroupId.isEmpty()){ + boolean hasScopeGroupId = false; + + if (scopeGroupId != null && !scopeGroupId.isEmpty()) { + hasScopeGroupId = true; + + } else { + hasScopeGroupId = false; + } + + if (hasScopeGroupId) { scope = pContext.getCurrentScope(scopeGroupId); } else { scope = pContext.getCurrentScope(httpServletRequest); - } - - if(scope==null||scope.isEmpty()){ - String error="Error retrieving scope: "+scope; - logger.error(error); - throw new ServiceException(error); - } - - userName = pContext.getCurrentUser(httpServletRequest) - .getUsername(); - - if(userName==null||userName.isEmpty()){ - String error="Error retrieving username in scope "+scope+": "+userName; - logger.error(error); - throw new ServiceException(error); - } - - token = pContext.getCurrentUserToken(httpServletRequest); - - if(token==null||token.isEmpty()){ - String error="Error retrieving token for "+userName+" in "+scope+": "+token; - logger.error(error); - throw new ServiceException(error); - } - - - String name = pContext.getCurrentUser(httpServletRequest) - .getFirstName(); - String lastName = pContext.getCurrentUser(httpServletRequest) - .getLastName(); - String fullName = pContext.getCurrentUser(httpServletRequest) - .getFullname(); - String userAvatarURL = pContext.getCurrentUser(httpServletRequest) - .getUserAvatarURL(); + } - String email = pContext.getCurrentUser(httpServletRequest) - .getEmail(); - String groupId = String.valueOf(pContext - .getCurrentGroupId(httpServletRequest)); - String groupName = pContext.getCurrentGroupName(httpServletRequest); + if (scope == null || scope.isEmpty()) { + String error = "Error retrieving scope: " + scope; + logger.error(error); + throw new ServiceException(error); + } + + GCubeUser gCubeUser = pContext.getCurrentUser(httpServletRequest); + + if (gCubeUser == null) { + String error = "Error retrieving gCubeUser in scope " + scope + + ": " + gCubeUser; + logger.error(error); + throw new ServiceException(error); + } + + userName = gCubeUser.getUsername(); + + if (userName == null || userName.isEmpty()) { + String error = "Error retrieving username in scope " + scope + + ": " + userName; + logger.error(error); + throw new ServiceException(error); + } + + token = pContext.getCurrentUserToken(scope, httpServletRequest); + + if (token == null || token.isEmpty()) { + String error = "Error retrieving token for " + userName + + " in " + scope + ": " + token; + logger.error(error); + throw new ServiceException(error); + } + + String name = gCubeUser.getFirstName(); + String lastName = gCubeUser.getLastName(); + String fullName = gCubeUser.getFullname(); + + String userAvatarURL = gCubeUser.getUserAvatarURL(); + + String email = gCubeUser.getEmail(); + + if (hasScopeGroupId) { + + groupId = scopeGroupId; + + long gId; + + try { + gId = Long.parseLong(scopeGroupId); + } catch (Throwable e) { + String error = "Error retrieving groupId: " + scopeGroupId; + logger.error(error, e); + throw new ServiceException(error); + } + + GCubeGroup group; + try { + group = new LiferayGroupManager().getGroup(gId); + } catch (Throwable e) { + String error = "Error retrieving group: " + groupName; + logger.error(error); + throw new ServiceException(error); + } + + groupName = group.getGroupName(); + + } else { + + groupId = String.valueOf(pContext + .getCurrentGroupId(httpServletRequest)); + + groupName = pContext.getCurrentGroupName(httpServletRequest); + + } sCredentials = new ServiceCredentials(userName, fullName, name, - lastName, email, scope, groupId, groupName, userAvatarURL, token); - + lastName, email, scope, groupId, groupName, userAvatarURL, + token); } logger.info("ServiceCredentials: " + sCredentials); @@ -110,10 +159,8 @@ public class SessionUtil { return sCredentials; } - - - public static SClient getSClient(ServiceCredentials serviceCredentials, HttpSession session) - throws Exception { + public static SClient getSClient(ServiceCredentials serviceCredentials, + HttpSession session) throws Exception { if (serviceCredentials == null) { logger.error("ServiceCredentials is null!");