Skip to content

Commit

Permalink
chore(Edit Mode) fixes #30699 : Add the folder ID to the ``/api/v1/fo…
Browse files Browse the repository at this point in the history
…lder/byPath` Endpoint
  • Loading branch information
jcastro-dotcms committed Nov 20, 2024
1 parent 3c2c587 commit 0fe3c51
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -174,7 +176,7 @@ public List<FolderSearchResultView> findSubFoldersPathByParentPath(final String
throws DotSecurityException, DotDataException {
final List<FolderSearchResultView> 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{
Expand Down Expand Up @@ -212,23 +214,24 @@ private List<FolderSearchResultView> findSubfoldersUnderHost(final String siteId
final String pathToSearch, final User user)
throws DotSecurityException, DotDataException {
final List<FolderSearchResultView> 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<Folder> 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<Folder> 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;
}
Expand All @@ -241,31 +244,30 @@ private List<FolderSearchResultView> findSubfoldersUnderFolder(final String site
final String pathToSearch, final User user)
throws DotSecurityException, DotDataException {
final List<FolderSearchResultView> 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<Folder> subFoldersOfLastValidPath = APILocator.getFolderAPI()
.findSubFolders(lastValidFolder, user, false);
final List<Folder> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }

}
Loading

0 comments on commit 0fe3c51

Please sign in to comment.