Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : enable to display published news images selected from existing upload for non members - EXO-65111 #163

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -284,7 +284,7 @@
<value-param>
<name>plugin.upgrade.target.version</name>
<description>Target version of the plugin</description>
<value>6.4.2</value>
<value>6.4.3</value>
</value-param>
</init-params>
</component-plugin>
Expand Down
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 All @@ -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
Expand Down Expand Up @@ -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();

}
}