diff --git a/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java b/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java index 4ae064e..3b6118d 100644 --- a/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java +++ b/src/main/java/org/gcube/vomanagement/usermanagement/GroupManager.java @@ -3,6 +3,7 @@ package org.gcube.vomanagement.usermanagement; import java.io.Serializable; import java.util.List; import java.util.Map; +import java.util.Set; import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; import org.gcube.vomanagement.usermanagement.exception.UserManagementNameException; @@ -153,6 +154,15 @@ public interface GroupManager { * @throws GroupRetrievalFault */ List listGroupsByUser(long userId) throws UserRetrievalFault, UserManagementSystemException, GroupRetrievalFault; + /** + * @param userId the LR userId + * @param serverName the host name of the server that is sending the request (e.g. i-marine.d4science.org) + * @return a set of VREs where the user is registered as @see {@link GCubeGroup} on the current Site (e.g i-marine.d4science.org) + * @throws UserRetrievalFault + * @throws UserManagementSystemException + * @throws GroupRetrievalFault + */ + Set listGroupsByUserAndSite(long userId, final String serverName) throws UserRetrievalFault, UserManagementSystemException, GroupRetrievalFault, VirtualGroupNotExistingException; /** * * @param userId the LR userId diff --git a/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayGroupManager.java b/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayGroupManager.java index d7f916d..e792e96 100644 --- a/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayGroupManager.java +++ b/src/main/java/org/gcube/vomanagement/usermanagement/impl/LiferayGroupManager.java @@ -7,9 +7,11 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Set; import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; @@ -393,6 +395,26 @@ public class LiferayGroupManager implements GroupManager { * {@inheritDoc} */ @Override + public Set listGroupsByUserAndSite(long userId, final String serverName) throws UserRetrievalFault, UserManagementSystemException, GroupRetrievalFault, VirtualGroupNotExistingException { + Set toReturn = new HashSet<>(); + try { + List currSiteVirtualGroups = ManagementUtils.getVirtualGroupsBySiteGroupId(ManagementUtils.getSiteGroupIdFromServletRequest(serverName)); + for (GCubeGroup userGroup : listGroupsByUser(userId)) { + if (isVRE(userGroup.getGroupId())) { + for (VirtualGroup vg : currSiteVirtualGroups) + if (getVirtualGroup(userGroup.getGroupId()).getName().compareTo(vg.getName()) == 0) + toReturn.add(userGroup); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return toReturn; + } + /** + * {@inheritDoc} + */ + @Override public Map> listGroupsAndRolesByUser(long userId) throws UserManagementSystemException { Map> toReturn = new HashMap>(); try { @@ -615,4 +637,5 @@ public class LiferayGroupManager implements GroupManager { return layoutSetLogo; } + } diff --git a/src/main/java/org/gcube/vomanagement/usermanagement/util/ManagementUtils.java b/src/main/java/org/gcube/vomanagement/usermanagement/util/ManagementUtils.java index 35299c2..7b03681 100644 --- a/src/main/java/org/gcube/vomanagement/usermanagement/util/ManagementUtils.java +++ b/src/main/java/org/gcube/vomanagement/usermanagement/util/ManagementUtils.java @@ -1,5 +1,13 @@ package org.gcube.vomanagement.usermanagement.util; +import java.util.ArrayList; +import java.util.List; + +import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; +import org.gcube.vomanagement.usermanagement.exception.VirtualGroupNotExistingException; +import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; +import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys; +import org.gcube.vomanagement.usermanagement.model.VirtualGroup; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -8,7 +16,17 @@ import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.PropsUtil; import com.liferay.portal.model.Company; +import com.liferay.portal.model.Group; +import com.liferay.portal.model.VirtualHost; +import com.liferay.portal.security.auth.PrincipalThreadLocal; +import com.liferay.portal.security.permission.PermissionChecker; +import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil; +import com.liferay.portal.security.permission.PermissionThreadLocal; import com.liferay.portal.service.CompanyLocalServiceUtil; +import com.liferay.portal.service.GroupLocalServiceUtil; +import com.liferay.portal.service.LayoutSetLocalServiceUtil; +import com.liferay.portal.service.UserLocalServiceUtil; +import com.liferay.portal.service.VirtualHostLocalServiceUtil; public class ManagementUtils { /** @@ -41,4 +59,68 @@ public class ManagementUtils { } return defaultWebId; } + /** + * + * @param serverName the host name of the server that is sending the request (e.g. i-marine.d4science.org) + * @return the current Group instance based on the request + * @throws SystemException + * @throws PortalException + */ + public static long getSiteGroupIdFromServletRequest(final String serverName) throws SystemException, PortalException { + Group site = null; + List vHosts = VirtualHostLocalServiceUtil.getVirtualHosts(0, VirtualHostLocalServiceUtil.getVirtualHostsCount()); + for (VirtualHost virtualHost : vHosts) { + if (virtualHost.getHostname().compareTo("localhost") != 0 && + virtualHost.getLayoutSetId() != 0 && + virtualHost.getHostname().compareTo(serverName) == 0) { + long layoutSetId = virtualHost.getLayoutSetId(); + site = LayoutSetLocalServiceUtil.getLayoutSet(layoutSetId).getGroup(); + return site.getGroupId(); + } + } + _log.warn("serverName is " + serverName + " but i could not find any virtualHost associated to it"); + return -1; + } + /** + * read the list of virtual groups the current site (i-marine, services etc. ) should show up + * @param actualGroupId + * @return he list of virtual groups the current site (i-marine, services etc. ) should show up + * @throws GroupRetrievalFault + * @throws VirtualGroupNotExistingException + */ + public static List getVirtualGroupsBySiteGroupId(long actualGroupId) throws GroupRetrievalFault, VirtualGroupNotExistingException { + List toReturn = new ArrayList(); + try { + long userId = LiferayUserManager.getAdmin().getUserId(); + PrincipalThreadLocal.setName(userId); + PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(UserLocalServiceUtil.getUser(userId)); + PermissionThreadLocal.setPermissionChecker(permissionChecker); + Group site = GroupLocalServiceUtil.getGroup(actualGroupId); + // _log.debug("Set Thread Permission done, getVirtual Group of " + site.getName()); + if (site.getExpandoBridge().getAttribute(CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()) == null || site.getExpandoBridge().getAttribute(CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()).equals("")) { + String warningMessage = String.format("Attribute %s not initialized.", CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()); + _log.warn(warningMessage); + throw new VirtualGroupNotExistingException(warningMessage); + } else { + String[] values = (String[]) site.getExpandoBridge().getAttribute(CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()); + VirtualGroup toAdd = new VirtualGroup(); + if (values != null && values.length > 0) { + for (int i = 0; i < values.length; i++) { + toAdd = new VirtualGroup(); + String[] splits = values[i].split("\\|"); + toAdd.setName(splits[0]); + toAdd.setDescription(splits[1]); + toReturn.add(toAdd); + //_log.debug("VirtualGroup selected found for " + site.getName() + " -> " + toAdd.getName()); + } + } else { + toAdd.setName("NoVirtualGroupAssigned"); + toAdd.setDescription("NoVirtualGroupDescription"); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return toReturn; + } }