diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 5fe3680..5e9f405 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,6 +4,15 @@ + + uses + + + uses + + + uses + 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 c5555ea..b43d2c9 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 @@ -4,7 +4,6 @@ package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -56,67 +55,36 @@ public class UserUtil { UserManager userManager = new LiferayUserManager(); RoleManager roleManager = new LiferayRoleManager(); Map mapRoleByGroupSingleVre = new HashMap(); - Map mapRoleByGroupExtrasVre = new HashMap(); GCubeUser user; try { user = userManager.getUserByEmail(userEMail); - List listOfGroups = groupManager.listGroupsByUser(user.getUserId()); - + // filter according the current context: if it is a VO/VRE, we send all the VRES under the VO. If it is the root vo, we send all user's vres. long groupIdContext = groupManager.getGroupIdFromInfrastructureScope(context); - GCubeGroup groupContext = groupManager.getGroup(groupIdContext); - Iterator iterator = listOfGroups.iterator(); - if(groupManager.isVRE(groupIdContext)){ - long parentId = groupContext.getParentGroupId(); - while (iterator.hasNext()) { - GCubeGroup gCubeGroup = (GCubeGroup) iterator.next(); - if(gCubeGroup.getParentGroupId() != parentId) - iterator.remove(); - } - }else if(groupManager.isVO(groupIdContext)){ - // get the list of vres - while (iterator.hasNext()) { - GCubeGroup gCubeGroup = (GCubeGroup) iterator.next(); - if(groupIdContext != gCubeGroup.getParentGroupId()) - iterator.remove(); - } - }else{ - // only the vres - while (iterator.hasNext()) { - GCubeGroup gCubeGroup = (GCubeGroup) iterator.next(); - if(!groupManager.isVRE(gCubeGroup.getGroupId())) - iterator.remove(); - } - } - - // retrieve the role - for (GCubeGroup vre: listOfGroups) { - if(vre.getGroupId() == groupIdContext){ - String localRole = RolesCkanGroupOrOrg.convertToCkanCapacity(getLiferayHighestRoleInOrg(roleManager.listRolesByUserAndGroup(user.getUserId(), vre.getGroupId()))); - mapRoleByGroupSingleVre.put(vre.getGroupName().toLowerCase(), - localRole); - // check if extra roles must be assigned - logger.debug("Checking if there is the need to extend role " + localRole + " for user " + user.getUsername() + " in other vres"); - instance.assignRolesOtherOrganization(user.getUsername(), - vre.getGroupName().toLowerCase(), - RolesCkanGroupOrOrg.convertFromCapacity(localRole)); - - if(isViewPerVREEnabled) - break; - - }else if(!isViewPerVREEnabled) - mapRoleByGroupExtrasVre.put(vre.getGroupName().toLowerCase(), - RolesCkanGroupOrOrg.convertToCkanCapacity(getLiferayHighestRoleInOrg(roleManager.listRolesByUserAndGroup(user.getUserId(), vre.getGroupId())))); - } - - // schedule the thread for the groups other than the vre to be checked in the local ckan instance - if(mapRoleByGroupExtrasVre.size() > 0) + GCubeGroup currentVRE = groupManager.getGroup(groupIdContext); + String localRole = RolesCkanGroupOrOrg.convertToCkanCapacity(getLiferayHighestRoleInOrg(roleManager.listRolesByUserAndGroup(user.getUserId(), currentVRE.getGroupId()))); + mapRoleByGroupSingleVre.put(currentVRE.getGroupName().toLowerCase(), + localRole); + + // check if extra roles must be assigned + logger.debug("Checking if there is the need to extend role " + localRole + " for user " + user.getUsername() + " in other vres"); + instance.assignRolesOtherOrganization(user.getUsername(), + currentVRE.getGroupName().toLowerCase(), + RolesCkanGroupOrOrg.convertFromCapacity(localRole)); + + // perform further checks + if(!isViewPerVREEnabled){ + List listOfGroups = groupManager.listGroupsByUser(user.getUserId()); new AddUserToOrganizationThread( instance, - user.getUsername(), - mapRoleByGroupExtrasVre). + user, + listOfGroups, + isViewPerVREEnabled, + groupIdContext, + roleManager, + groupManager). start(); - + } logger.debug("Returning Map to the ckan connector : " + mapRoleByGroupSingleVre); return mapRoleByGroupSingleVre; }catch (UserManagementSystemException | UserRetrievalFault | GroupRetrievalFault e) { @@ -274,7 +242,7 @@ public class UserUtil { * @param roles * @return MEMBER/EDITOR/ADMIN role */ - private static RolesCkanGroupOrOrg getLiferayHighestRoleInOrg( + public static RolesCkanGroupOrOrg getLiferayHighestRoleInOrg( List roles) { // NOTE: it is supposed that there is just one role for this person correspondent to the one in the catalog diff --git a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/thread/AddUserToOrganizationThread.java b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/thread/AddUserToOrganizationThread.java index b5b1837..9c33c06 100644 --- a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/thread/AddUserToOrganizationThread.java +++ b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/thread/AddUserToOrganizationThread.java @@ -1,11 +1,19 @@ package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server.thread; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg; +import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server.UserUtil; +import org.gcube.vomanagement.usermanagement.GroupManager; +import org.gcube.vomanagement.usermanagement.RoleManager; +import org.gcube.vomanagement.usermanagement.model.GCubeGroup; +import org.gcube.vomanagement.usermanagement.model.GCubeUser; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; @@ -18,39 +26,96 @@ public class AddUserToOrganizationThread extends Thread { private static final Log logger = LogFactoryUtil.getLog(AddUserToOrganizationThread.class); private DataCatalogue instance; - private String username; - private Map orgAndCapacity; + private GCubeUser user; + private List listOfGroups; + private boolean isViewPerVREEnabled; + private long groupIdContext; + private GroupManager groupManager; + private RoleManager roleManager; /** * @param instance - * @param username - * @param orgAndCapacity + * @param user + * @param listOfGroups + * @param isViewPerVREEnabled + * @param groupIdContext + * @param groupManager + * @param roleManager */ - public AddUserToOrganizationThread(DataCatalogue instance, String username, - Map orgAndCapacity) { + public AddUserToOrganizationThread(DataCatalogue instance, GCubeUser user, + List listOfGroups, + boolean isViewPerVREEnabled, + long groupIdContext, RoleManager roleManager, GroupManager groupManager) { super(); this.instance = instance; - this.username = username; - this.orgAndCapacity = orgAndCapacity; + this.user = user; + this.listOfGroups = listOfGroups; + this.isViewPerVREEnabled= isViewPerVREEnabled; + this.groupIdContext = groupIdContext; + this.roleManager = roleManager; + this.groupManager = groupManager; } @Override public void run() { - logger.debug("Thread for role association started. Organizations and roles are in the map: " + orgAndCapacity); - Set> entrySet = orgAndCapacity.entrySet(); - for (Entry entry : entrySet) { - if(instance.getOrganizationByName(entry.getKey()) != null){ - instance.checkRoleIntoOrganization(username, entry.getKey(), RolesCkanGroupOrOrg.convertFromCapacity(entry.getValue())); - instance.assignRolesOtherOrganization(username, entry.getKey(), RolesCkanGroupOrOrg.convertFromCapacity(entry.getValue())); - } - else if(instance.getGroupByName(entry.getKey()) != null){ - RolesCkanGroupOrOrg roleInGroup = RolesCkanGroupOrOrg.convertFromCapacity(entry.getValue()); - roleInGroup = roleInGroup.equals(RolesCkanGroupOrOrg.EDITOR) ? RolesCkanGroupOrOrg.MEMBER : roleInGroup; - instance.checkRoleIntoGroup(username, entry.getKey(), roleInGroup); - } - } - logger.debug("Thread for role association ended"); + try{ + + Map orgAndCapacity = new HashMap(); + String username = user.getUsername(); + GCubeGroup groupContext = groupManager.getGroup(groupIdContext); + Iterator iterator = listOfGroups.iterator(); + + if(groupManager.isVRE(groupIdContext)){ + long parentId = groupContext.getParentGroupId(); + while (iterator.hasNext()) { + GCubeGroup gCubeGroup = (GCubeGroup) iterator.next(); + if(gCubeGroup.getParentGroupId() != parentId) + iterator.remove(); + } + }else if(groupManager.isVO(groupIdContext)){ + // get the list of vres + while (iterator.hasNext()) { + GCubeGroup gCubeGroup = (GCubeGroup) iterator.next(); + if(groupIdContext != gCubeGroup.getParentGroupId()) + iterator.remove(); + } + }else{ + // only the vres + while (iterator.hasNext()) { + GCubeGroup gCubeGroup = (GCubeGroup) iterator.next(); + if(!groupManager.isVRE(gCubeGroup.getGroupId())) + iterator.remove(); + } + } + + // retrieve the role + for (GCubeGroup vre: listOfGroups) { + if(vre.getGroupId() == groupIdContext){ + continue; + }else if(!isViewPerVREEnabled) + orgAndCapacity.put(vre.getGroupName().toLowerCase(), + RolesCkanGroupOrOrg.convertToCkanCapacity(UserUtil.getLiferayHighestRoleInOrg(roleManager.listRolesByUserAndGroup(user.getUserId(), vre.getGroupId())))); + } + + + logger.debug("Thread for role association started. Organizations and roles are in the map: " + orgAndCapacity); + Set> entrySet = orgAndCapacity.entrySet(); + for (Entry entry : entrySet) { + if(instance.getOrganizationByName(entry.getKey()) != null){ + instance.checkRoleIntoOrganization(username, entry.getKey(), RolesCkanGroupOrOrg.convertFromCapacity(entry.getValue())); + instance.assignRolesOtherOrganization(username, entry.getKey(), RolesCkanGroupOrOrg.convertFromCapacity(entry.getValue())); + } + else if(instance.getGroupByName(entry.getKey()) != null){ + RolesCkanGroupOrOrg roleInGroup = RolesCkanGroupOrOrg.convertFromCapacity(entry.getValue()); + roleInGroup = roleInGroup.equals(RolesCkanGroupOrOrg.EDITOR) ? RolesCkanGroupOrOrg.MEMBER : roleInGroup; + instance.checkRoleIntoGroup(username, entry.getKey(), roleInGroup); + } + } + logger.debug("Thread for role association ended"); + }catch(Exception e){ + logger.error("Failed while adding user to other organization", e); + } } }