From c66b170e7f00a6ec3948a4e26cdbaa7c5cb2275c Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Wed, 20 Jul 2016 21:59:07 +0000 Subject: [PATCH] minor improvement git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/gcube-ckan-datacatalog@130612 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../gcubeckandatacatalog/server/UserUtil.java | 174 +++++++----------- 1 file changed, 69 insertions(+), 105 deletions(-) diff --git a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/UserUtil.java b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/UserUtil.java index 21821be..a05874a 100644 --- a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/UserUtil.java +++ b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/UserUtil.java @@ -115,43 +115,16 @@ public class UserUtil { // get the role of the users in this group List roles = roleManager.listRolesByUserAndGroup(userid, groupManager.getGroupId(gCubeGroupName)); - // the default one - RolesIntoOrganization correspondentRoleToCheck = RolesIntoOrganization.MEMBER; - CkanRole ckanRoleMatched = CkanRole.MEMBER; - - // NOTE: it is supposed that there is just one role for this person correspondent to the one in the catalog - for (GCubeRole gCubeRole : roles) { - if(gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesNames.CATALOGUE_ADMIN.getRoleName())){ - logger.debug("User " + username + " has role " + gCubeRole.getRoleName() + " in " + gCubeGroupName); - correspondentRoleToCheck = RolesIntoOrganization.ADMIN; - ckanRoleMatched = CkanRole.ADMIN; - break; - } - } + // get highest role + RolesIntoOrganization correspondentRoleToCheck = getLiferayHighestRoleInOrg(roles); // if the role is member, continue if(correspondentRoleToCheck.equals(RolesIntoOrganization.MEMBER)) continue; - // with this invocation, we check if the role is present in ckan and if it is not it will be added - CKanUtils ckanUtils = gcubeCkanDataCatalogServiceImpl.getCkanUtilsObj(groupManager.getInfrastructureScope(gCubeGroup.getGroupId())); + checkIfRoleIsSetInCkanInstance(username, gCubeGroupName, gCubeGroup.getGroupId(), + correspondentRoleToCheck, groupManager, gcubeCkanDataCatalogServiceImpl, orgsInWhichAdminRole); - // if there is an instance of ckan in this scope.. - if(ckanUtils != null){ - boolean res = ckanUtils.checkRole(username, gCubeGroupName, correspondentRoleToCheck); - if(res){ - - // get the orgs of the user - List ckanOrgs = ckanUtils.getOrganizationsByUser(username); - for (CkanOrganization ckanOrganization : ckanOrgs) { - if(ckanOrganization.getName().equals(gCubeGroupName.toLowerCase())){ - orgsInWhichAdminRole.add(new OrganizationBean(ckanOrganization.getTitle(), ckanOrganization.getName())); - break; - } - } - } - }else - logger.error("It seems there is no ckan instance into scope " + groupManager.getInfrastructureScope(gCubeGroup.getGroupId())); } }else if(groupManager.isVO(currentGroupId)){ @@ -168,43 +141,15 @@ public class UserUtil { List roles = roleManager.listRolesByUserAndGroup(userid, groupManager.getGroupId(gCubeGroupName)); - // the default one - RolesIntoOrganization correspondentRoleToCheck = RolesIntoOrganization.MEMBER; - CkanRole ckanRoleMatched = CkanRole.MEMBER; - - // NOTE: it is supposed that there is just one role for this person correspondent to the one in the catalog - for (GCubeRole gCubeRole : roles) { - - logger.debug("User " + username + " has role " + gCubeRole.getRoleName() + " in " + gCubeGroupName); - if(gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesNames.CATALOGUE_ADMIN.getRoleName())){ - correspondentRoleToCheck = RolesIntoOrganization.ADMIN; - ckanRoleMatched = CkanRole.ADMIN; - break; - } - } + // get highest role + RolesIntoOrganization correspondentRoleToCheck = getLiferayHighestRoleInOrg(roles); // if the role is member, continue if(correspondentRoleToCheck.equals(RolesIntoOrganization.MEMBER)) continue; - // with this invocation, we check if the role is present in ckan and if it is not it will be added - CKanUtils ckanUtils = gcubeCkanDataCatalogServiceImpl.getCkanUtilsObj(groupManager.getInfrastructureScope(gCubeGroup.getGroupId())); - - // if there is an instance of ckan in this scope.. - if(ckanUtils != null){ - boolean res = ckanUtils.checkRole(username, gCubeGroupName, correspondentRoleToCheck); - if(res){ - // get the orgs of the user - List ckanOrgs = ckanUtils.getOrganizationsByUser(username); - for (CkanOrganization ckanOrganization : ckanOrgs) { - if(ckanOrganization.getName().equals(gCubeGroupName.toLowerCase())){ - orgsInWhichAdminRole.add(new OrganizationBean(ckanOrganization.getTitle(), ckanOrganization.getName())); - break; - } - } - } - }else - logger.error("It seems there is no ckan instance into scope " + groupManager.getInfrastructureScope(gCubeGroup.getGroupId())); + checkIfRoleIsSetInCkanInstance(username, gCubeGroupName, gCubeGroup.getGroupId(), + correspondentRoleToCheck, groupManager, gcubeCkanDataCatalogServiceImpl, orgsInWhichAdminRole); } }else if(groupManager.isVRE(currentGroupId)){ @@ -212,65 +157,84 @@ public class UserUtil { logger.debug("The current scope is the vre " + groupName); - // the default one - String mainRole = "Catalogue-Member"; - RolesIntoOrganization correspondentRoleToCheck = RolesIntoOrganization.MEMBER; - - // NOTE: it is supposed that there is just one role for this person correspondent to the one in the catalog - for (GCubeRole role : roles) { - - logger.debug("User " + username + " has role " + role.getRoleName() + " in " + currentScope); - if(role.getRoleName().equalsIgnoreCase(GatewayRolesNames.CATALOGUE_ADMIN.getRoleName())){ - mainRole = GatewayRolesNames.CATALOGUE_ADMIN.getRoleName(); - correspondentRoleToCheck = RolesIntoOrganization.ADMIN; - break; - } - } + // get highest role + RolesIntoOrganization correspondentRoleToCheck = getLiferayHighestRoleInOrg(roles); // if it the role is ADMIN we have to be sure to set it if(correspondentRoleToCheck.equals(RolesIntoOrganization.ADMIN)){ - // with this invocation, we check if the role is present in ckan and if it is not it will be added - CKanUtils ckanUtils = gcubeCkanDataCatalogServiceImpl.getCkanUtilsObj(groupManager.getInfrastructureScope(currentGroupId)); - boolean res = ckanUtils.checkRole(username, groupName, correspondentRoleToCheck); - - if(res){ - // set the role - toReturn = mapLiferayRoleToCkan(mainRole); - - // get the orgs of the user - List ckanOrgs = ckanUtils.getOrganizationsByUser(username); - for (CkanOrganization ckanOrganization : ckanOrgs) { - if(ckanOrganization.getName().equals(groupName.toLowerCase())){ - orgsInWhichAdminRole.add(new OrganizationBean(ckanOrganization.getTitle(), ckanOrganization.getName())); - break; - } - } - } + checkIfRoleIsSetInCkanInstance(username, groupName, currentGroupId, + correspondentRoleToCheck, groupManager, gcubeCkanDataCatalogServiceImpl, orgsInWhichAdminRole); } } }catch(Exception e){ logger.error("Unable to retrieve the role information for this user. Returning member role", e); - toReturn = CkanRole.MEMBER; + return CkanRole.MEMBER; } + // check the list + if(orgsInWhichAdminRole.size() > 0) + toReturn = CkanRole.ADMIN; + // return the role logger.debug("Returning role " + toReturn + " for user " + username); return toReturn; } /** - * Map between roles. - * @param mainRole the main role - * @return the ckan role + * Check if the role admin is set or must be set into the ckan instance at this scope + * @param username + * @param gCubeGroupName + * @param groupId + * @param correspondentRoleToCheck + * @param groupManager + * @param gcubeCkanDataCatalogServiceImpl + * @param orgsInWhichAdminRole + * @throws UserManagementSystemException + * @throws GroupRetrievalFault */ - public static CkanRole mapLiferayRoleToCkan(String mainRole) { - if(GatewayRolesNames.CATALOGUE_ADMIN.getRoleName().equals(mainRole)) - return CkanRole.ADMIN; - else if(GatewayRolesNames.CATALOGUE_EDITOR.getRoleName().equals(mainRole)) - return CkanRole.EDITOR; - else - return CkanRole.MEMBER; + private static void checkIfRoleIsSetInCkanInstance(String username, + String gCubeGroupName, long groupId, + RolesIntoOrganization correspondentRoleToCheck, + GroupManager groupManager, + GcubeCkanDataCatalogServiceImpl gcubeCkanDataCatalogServiceImpl, List orgsInWhichAdminRole) throws UserManagementSystemException, GroupRetrievalFault { + + // with this invocation, we check if the role is present in ckan and if it is not it will be added + CKanUtils ckanUtils = gcubeCkanDataCatalogServiceImpl.getCkanUtilsObj(groupManager.getInfrastructureScope(groupId)); + + // if there is an instance of ckan in this scope.. + if(ckanUtils != null){ + boolean res = ckanUtils.checkRole(username, gCubeGroupName, correspondentRoleToCheck); + if(res){ + + // get the orgs of the user + List ckanOrgs = ckanUtils.getOrganizationsByUser(username); + for (CkanOrganization ckanOrganization : ckanOrgs) { + if(ckanOrganization.getName().equals(gCubeGroupName.toLowerCase())){ + orgsInWhichAdminRole.add(new OrganizationBean(ckanOrganization.getTitle(), ckanOrganization.getName())); + break; + } + } + } + }else + logger.error("It seems there is no ckan instance into scope " + groupManager.getInfrastructureScope(groupId)); + + } + + /** + * Retrieve the ckan roles among a list of liferay roles + * @param roles + * @return + */ + private static RolesIntoOrganization getLiferayHighestRoleInOrg( + List roles) { + // NOTE: it is supposed that there is just one role for this person correspondent to the one in the catalog + for (GCubeRole gCubeRole : roles) { + if(gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesNames.CATALOGUE_ADMIN.getRoleName())){ + return RolesIntoOrganization.ADMIN; + } + } + return RolesIntoOrganization.MEMBER; } /**