diff --git a/dotCMS/src/main/java/com/dotcms/rest/api/v1/folder/FolderHelper.java b/dotCMS/src/main/java/com/dotcms/rest/api/v1/folder/FolderHelper.java index 8325c2f54c3e..522cc374d16d 100644 --- a/dotCMS/src/main/java/com/dotcms/rest/api/v1/folder/FolderHelper.java +++ b/dotCMS/src/main/java/com/dotcms/rest/api/v1/folder/FolderHelper.java @@ -3,7 +3,6 @@ import com.dotcms.util.TreeableNameComparator; import com.dotmarketing.beans.Host; import com.dotmarketing.business.APILocator; -import com.dotmarketing.business.PermissionAPI; import com.dotmarketing.exception.DotDataException; import com.dotmarketing.exception.DotSecurityException; import com.dotmarketing.portlets.contentlet.business.HostAPI; @@ -12,7 +11,6 @@ import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; import com.liferay.portal.model.User; -import com.liferay.util.StringPool; import io.vavr.control.Try; import java.util.ArrayList; @@ -22,6 +20,10 @@ import java.util.Map; import java.util.stream.Collectors; +import static com.dotmarketing.business.PermissionAPI.PERMISSION_CAN_ADD_CHILDREN; +import static com.dotmarketing.util.Constants.DONT_RESPECT_FRONT_END_ROLES; +import static com.liferay.util.StringPool.FORWARD_SLASH; + /** * Created by jasontesser on 9/28/16. */ @@ -120,7 +122,7 @@ public Folder loadFolderByURI(String siteName, User user, String uri) throws Dot */ public FolderView loadFolderAndSubFoldersByPath(final String hostId, final String folder, final User user) throws DotSecurityException, DotDataException { - final String uriParam = !folder.startsWith(StringPool.FORWARD_SLASH) ? StringPool.FORWARD_SLASH.concat(folder) : folder; + final String uriParam = !folder.startsWith(FORWARD_SLASH) ? FORWARD_SLASH.concat(folder) : folder; final Host host = APILocator.getHostAPI().find(hostId,user,false); final Folder folderByPath = APILocator.getFolderAPI().findFolderByPath(uriParam, host, user, false); if(!UtilMethods.isSet(host)) { @@ -174,7 +176,7 @@ public List findSubFoldersPathByParentPath(final String throws DotSecurityException, DotDataException { final List subFolders = new ArrayList<>(); - if(pathToSearch.lastIndexOf(StringPool.FORWARD_SLASH) == 0){ //If there is only one / we need to search the subfolders under the host(s) + if(pathToSearch.lastIndexOf(FORWARD_SLASH) == 0){ //If there is only one / we need to search the subfolders under the host(s) if(UtilMethods.isSet(siteId)) { subFolders.addAll(findSubfoldersUnderHost(siteId,pathToSearch,user)); } else{ @@ -212,23 +214,24 @@ private List findSubfoldersUnderHost(final String siteId final String pathToSearch, final User user) throws DotSecurityException, DotDataException { final List subFolders = new ArrayList<>(); + final Host site = APILocator.getHostAPI().find(siteId, user, DONT_RESPECT_FRONT_END_ROLES); - final Host host = APILocator.getHostAPI().find(siteId, user, false); - - if(pathToSearch.equals(StringPool.FORWARD_SLASH)) { + if (pathToSearch.equals(FORWARD_SLASH)) { final Folder systemFolder = APILocator.getFolderAPI().findSystemFolder(); - subFolders.add(new FolderSearchResultView(systemFolder.getPath(), host.getHostname(), + subFolders.add(new FolderSearchResultView(systemFolder.getIdentifier(), systemFolder.getInode(), systemFolder.getPath(), site.getHostname(), Try.of(() -> APILocator.getPermissionAPI().doesUserHavePermission(systemFolder, - PermissionAPI.PERMISSION_CAN_ADD_CHILDREN, user)).getOrElse(false))); + PERMISSION_CAN_ADD_CHILDREN, user)).getOrElse(false))); } - final List subFoldersOfRootPath = APILocator.getFolderAPI() - .findSubFolders(host, user, false); - subFoldersOfRootPath.stream().filter(folder -> folder.getPath().toLowerCase().startsWith(pathToSearch)) - .limit(SUB_FOLDER_SIZE_DEFAULT_LIMIT).forEach( - folder -> subFolders.add(new FolderSearchResultView(folder.getPath(), host.getHostname(), - Try.of(() -> APILocator.getPermissionAPI().doesUserHavePermission(folder, - PermissionAPI.PERMISSION_CAN_ADD_CHILDREN,user)).getOrElse(false)))); + final List subFoldersOfRootPath = APILocator.getFolderAPI().findSubFolders(site, user, DONT_RESPECT_FRONT_END_ROLES); + subFoldersOfRootPath.stream() + .filter(folder -> folder.getPath().toLowerCase().startsWith(pathToSearch)) + .limit(SUB_FOLDER_SIZE_DEFAULT_LIMIT) + .forEach(folder -> subFolders + .add(new FolderSearchResultView(folder.getIdentifier(), folder.getInode(), folder.getPath(), site.getHostname(), + Try.of(() -> APILocator.getPermissionAPI() + .doesUserHavePermission(folder, PERMISSION_CAN_ADD_CHILDREN, user)) + .getOrElse(false)))); return subFolders; } @@ -241,31 +244,30 @@ private List findSubfoldersUnderFolder(final String site final String pathToSearch, final User user) throws DotSecurityException, DotDataException { final List subFolders = new ArrayList<>(); - final Host host = APILocator.getHostAPI().find(siteId, user, false); + final Host site = APILocator.getHostAPI().find(siteId, user, DONT_RESPECT_FRONT_END_ROLES); - final int lastIndexOf = pathToSearch.lastIndexOf(StringPool.FORWARD_SLASH); + final int lastIndexOf = pathToSearch.lastIndexOf(FORWARD_SLASH); final String lastValidPath = pathToSearch.substring(0, lastIndexOf); final Folder lastValidFolder = APILocator.getFolderAPI() - .findFolderByPath(lastValidPath, host, user, false); + .findFolderByPath(lastValidPath, site, user, DONT_RESPECT_FRONT_END_ROLES); if (UtilMethods.isSet(lastValidFolder) && UtilMethods .isSet(lastValidFolder.getInode())) { - if(pathToSearch.equals(lastValidPath) || pathToSearch.equals(lastValidPath+"/")) { - subFolders.add(new FolderSearchResultView(lastValidFolder.getPath(), - host.getHostname(), + if (pathToSearch.equals(lastValidPath) || pathToSearch.equals(lastValidPath + FORWARD_SLASH)) { + subFolders.add(new FolderSearchResultView(lastValidFolder.getIdentifier(), lastValidFolder.getInode(), lastValidFolder.getPath(), + site.getHostname(), Try.of(() -> APILocator.getPermissionAPI() - .doesUserHavePermission(lastValidFolder, - PermissionAPI.PERMISSION_CAN_ADD_CHILDREN, user)) + .doesUserHavePermission(lastValidFolder, PERMISSION_CAN_ADD_CHILDREN, user)) .getOrElse(false))); } - final List subFoldersOfLastValidPath = APILocator.getFolderAPI() - .findSubFolders(lastValidFolder, user, false); + final List subFoldersOfLastValidPath = APILocator.getFolderAPI().findSubFolders(lastValidFolder, user, DONT_RESPECT_FRONT_END_ROLES); subFoldersOfLastValidPath.stream() - .filter(folder -> folder.getPath().toLowerCase() - .startsWith(pathToSearch)) - .limit(SUB_FOLDER_SIZE_DEFAULT_LIMIT).forEach(folder -> subFolders - .add(new FolderSearchResultView(folder.getPath(), host.getHostname(), - Try.of(() -> APILocator.getPermissionAPI().doesUserHavePermission(folder, - PermissionAPI.PERMISSION_CAN_ADD_CHILDREN,user)).getOrElse(false)))); + .filter(folder -> folder.getPath().toLowerCase().startsWith(pathToSearch)) + .limit(SUB_FOLDER_SIZE_DEFAULT_LIMIT) + .forEach(folder -> subFolders + .add(new FolderSearchResultView(folder.getIdentifier(), folder.getInode(), folder.getPath(), site.getHostname(), + Try.of(() -> APILocator.getPermissionAPI() + .doesUserHavePermission(folder, PERMISSION_CAN_ADD_CHILDREN, user)) + .getOrElse(false)))); } return subFolders; } diff --git a/dotCMS/src/main/java/com/dotcms/rest/api/v1/folder/FolderSearchResultView.java b/dotCMS/src/main/java/com/dotcms/rest/api/v1/folder/FolderSearchResultView.java index 74631aa7a6d7..2fe989b3f680 100644 --- a/dotCMS/src/main/java/com/dotcms/rest/api/v1/folder/FolderSearchResultView.java +++ b/dotCMS/src/main/java/com/dotcms/rest/api/v1/folder/FolderSearchResultView.java @@ -1,29 +1,47 @@ package com.dotcms.rest.api.v1.folder; /** - * View of the Folder for the move actionlet. + * This class represents the REST View of a Folder. It's used by several Endpoints and related + * classes, such as the {@link FolderResource}, the + * {@link com.dotmarketing.portlets.workflows.actionlet.MoveContentActionlet}, and so on. + * + * @author Jonathan Sanchez + * @since Jul 19th, 2021 */ public class FolderSearchResultView { + private final String id; + private final String inode; private final String path; private final String hostName; + private final boolean addChildrenAllowed; - public FolderSearchResultView(final String path, final String hostname, final boolean addChildrenAllowed) { + public FolderSearchResultView(final String id, final String inode, final String path, + final String hostname, final boolean addChildrenAllowed) { + this.id = id; + this.inode = inode; this.path = path; this.hostName = hostname; this.addChildrenAllowed = addChildrenAllowed; } + public String getId() { + return id; + } + + public String getInode() { + return inode; + } public String getPath() { return path; } - public String getHostName() { return hostName; } public boolean isAddChildrenAllowed(){ return addChildrenAllowed; } + } diff --git a/dotcms-postman/src/main/resources/postman/FolderResource.postman_collection.json b/dotcms-postman/src/main/resources/postman/FolderResource.postman_collection.json index ec00d76bcd10..883097da78b5 100644 --- a/dotcms-postman/src/main/resources/postman/FolderResource.postman_collection.json +++ b/dotcms-postman/src/main/resources/postman/FolderResource.postman_collection.json @@ -1,9 +1,10 @@ { "info": { - "_postman_id": "cf4e4a36-6368-430f-8bc1-7127345700e1", + "_postman_id": "d0b98de2-fe86-455b-aac7-6065ddbc0ad8", "name": "FolderResource", "description": "Test for FolderResource", - "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "5403727" }, "item": [ { @@ -45,26 +46,12 @@ "", "" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "POST", "header": [], "body": { @@ -102,26 +89,12 @@ " pm.response.to.have.status(200);", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "POST", "header": [], "body": { @@ -156,11 +129,12 @@ "listen": "test", "script": { "exec": [ - "pm.test(\"Status code should be 200\", function () {", + "pm.test(\"HTTP Status code must be successful\", function () {", " pm.response.to.have.status(200);", "});", "", - "var jsonData = pm.response.json();", + "const jsonData = pm.response.json();", + "const entity = jsonData.entity;", "", "pm.test(\"Folder Check\", function () {", " pm.expect(jsonData.entity[1].path).to.eql('/testfindfolder1/');", @@ -168,9 +142,21 @@ "", "pm.test(\"Length Check\", function () {", " pm.expect(jsonData.entity.length).to.eql(4);", - "});" + "});", + "", + "pm.test(\"Checking that all expected folder attributes are present\", function () {", + " entity.forEach(function(folderEntry) {", + " pm.expect(folderEntry.addChildrenAllowed).to.not.equal(undefined);", + " pm.expect(folderEntry.hostName).to.not.equal(undefined);", + " pm.expect(folderEntry.id).to.not.equal(undefined);", + " pm.expect(folderEntry.inode).to.not.equal(undefined);", + " pm.expect(folderEntry.path).to.not.equal(undefined);", + " });", + "});", + "" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], @@ -606,35 +592,12 @@ " pm.response.to.have.status(400);", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - }, - { - "key": "saveHelperData", - "type": "any" - }, - { - "key": "showPassword", - "value": false, - "type": "boolean" - } - ] - }, "method": "POST", "header": [], "url": { @@ -672,26 +635,12 @@ " pm.collectionVariables.set(\"folderId\", jsonData[\"entity\"][0][\"identifier\"]);", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "POST", "header": [], "body": { @@ -788,26 +737,12 @@ "", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "PUT", "header": [], "url": { @@ -838,7 +773,8 @@ "", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], @@ -847,13 +783,13 @@ "type": "basic", "basic": [ { - "key": "password", - "value": "admin", + "key": "username", + "value": "admin@dotcms.com", "type": "string" }, { - "key": "username", - "value": "admin@dotcms.com", + "key": "password", + "value": "admin", "type": "string" } ] @@ -887,26 +823,12 @@ " pm.response.to.have.status(200);", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "POST", "header": [], "body": { @@ -947,26 +869,12 @@ " pm.response.to.have.status(200);", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "GET", "header": [], "url": { @@ -999,26 +907,12 @@ " pm.response.to.have.status(400);", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "POST", "header": [], "body": { @@ -1057,26 +951,12 @@ " pm.response.to.have.status(400);", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "POST", "header": [], "body": { @@ -1115,26 +995,12 @@ " pm.response.to.have.status(400);", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "GET", "header": [], "url": { @@ -1168,26 +1034,12 @@ " pm.response.to.have.status(400);", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "GET", "header": [], "url": { @@ -1223,26 +1075,12 @@ " pm.expect(pm.response.text()).to.include(\"subfolder1\");", "});" ], - "type": "text/javascript" + "type": "text/javascript", + "packages": {} } } ], "request": { - "auth": { - "type": "basic", - "basic": [ - { - "key": "password", - "value": "admin", - "type": "string" - }, - { - "key": "username", - "value": "admin@dotcms.com", - "type": "string" - } - ] - }, "method": "GET", "header": [], "url": { @@ -1265,14 +1103,76 @@ "response": [] } ], - "variable": [ + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{jwt}}", + "type": "string" + } + ] + }, + "event": [ { - "key": "siteId", - "value": "" + "listen": "prerequest", + "script": { + "type": "text/javascript", + "packages": {}, + "exec": [ + "if (!pm.environment.get('jwt')) {", + " console.log(\"generating....\")", + " const serverURL = pm.environment.get('serverURL'); // Get the server URL from the environment variable", + " const apiUrl = `${serverURL}/api/v1/apitoken`; // Construct the full API URL", + "", + " if (!pm.environment.get('jwt')) {", + " const username = pm.environment.get(\"user\");", + " const password = pm.environment.get(\"password\");", + " const basicAuth = Buffer.from(`${username}:${password}`).toString('base64');", + "", + " const requestOptions = {", + " url: apiUrl,", + " method: \"POST\",", + " header: {", + " \"accept\": \"*/*\",", + " \"content-type\": \"application/json\",", + " \"Authorization\": `Basic ${basicAuth}`", + " },", + " body: {", + " mode: \"raw\",", + " raw: JSON.stringify({", + " \"expirationSeconds\": 7200,", + " \"userId\": \"dotcms.org.1\",", + " \"network\": \"0.0.0.0/0\",", + " \"claims\": {\"label\": \"postman-tests\"}", + " })", + " }", + " };", + "", + " pm.sendRequest(requestOptions, function (err, response) {", + " if (err) {", + " console.log(err);", + " } else {", + " const jwt = response.json().entity.jwt;", + " pm.environment.set('jwt', jwt);", + " console.log(jwt);", + " }", + " });", + " }", + "}", + "" + ] + } }, { - "key": "folderId", - "value": "25e35d3f515876f8c76524065ca00a9f" + "listen": "test", + "script": { + "type": "text/javascript", + "packages": {}, + "exec": [ + "" + ] + } } ] -} +} \ No newline at end of file