From 5d4b49b43a422577a16a15c64317823ffa9ee5e4 Mon Sep 17 00:00:00 2001 From: "massimiliano.assante" Date: Wed, 14 Sep 2016 14:27:12 +0000 Subject: [PATCH] Shows only the virtual groups available in the Site it is deployed on git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/my-vres@131354 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 10 +- .project | 1 + .settings/org.eclipse.wst.common.component | 1 + distro/changelog.xml | 3 + pom.xml | 2 +- .../my_vres/server/MyVREsServiceImpl.java | 94 +++++++++++++++++-- 6 files changed, 101 insertions(+), 10 deletions(-) diff --git a/.classpath b/.classpath index ab97d04..3668b93 100644 --- a/.classpath +++ b/.classpath @@ -1,12 +1,16 @@ - + - + + + + + @@ -34,5 +38,5 @@ - + diff --git a/.project b/.project index 9969891..dc13cc9 100644 --- a/.project +++ b/.project @@ -55,5 +55,6 @@ org.eclipse.wst.common.project.facet.core.nature org.eclipse.wst.jsdt.core.jsNature com.google.gwt.eclipse.core.gwtNature + com.liferay.ide.core.liferayNature diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index c897cc3..7d5323a 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,6 +4,7 @@ + uses diff --git a/distro/changelog.xml b/distro/changelog.xml index 57f0dc5..8d9f2ec 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,4 +1,7 @@ + + Shows only the virtual groups available in the Site it is deployed on + Ported to Liferay 6.2 diff --git a/pom.xml b/pom.xml index b62d9e5..ead9b7f 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ org.gcube.portlets.user my-vres war - 2.0.1-SNAPSHOT + 2.1.0-SNAPSHOT My VREs Portlet gCube My VREs Portlet shows only the VO and VREs a user is member of, divided by category. diff --git a/src/main/java/org/gcube/portlet/user/my_vres/server/MyVREsServiceImpl.java b/src/main/java/org/gcube/portlet/user/my_vres/server/MyVREsServiceImpl.java index b2dbafe..dc5d2db 100644 --- a/src/main/java/org/gcube/portlet/user/my_vres/server/MyVREsServiceImpl.java +++ b/src/main/java/org/gcube/portlet/user/my_vres/server/MyVREsServiceImpl.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.gcube.application.framework.core.session.ASLSession; @@ -14,11 +15,13 @@ import org.gcube.common.portal.PortalContext; import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper; import org.gcube.portlet.user.my_vres.client.MyVREsService; import org.gcube.portlet.user.my_vres.shared.UserBelonging; -import org.gcube.portlet.user.my_vres.shared.VO; import org.gcube.portlet.user.my_vres.shared.VRE; import org.gcube.vomanagement.usermanagement.GroupManager; +import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; +import org.gcube.vomanagement.usermanagement.exception.VirtualGroupNotExistingException; import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; +import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys; import org.gcube.vomanagement.usermanagement.model.GCubeGroup; import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.gcube.vomanagement.usermanagement.model.VirtualGroup; @@ -26,7 +29,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gwt.user.server.rpc.RemoteServiceServlet; +import com.liferay.portal.kernel.exception.PortalException; +import com.liferay.portal.kernel.exception.SystemException; +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.GroupLocalServiceUtil; +import com.liferay.portal.service.LayoutSetLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil; +import com.liferay.portal.service.VirtualHostLocalServiceUtil; /** * The server side implementation of the RPC service. @@ -93,15 +107,17 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer GroupManager gm = new LiferayGroupManager(); LinkedHashMap> toReturn = new LinkedHashMap>(); - //before iterating the actual groups create the virtualGroups in the correct order - List virtualGroups = gm.getVirtualGroups(); - for (VirtualGroup vg : virtualGroups) { + + long currentSiteGroupId = getSiteFromServletRequest(getThreadLocalRequest()).getGroupId(); + List currentSiteVGroups = getVirtualGroups(currentSiteGroupId); + for (VirtualGroup vg : currentSiteVGroups) { String gName = vg.getName(); ArrayList toCreate = new ArrayList(); String cat = gName; toReturn.put(cat, toCreate); } - + + GCubeGroup rootGroupVO = gm.getRootVO(); try { _log.debug("root: " + rootGroupVO.getGroupName() ); @@ -242,6 +258,72 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer return toReturn; } - + /** + * 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 + */ + private List getVirtualGroups(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; + } + /** + * + * @param request + * @return the current Group instance based on the request + * @throws PortalException + * @throws SystemException + */ + private Group getSiteFromServletRequest(final HttpServletRequest request) throws PortalException, SystemException { + String serverName = request.getServerName(); + _log.debug("currentHost is " + serverName); + Group site = null; + List vHosts = VirtualHostLocalServiceUtil.getVirtualHosts(0, VirtualHostLocalServiceUtil.getVirtualHostsCount()); + for (VirtualHost virtualHost : vHosts) { + _log.debug("Found " + virtualHost.getHostname()); + if (virtualHost.getHostname().compareTo("localhost") != 0 && + virtualHost.getLayoutSetId() != 0 && + virtualHost.getHostname().compareTo(serverName) == 0) { + long layoutSetId = virtualHost.getLayoutSetId(); + site = LayoutSetLocalServiceUtil.getLayoutSet(layoutSetId).getGroup(); + _log.debug("Found match! Your site is " + site.getName()); + return site; + } + } + return null; + } } \ No newline at end of file