Skip to content

Commit

Permalink
fix: Update the PublishedNewsImagesPermissionsUpgradePlugin to change…
Browse files Browse the repository at this point in the history
… the published news images permissions to be visible for users that are not in the space- EXO-60866
  • Loading branch information
hakermi authored and mkrout committed Oct 16, 2024
1 parent b7614b3 commit e1a0347
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/?(.+)?\"";
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@
</component-plugin>

<component-plugin>
<name>PublishedNewsImagesPermissionsUpgradePlugin</name>
<name>PublishedNewsImagesPermissionsV2UpgradePlugin</name>
<set-method>addUpgradePlugin</set-method>
<type>org.exoplatform.news.upgrade.jcr.PublishedNewsImagesPermissionsUpgradePlugin</type>
<type>org.exoplatform.news.upgrade.jcr.PublishedNewsImagesPermissionsV2UpgradePlugin</type>
<description>Update published news images permissions</description>
<init-params>
<value-param>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;

Expand All @@ -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;
Expand Down Expand Up @@ -43,6 +44,9 @@ public class PublishedNewsImagesPermissionsUpgradePluginTest {
@Mock
RepositoryEntry repositoryEntry;

@Mock
SpaceService spaceService;

@Mock
Session session;

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

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

}
Expand Down

0 comments on commit e1a0347

Please sign in to comment.