diff --git a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/Utils.java b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/Utils.java index 8c86fdd..df72d39 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/Utils.java +++ b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/Utils.java @@ -31,6 +31,8 @@ import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWra import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.RoleManager; import org.gcube.vomanagement.usermanagement.UserManager; +import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; +import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; @@ -166,40 +168,14 @@ public class Utils { // get the role of the users in this vre List roles = roleManager.listRolesByUserAndGroup(userid, groupManager.getGroupId(gCubeGroupName)); - // the default one - 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 gCubeRole : roles) { - if(gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesNames.CATALOGUE_ADMIN.getRoleName())){ - logger.debug("User " + username + " has role " + gCubeRole.getRoleName() + " in " + gCubeGroupName); - correspondentRoleToCheck = RolesIntoOrganization.ADMIN; - break; - } - } + // get highest role according liferay + 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 = ckanPublisherServicesImpl.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())){ - toReturn.add(new OrganizationBean(ckanOrganization.getTitle(), ckanOrganization.getName())); - break; - } - } - } - } + checkIfRoleIsSetInCkanInstance(username, gCubeGroupName, gCubeGroup.getGroupId(), correspondentRoleToCheck, toReturn, groupManager, ckanPublisherServicesImpl); } }else if(groupManager.isVO(currentGroupId)){ @@ -213,41 +189,14 @@ public class Utils { List roles = roleManager.listRolesByUserAndGroup(userid, groupManager.getGroupId(gCubeGroupName)); - // the default one - 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 gCubeRole : roles) { - - logger.debug("User " + username + " has role " + gCubeRole.getRoleName() + " in " + gCubeGroupName); - if(gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesNames.CATALOGUE_ADMIN.getRoleName())){ - correspondentRoleToCheck = RolesIntoOrganization.ADMIN; - break; - } - } + // get highest role according liferay + 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 = ckanPublisherServicesImpl.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())){ - toReturn.add(new OrganizationBean(ckanOrganization.getTitle(), ckanOrganization.getName())); - break; - } - } - } - } + checkIfRoleIsSetInCkanInstance(username, gCubeGroupName, gCubeGroup.getGroupId(), correspondentRoleToCheck, toReturn, groupManager, ckanPublisherServicesImpl); } }else if(groupManager.isVRE(currentGroupId)){ // vre @@ -255,35 +204,12 @@ public class Utils { logger.debug("The list of roles for " + username + " into " + groupName + " is " + roles); - // the default one - 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())){ - correspondentRoleToCheck = RolesIntoOrganization.ADMIN; - break; - } - } + // get highest role according liferay + RolesIntoOrganization correspondentRoleToCheck = getLiferayHighestRoleInOrg(roles); 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 = ckanPublisherServicesImpl.getCkanUtilsObj(groupManager.getInfrastructureScope(currentGroupId)); - boolean res = ckanUtils.checkRole(username, groupName, correspondentRoleToCheck); - - if(res){ - // get the orgs of the user - List ckanOrgs = ckanUtils.getOrganizationsByUser(username); - for (CkanOrganization ckanOrganization : ckanOrgs) { - if(ckanOrganization.getName().equals(groupName.toLowerCase())){ - toReturn.add(new OrganizationBean(ckanOrganization.getTitle(), ckanOrganization.getName())); - break; - } - } - } + checkIfRoleIsSetInCkanInstance(username, groupName, currentGroupId, correspondentRoleToCheck, toReturn, groupManager, ckanPublisherServicesImpl); } } }catch(Exception e){ @@ -294,6 +220,60 @@ public class Utils { return toReturn; } + /** + * 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 toReturn + * @param groupManager + * @param ckanPublisherServicesImpl + * @throws UserManagementSystemException + * @throws GroupRetrievalFault + */ + private static void checkIfRoleIsSetInCkanInstance(String username, + String gCubeGroupName, long groupId, + RolesIntoOrganization correspondentRoleToCheck, + List orgs, GroupManager groupManager, CKANPublisherServicesImpl ckanPublisherServicesImpl) 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 = ckanPublisherServicesImpl.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())){ + orgs.add(new OrganizationBean(ckanOrganization.getTitle(), ckanOrganization.getName())); + break; + } + } + } + } + + } + + /** + * 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; + } + /** * Given a ckan organization name retrieve the infrastructure scope * @param organizationName (prevre, devvre, ...)