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/PublishedNewsImagesPermissionsUpgradePlugin.java index ebbf054bd..5661ff91a 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/PublishedNewsImagesPermissionsUpgradePlugin.java @@ -16,6 +16,8 @@ */ package org.exoplatform.news.upgrade.jcr; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -28,6 +30,8 @@ import org.exoplatform.commons.upgrade.UpgradePluginExecutionContext; import org.exoplatform.commons.upgrade.UpgradeProductPlugin; +import org.exoplatform.commons.utils.CommonsUtils; +import org.exoplatform.container.PortalContainer; import org.exoplatform.container.xml.InitParams; import org.exoplatform.services.jcr.RepositoryService; import org.exoplatform.services.jcr.core.ExtendedNode; @@ -47,7 +51,7 @@ public class PublishedNewsImagesPermissionsUpgradePlugin extends UpgradeProductP private static final Log LOG = ExoLogger.getLogger(PublishedNewsImagesPermissionsUpgradePlugin.class.getName()); - private static final Pattern IMAGE_SRC_PATTERN = Pattern.compile("src=\"/portal/rest/images/?(.+)?\""); + private static final String IMAGE_SRC_REGEX = "src=\"/portal/rest/images/?(.+)?\""; private final RepositoryService repositoryService; @@ -122,23 +126,46 @@ public void processUpgrade(String s, String s1) { } private void updateNewsImagesPermissions(Node newsNode, Session session) throws RepositoryException { - Matcher matcher = IMAGE_SRC_PATTERN.matcher(getStringProperty(newsNode, "exo:body")); + Matcher matcher = Pattern.compile(IMAGE_SRC_REGEX).matcher(getStringProperty(newsNode, "exo:body")); int imagesCount = 0; + ExtendedNode image = null; while (matcher.find()) { String match = matcher.group(1); String imageUUID = match.substring(match.lastIndexOf("/") + 1); - ExtendedNode image = (ExtendedNode) session.getNodeByUUID(imageUUID); + image = (ExtendedNode) session.getNodeByUUID(imageUUID); if (image != null) { if (image.canAddMixin(EXO_PRIVILEGEABLE)) { image.addMixin(EXO_PRIVILEGEABLE); } boolean isPublicImage = image.getACL() - .getPermissionEntries() - .stream() - .filter(accessControlEntry -> accessControlEntry.getIdentity() - .equals(PLATFORM_USERS_GROUP_IDENTITY)) - .toList() - .size() > 0; + .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; + } + } + } + String existingUploadImagesSrcRegex = "src=\"" + CommonsUtils.getCurrentDomain() + "/" + + PortalContainer.getCurrentPortalContainerName() + "/" + CommonsUtils.getRestContextName() + "/jcr/?(.+)?\""; + matcher = Pattern.compile(existingUploadImagesSrcRegex).matcher(getStringProperty(newsNode, "exo:body")); + while (matcher.find()) { + 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); + } + 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); @@ -159,4 +186,13 @@ private String getStringProperty(Node node, String propertyName) throws Reposito } return ""; } + + private Node getNodeByPath(String path, Session session) { + try { + return (Node) session.getItem(URLDecoder.decode(path, StandardCharsets.UTF_8)); + } catch (RepositoryException exception) { + return null; + } + } + } 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 66fd1534c..4dc923dd0 100644 --- a/data-upgrade-news/src/main/resources/conf/portal/configuration.xml +++ b/data-upgrade-news/src/main/resources/conf/portal/configuration.xml @@ -284,7 +284,7 @@ plugin.upgrade.target.version Target version of the plugin - 6.4.2 + 6.4.3 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 35456bea9..06057ac21 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 @@ -11,9 +11,13 @@ import javax.jcr.query.QueryManager; import javax.jcr.query.QueryResult; +import org.exoplatform.commons.utils.CommonsUtils; +import org.exoplatform.container.PortalContainer; +import org.junit.AfterClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.junit.MockitoJUnitRunner; import org.exoplatform.container.xml.InitParams; @@ -26,8 +30,14 @@ 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.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; -@RunWith(MockitoJUnitRunner.class) +@RunWith(PowerMockRunner.class) +@PowerMockIgnore({"com.sun.*", "org.w3c.*", "javax.naming.*", "javax.xml.*", "org.xml.*", "javax.management.*"}) +@PrepareForTest({CommonsUtils.class, PortalContainer.class}) public class PublishedNewsImagesPermissionsUpgradePluginTest { @Mock @@ -91,5 +101,27 @@ public void publishedNewsImagesPermissionsUpgradePluginTest() throws Exception { // then verify(imageNode, times(1)).setPermission("*:/platform/users", new String[] { "read" }); verify(imageNode, times(1)).save(); + + // + when(nodeIterator.hasNext()).thenReturn(true, false); + when(property.getString()).thenReturn("news body with image src=\"https://exoplatform.com/portal/rest/jcr/repository/collaboration/Groups/spaces/test/testimage\""); + String currentDomainName = "https://exoplatform.com"; + String currentPortalContainerName = "portal"; + String restContextName = "rest"; + PowerMockito.mockStatic(CommonsUtils.class); + PowerMockito.mockStatic(PortalContainer.class); + when(CommonsUtils.getRestContextName()).thenReturn(restContextName); + when(PortalContainer.getCurrentPortalContainerName()).thenReturn(currentPortalContainerName); + when(CommonsUtils.getCurrentDomain()).thenReturn(currentDomainName); + ExtendedNode existingUploadImageNode = mock(ExtendedNode.class); + when(existingUploadImageNode.canAddMixin(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)).save(); + } }