From e09671daae13ca47a5e51330c0a8ad432dec72cc Mon Sep 17 00:00:00 2001 From: "giancarlo.panichi" Date: Tue, 29 Nov 2016 15:28:57 +0000 Subject: [PATCH] Fixed PortalContext for get token git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/admin/accounting-manager@135047 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../accountingmanager/server/SessionUtil.java | 84 +++++++++++++++---- .../server/export/CSVManager.java | 3 +- 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/SessionUtil.java b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/SessionUtil.java index 0e647cf..2ce0ecf 100644 --- a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/SessionUtil.java +++ b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/SessionUtil.java @@ -20,6 +20,7 @@ import org.gcube.portlets.admin.accountingmanager.shared.exception.ServiceExcept import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.model.GCubeGroup; +import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,8 +35,7 @@ public class SessionUtil { private static Logger logger = LoggerFactory.getLogger(SessionUtil.class); public static ServiceCredentials getServiceCredentials( - HttpServletRequest httpServletRequest) - throws ServiceException { + HttpServletRequest httpServletRequest) throws ServiceException { return getServiceCredentials(httpServletRequest, null); } @@ -47,6 +47,8 @@ public class SessionUtil { 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!"); @@ -59,10 +61,20 @@ public class SessionUtil { } else { logger.info("Retrieving credential in session!"); PortalContext pContext = PortalContext.getConfiguration(); + 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()) { @@ -71,8 +83,16 @@ public class SessionUtil { throw new ServiceException(error); } - userName = pContext.getCurrentUser(httpServletRequest) - .getUsername(); + 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 @@ -81,7 +101,7 @@ public class SessionUtil { throw new ServiceException(error); } - token = pContext.getCurrentUserToken(httpServletRequest); + token = pContext.getCurrentUserToken(scope, httpServletRequest); if (token == null || token.isEmpty()) { String error = "Error retrieving token for " + userName @@ -90,22 +110,55 @@ public class SessionUtil { throw new ServiceException(error); } - String name = pContext.getCurrentUser(httpServletRequest) + String name = gCubeUser .getFirstName(); - String lastName = pContext.getCurrentUser(httpServletRequest) + String lastName = gCubeUser .getLastName(); - String fullName = pContext.getCurrentUser(httpServletRequest) + String fullName = gCubeUser .getFullname(); - String userAvatarURL = pContext.getCurrentUser(httpServletRequest) + String userAvatarURL = gCubeUser .getUserAvatarURL(); - String email = pContext.getCurrentUser(httpServletRequest) + String email = gCubeUser .getEmail(); - String groupId = String.valueOf(pContext - .getCurrentGroupId(httpServletRequest)); - String groupName = pContext.getCurrentGroupName(httpServletRequest); + + + 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); @@ -154,8 +207,9 @@ public class SessionUtil { if (Constants.DEBUG_MODE) { contexts.add(serviceCredentials.getScope()); - for(int i=0; i<50 ; i++){ - contexts.add("/d4science.research-infrastructures.eu/gCubeApps/PerformanceEvaluationInAquaculture"+i); + for (int i = 0; i < 50; i++) { + contexts.add("/d4science.research-infrastructures.eu/gCubeApps/PerformanceEvaluationInAquaculture" + + i); } } else { contexts.add(serviceCredentials.getScope()); diff --git a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/export/CSVManager.java b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/export/CSVManager.java index d0a7504..687cd21 100644 --- a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/export/CSVManager.java +++ b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/export/CSVManager.java @@ -186,11 +186,12 @@ public class CSVManager { logger.debug("Temp File: " + tempFile.toString()); // Create the CSVFormat object - CSVFormat format = CSVFormat.RFC4180.withHeader() + CSVFormat format = CSVFormat.RFC4180.withFirstRecordAsHeader() .withDelimiter(','); PrintStream printStream = new PrintStream(tempFile.toFile()); // CSV Write Example using CSVPrinter + CSVPrinter printer = new CSVPrinter(printStream, format); printer.printRecord(accountingDataModel.getHeader()); for (AccountingDataRow row : accountingDataModel.getRows()) {