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/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..aed95006d 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; @@ -48,6 +52,15 @@ public class PublishedNewsImagesPermissionsUpgradePluginTest { @Mock SessionProvider sessionProvider; + private static final MockedStatic PORTAL_CONTAINER = mockStatic(PortalContainer.class); + private static final MockedStatic COMMONS_UTILS = mockStatic(CommonsUtils.class); + + @AfterClass + public static void afterRunBare() throws Exception { // NOSONAR + COMMONS_UTILS.close(); + PORTAL_CONTAINER.close(); + } + @Test public void publishedNewsImagesPermissionsUpgradePluginTest() throws Exception { InitParams initParams = new InitParams(); @@ -91,5 +104,25 @@ 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"; + COMMONS_UTILS.when(() -> CommonsUtils.getRestContextName()).thenReturn(restContextName); + 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(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(); + } }