From e1a034770af81060e1a68b734cafbb7f7ab8552e Mon Sep 17 00:00:00 2001 From: Helmi Akermi <70575401+hakermi@users.noreply.github.com> Date: Thu, 1 Aug 2024 16:23:33 +0100 Subject: [PATCH] fix: Update the PublishedNewsImagesPermissionsUpgradePlugin to change the published news images permissions to be visible for users that are not in the space- EXO-60866 --- ...NewsImagesPermissionsV2UpgradePlugin.java} | 69 +++++++++---------- .../resources/conf/portal/configuration.xml | 4 +- ...ewsImagesPermissionsUpgradePluginTest.java | 26 ++++--- 3 files changed, 54 insertions(+), 45 deletions(-) rename data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/{PublishedNewsImagesPermissionsUpgradePlugin.java => PublishedNewsImagesPermissionsV2UpgradePlugin.java} (80%) diff --git a/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsUpgradePlugin.java b/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsV2UpgradePlugin.java similarity index 80% rename from data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsUpgradePlugin.java rename to data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsV2UpgradePlugin.java index 5661ff91a..d7b7ca288 100644 --- a/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsUpgradePlugin.java +++ b/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsV2UpgradePlugin.java @@ -34,39 +34,41 @@ import org.exoplatform.container.PortalContainer; import org.exoplatform.container.xml.InitParams; import org.exoplatform.services.jcr.RepositoryService; +import org.exoplatform.services.jcr.access.PermissionType; import org.exoplatform.services.jcr.core.ExtendedNode; import org.exoplatform.services.jcr.ext.app.SessionProviderService; import org.exoplatform.services.jcr.ext.common.SessionProvider; import org.exoplatform.services.jcr.impl.core.query.QueryImpl; import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; +import org.exoplatform.services.wcm.core.NodetypeConstant; +import org.exoplatform.social.core.space.model.Space; +import org.exoplatform.social.core.space.spi.SpaceService; -public class PublishedNewsImagesPermissionsUpgradePlugin extends UpgradeProductPlugin { - public static final String EXO_PRIVILEGEABLE = "exo:privilegeable"; - - public static final String[] READ_PERMISSIONS = new String[] { "read" }; - - public static final String PLATFORM_USERS_GROUP_IDENTITY = "*:/platform/users"; +public class PublishedNewsImagesPermissionsV2UpgradePlugin extends UpgradeProductPlugin { private static final Log LOG = - ExoLogger.getLogger(PublishedNewsImagesPermissionsUpgradePlugin.class.getName()); + ExoLogger.getLogger(PublishedNewsImagesPermissionsV2UpgradePlugin.class.getName()); private static final String IMAGE_SRC_REGEX = "src=\"/portal/rest/images/?(.+)?\""; private final RepositoryService repositoryService; private final SessionProviderService sessionProviderService; + private final SpaceService spaceService; private int imageNewsUpdatedCount; private int newsCount; - public PublishedNewsImagesPermissionsUpgradePlugin(InitParams initParams, + public PublishedNewsImagesPermissionsV2UpgradePlugin(InitParams initParams, RepositoryService repositoryService, - SessionProviderService sessionProviderService) { + SessionProviderService sessionProviderService, + SpaceService spaceService) { super(initParams); this.repositoryService = repositoryService; this.sessionProviderService = sessionProviderService; + this.spaceService = spaceService; } @Override @@ -133,22 +135,7 @@ private void updateNewsImagesPermissions(Node newsNode, Session session) throws String match = matcher.group(1); String imageUUID = match.substring(match.lastIndexOf("/") + 1); image = (ExtendedNode) session.getNodeByUUID(imageUUID); - if (image != null) { - if (image.canAddMixin(EXO_PRIVILEGEABLE)) { - image.addMixin(EXO_PRIVILEGEABLE); - } - boolean isPublicImage = image.getACL() - .getPermissionEntries() - .stream() - .anyMatch(accessControlEntry -> accessControlEntry.getIdentity() - .equals(PLATFORM_USERS_GROUP_IDENTITY)); - if (!isPublicImage) { - // make news images public - image.setPermission(PLATFORM_USERS_GROUP_IDENTITY, READ_PERMISSIONS); - image.save(); - imagesCount += 1; - } - } + imagesCount = updateNodePermissions(newsNode, image, imagesCount); } String existingUploadImagesSrcRegex = "src=\"" + CommonsUtils.getCurrentDomain() + "/" + PortalContainer.getCurrentPortalContainerName() + "/" + CommonsUtils.getRestContextName() + "/jcr/?(.+)?\""; @@ -157,27 +144,39 @@ private void updateNewsImagesPermissions(Node newsNode, Session session) throws String match = matcher.group(1); String imagePath = match.substring(match.indexOf("/Groups")); image = (ExtendedNode) getNodeByPath(imagePath, session); - if (image != null) { - if (image.canAddMixin(EXO_PRIVILEGEABLE)) { - image.addMixin(EXO_PRIVILEGEABLE); + imagesCount = updateNodePermissions(newsNode, image, imagesCount); + } + if (imagesCount > 0) { + this.newsCount += 1; + this.imageNewsUpdatedCount += imagesCount; + } + } + + private int updateNodePermissions(Node newsNode, ExtendedNode image, int imagesCount) throws RepositoryException { + if (image != null) { + if (image.canAddMixin(NodetypeConstant.EXO_PRIVILEGEABLE)) { + image.addMixin(NodetypeConstant.EXO_PRIVILEGEABLE); } + String spaceId = getStringProperty(newsNode, "exo:spaceId"); + Space space = spaceService.getSpaceById(spaceId); + if (space != null) { + ((ExtendedNode) image).setPermission("*:" + space.getGroupId(), new String[]{PermissionType.READ}); + image.save(); + } + if (getStringProperty(newsNode, "exo:audience").equals("all")) { boolean isPublicImage = image.getACL() .getPermissionEntries() .stream() .anyMatch(accessControlEntry -> accessControlEntry.getIdentity() - .equals(PLATFORM_USERS_GROUP_IDENTITY)); + .equals("any")); if (!isPublicImage) { - // make news images public - image.setPermission(PLATFORM_USERS_GROUP_IDENTITY, READ_PERMISSIONS); + ((ExtendedNode) image).setPermission("any", new String[]{PermissionType.READ}); image.save(); imagesCount += 1; } } } - if (imagesCount > 0) { - this.newsCount += 1; - this.imageNewsUpdatedCount += imagesCount; - } + return imagesCount; } private String getStringProperty(Node node, String propertyName) throws RepositoryException { diff --git a/data-upgrade-news/src/main/resources/conf/portal/configuration.xml b/data-upgrade-news/src/main/resources/conf/portal/configuration.xml index 1dde6eb85..b657ab132 100644 --- a/data-upgrade-news/src/main/resources/conf/portal/configuration.xml +++ b/data-upgrade-news/src/main/resources/conf/portal/configuration.xml @@ -256,9 +256,9 @@ - PublishedNewsImagesPermissionsUpgradePlugin + PublishedNewsImagesPermissionsV2UpgradePlugin addUpgradePlugin - org.exoplatform.news.upgrade.jcr.PublishedNewsImagesPermissionsUpgradePlugin + org.exoplatform.news.upgrade.jcr.PublishedNewsImagesPermissionsV2UpgradePlugin Update published news images permissions diff --git a/data-upgrade-news/src/test/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsUpgradePluginTest.java b/data-upgrade-news/src/test/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsUpgradePluginTest.java index aed95006d..6ac58b1c4 100644 --- a/data-upgrade-news/src/test/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsUpgradePluginTest.java +++ b/data-upgrade-news/src/test/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsUpgradePluginTest.java @@ -1,6 +1,5 @@ package org.exoplatform.news.upgrade.jcr; -import static org.exoplatform.news.upgrade.jcr.PublishedNewsImagesPermissionsUpgradePlugin.EXO_PRIVILEGEABLE; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; @@ -13,6 +12,8 @@ import org.exoplatform.commons.utils.CommonsUtils; import org.exoplatform.container.PortalContainer; +import org.exoplatform.services.wcm.core.NodetypeConstant; +import org.exoplatform.social.core.space.spi.SpaceService; import org.junit.AfterClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,6 +44,9 @@ public class PublishedNewsImagesPermissionsUpgradePluginTest { @Mock RepositoryEntry repositoryEntry; + @Mock + SpaceService spaceService; + @Mock Session session; @@ -72,6 +76,7 @@ public void publishedNewsImagesPermissionsUpgradePluginTest() throws Exception { when(repositoryService.getCurrentRepository()).thenReturn(repository); when(repository.getConfiguration()).thenReturn(repositoryEntry); when(sessionProvider.getSession(any(), any())).thenReturn(session); + when(spaceService.getSpaceById(any())).thenReturn(null); QueryManager qm = mock(QueryManager.class); Workspace workSpace = mock(Workspace.class); when(session.getWorkspace()).thenReturn(workSpace); @@ -85,24 +90,29 @@ public void publishedNewsImagesPermissionsUpgradePluginTest() throws Exception { when(nodeIterator.hasNext()).thenReturn(true, false); Node newsNode = mock(Node.class); Property property = mock(Property.class); + Property audienceProperty = mock(Property.class); when(nodeIterator.nextNode()).thenReturn(newsNode); when(newsNode.hasProperty("exo:body")).thenReturn(true); + when(newsNode.hasProperty("exo:audience")).thenReturn(true); when(newsNode.getProperty("exo:body")).thenReturn(property); + when(newsNode.getProperty("exo:audience")).thenReturn(audienceProperty); when(property.getString()).thenReturn("news body with image src=\"/portal/rest/images/repository/collaboration/123\""); + when(audienceProperty.getString()).thenReturn("all"); ExtendedNode imageNode = mock(ExtendedNode.class); when(session.getNodeByUUID("123")).thenReturn(imageNode); - when(imageNode.canAddMixin(EXO_PRIVILEGEABLE)).thenReturn(true); + when(imageNode.canAddMixin(NodetypeConstant.EXO_PRIVILEGEABLE)).thenReturn(true); AccessControlList accessControlList = mock(AccessControlList.class); when(imageNode.getACL()).thenReturn(accessControlList); when(accessControlList.getPermissionEntries()).thenReturn(new ArrayList<>()); // when - PublishedNewsImagesPermissionsUpgradePlugin publishedNewsImagesPermissionsUpgradePlugin = - new PublishedNewsImagesPermissionsUpgradePlugin(initParams, + PublishedNewsImagesPermissionsV2UpgradePlugin publishedNewsImagesPermissionsUpgradePlugin = + new PublishedNewsImagesPermissionsV2UpgradePlugin(initParams, repositoryService, - sessionProviderService); + sessionProviderService, + spaceService); publishedNewsImagesPermissionsUpgradePlugin.processUpgrade(null, null); // then - verify(imageNode, times(1)).setPermission("*:/platform/users", new String[] { "read" }); + verify(imageNode, times(1)).setPermission("any", new String[] { "read" }); verify(imageNode, times(1)).save(); // @@ -115,13 +125,13 @@ public void publishedNewsImagesPermissionsUpgradePluginTest() throws Exception { PORTAL_CONTAINER.when(() -> PortalContainer.getCurrentPortalContainerName()).thenReturn(currentPortalContainerName); COMMONS_UTILS.when(() -> CommonsUtils.getCurrentDomain()).thenReturn(currentDomainName); ExtendedNode existingUploadImageNode = mock(ExtendedNode.class); - when(existingUploadImageNode.canAddMixin(EXO_PRIVILEGEABLE)).thenReturn(true); + when(existingUploadImageNode.canAddMixin(NodetypeConstant.EXO_PRIVILEGEABLE)).thenReturn(true); when(session.getItem(nullable(String.class))).thenReturn(existingUploadImageNode); when(existingUploadImageNode.getACL()).thenReturn(accessControlList); publishedNewsImagesPermissionsUpgradePlugin.processUpgrade(null, null); // then - verify(existingUploadImageNode, times(1)).setPermission("*:/platform/users", new String[] { "read" }); + verify(existingUploadImageNode, times(1)).setPermission("any", new String[] { "read" }); verify(existingUploadImageNode, times(1)).save(); }