Skip to content

Commit

Permalink
fix : enable to display published news images selected from existing …
Browse files Browse the repository at this point in the history
…upload for non members - EXO-65111 (#160) (#162)

This change is going to display the published news images selected from existing upload for non space members.
  • Loading branch information
sofyenne authored Aug 3, 2023
1 parent 6df4af4 commit 6294861
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +52,15 @@ public class PublishedNewsImagesPermissionsUpgradePluginTest {
@Mock
SessionProvider sessionProvider;

private static final MockedStatic<PortalContainer> PORTAL_CONTAINER = mockStatic(PortalContainer.class);
private static final MockedStatic<CommonsUtils> 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();
Expand Down Expand Up @@ -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();

}
}

0 comments on commit 6294861

Please sign in to comment.