Skip to content

Commit

Permalink
#27516 support other versionable cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jdotcms committed Feb 7, 2024
1 parent 6731a0e commit 85ea950
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Expand Up @@ -6,6 +6,7 @@
import com.dotcms.rendering.velocity.viewtools.secrets.DotVelocitySecretAppConfig;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.Role;
import com.dotmarketing.business.Versionable;
import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.filters.CMSUrlUtil;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
Expand Down Expand Up @@ -163,10 +164,22 @@ private boolean checkRoleFromLastModUser(final Role role) {
if (UtilMethods.isSet(resourcePath)) {
String contentletInode = StringPool.BLANK;
try {

contentletInode = CMSUrlUtil.getInstance().getInodeFromUrlPath(resourcePath);
final Contentlet contentlet = APILocator.getContentletAPI().find(contentletInode, APILocator.systemUser(), true);
final User lastModifiedUser = APILocator.getUserAPI().loadUserById(contentlet.getModUser(), APILocator.systemUser(), true);
hasRole = APILocator.getRoleAPI().doesUserHaveRole(lastModifiedUser, role);
Versionable versionable = APILocator.getContentletAPI().find(contentletInode, APILocator.systemUser(), true);

if (null == versionable) {
versionable = APILocator.getContainerAPI().getLiveContainerById(contentletInode, APILocator.systemUser(), true);
}

if (null == versionable) {
versionable = APILocator.getTemplateAPI().findLiveTemplate(contentletInode, APILocator.systemUser(), true);
}

if (null != versionable) {
final User lastModifiedUser = APILocator.getUserAPI().loadUserById(versionable.getModUser(), APILocator.systemUser(), true);
hasRole = APILocator.getRoleAPI().doesUserHaveRole(lastModifiedUser, role);
}
} catch (final Exception e) {
Logger.warnAndDebug(SecretTool.class, String.format("Failed to find last " +
"modification user from Retrieved ID '%s' in URL Path '%s': %s",
Expand Down
12 changes: 12 additions & 0 deletions dotCMS/src/main/java/com/dotmarketing/filters/CMSUrlUtil.java
Expand Up @@ -50,6 +50,7 @@
import static com.dotmarketing.filters.Constants.CMS_FILTER_QUERY_STRING_OVERRIDE;
import static com.dotmarketing.filters.Constants.CMS_FILTER_URI_OVERRIDE;
import static com.liferay.util.StringPool.FORWARD_SLASH;
import static com.liferay.util.StringPool.PERIOD;
import static com.liferay.util.StringPool.UNDERLINE;
import static java.util.stream.Collectors.toSet;

Expand Down Expand Up @@ -602,6 +603,17 @@ public String getInodeFromUrlPath(final String urlPath) {
final String urlPathWithoutMode = urlPath.substring(mode.name().length() + 2);
return urlPathWithoutMode.substring(0, urlPathWithoutMode.indexOf(FORWARD_SLASH));
}
if (urlPath.startsWith(mode.name() + FORWARD_SLASH)) {
final String urlPathWithoutMode = urlPath.substring(mode.name().length() + 1);
int indexOf = urlPathWithoutMode.indexOf(FORWARD_SLASH);
if (indexOf == -1) {
indexOf = urlPathWithoutMode.indexOf(UNDERLINE);
}
if (indexOf == -1) {
indexOf = urlPathWithoutMode.indexOf(PERIOD);
}
return urlPathWithoutMode.substring(0, indexOf);
}
}

// tries the fe mode: /data/shared/assets/c/e/ce837ff5-dc6f-427a-8f60-d18afc395be9/fileAsset/openai-summarize.vtl
Expand Down
Expand Up @@ -66,6 +66,13 @@ public void test_getIdentifierFromUrlPath() {
final String contentIdentifier3 = CMSUrlUtil.getInstance().getInodeFromUrlPath(feUrlPath);
assertNotNull(contentIdentifier3);
assertEquals("ce837ff5-dc6f-427a-8f60-d18afc395be9", contentIdentifier3);


final String template2UrlPath = "LIVE/d2e56042255158023d03164cd3852ead.templatelayout";
final String contentIdentifier4 = CMSUrlUtil.getInstance().getInodeFromUrlPath(template2UrlPath);
assertNotNull(contentIdentifier4);
assertEquals("d2e56042255158023d03164cd3852ead", contentIdentifier4);

}

}

0 comments on commit 85ea950

Please sign in to comment.