Skip to content

Commit

Permalink
#24644 : Clicking a Container as File is not taking you to the right …
Browse files Browse the repository at this point in the history
…location in the Site Browser.
  • Loading branch information
jcastro-dotcms committed Apr 17, 2023
1 parent 09201cc commit 954cbd6
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 190 deletions.
@@ -1,11 +1,11 @@
package com.dotcms.rest.api.v1.browsertree;

import com.dotcms.rest.ResponseEntityView;
import com.dotcms.util.TreeableNameComparator;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.Permissionable;
import com.dotmarketing.business.Treeable;
import com.dotmarketing.business.UserAPI;
import com.dotcms.util.TreeableNameComparator;
import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
Expand All @@ -15,13 +15,12 @@
import com.dotmarketing.portlets.folders.model.Folder;
import com.dotmarketing.util.HostUtil;
import com.dotmarketing.util.UtilMethods;
import com.dotmarketing.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.util.StringPool;
import io.vavr.control.Try;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -36,6 +35,9 @@ public class BrowserTreeHelper {
private final FolderAPI folderAPI;
private final UserAPI userAPI;

public static final String OPEN_FOLDER_IDS = "siteBrowserOpenFolderIds";
public static final String ACTIVE_FOLDER_ID = "siteBrowserActiveFolderInode";

private final static TreeableNameComparator TREEABLE_NAME_COMPARATOR =
new TreeableNameComparator();

Expand Down Expand Up @@ -140,34 +142,34 @@ private String getFolderPath(final String folderPath) {
* @throws DotDataException
* @throws DotSecurityException
*/
public void selectFolder (final HttpServletRequest request, final String fullFolderPath,
final User user, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException {

final Optional<Host> hostOpt = findHostFromPath(fullFolderPath, user);
final Host host = hostOpt.isPresent()? hostOpt.get(): APILocator.getHostAPI().findDefaultHost(user, respectFrontendRoles);
final Host currentHost = WebAPILocator.getHostWebAPI().getCurrentHostNoThrow(request);
final String folderPath = getFolderPath(fullFolderPath);
final Folder folder = APILocator.getFolderAPI().findFolderByPath(folderPath, host, user, respectFrontendRoles);

if (null != folder && UtilMethods.isSet(folder.getIdentifier()) && null != host) {

if (null == currentHost || !host.getIdentifier().equals(currentHost.getIdentifier())) {

HostUtil.switchSite(request, host);
}

BrowserAjax browserAjax = (BrowserAjax)request.getSession().getAttribute("BrowserAjax");
if (null == browserAjax) {

browserAjax = new BrowserAjax();
request.getSession().setAttribute("BrowserAjax", browserAjax);
public void selectFolder(final HttpServletRequest request, final String fullFolderPath, final User user,
final boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
final Optional<Host> siteOpt = this.findHostFromPath(fullFolderPath, user);
final Host site = siteOpt.isPresent()? siteOpt.get(): this.hostAPI.findDefaultHost(user, respectFrontendRoles);
final Host currentSite = WebAPILocator.getHostWebAPI().getCurrentHostNoThrow(request);
final String folderPath = this.getFolderPath(fullFolderPath);
final Folder folder = this.folderAPI.findFolderByPath(folderPath, site, user, respectFrontendRoles);
if (null == folder || !UtilMethods.isSet(folder.getIdentifier()) || null == site || !UtilMethods.isSet(site.getIdentifier())) {
throw new IllegalArgumentException(String.format("Path '%s' is pointing to an invalid Site or an invalid " +
"Folder path", fullFolderPath));
}
if (null == currentSite || !site.getIdentifier().equals(currentSite.getIdentifier())) {
HostUtil.switchSite(request, site);
}
final HttpSession session = request.getSession();
session.setAttribute(ACTIVE_FOLDER_ID, folder.getInode());
final Folder leafFolder = this.folderAPI.find(folder.getInode(), user, false);
if (null != leafFolder && UtilMethods.isSet(leafFolder.getIdentifier())) {
final List<String> openFolderIds = new ArrayList<>();
openFolderIds.add(folder.getInode());
session.setAttribute(OPEN_FOLDER_IDS, openFolderIds);
Permissionable parent = leafFolder.getParentPermissionable();
while (parent != null) {
if (parent instanceof Folder) {
openFolderIds.add(((Folder) parent).getIdentifier());
}
parent = parent.getParentPermissionable();
}

browserAjax.setCurrentOpenFolder(folder.getInode(), host.getIdentifier(), user);
request.getSession().setAttribute("siteBrowserActiveFolderInode", folder.getInode());
} else {

throw new IllegalArgumentException("The path seems to be not valid: " + fullFolderPath);
}
}
}
62 changes: 62 additions & 0 deletions dotCMS/src/main/java/com/dotmarketing/business/ajax/DwrUtil.java
Expand Up @@ -3,14 +3,24 @@
import com.dotcms.repackage.org.directwebremoting.WebContext;
import com.dotcms.repackage.org.directwebremoting.WebContextFactory;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.PermissionAPI;
import com.dotmarketing.business.Permissionable;
import com.dotmarketing.business.Role;
import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.browser.ajax.BrowserAjax;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.SecurityLogger;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.liferay.portal.model.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
* @author Jonathan Gamba 11/29/17
Expand Down Expand Up @@ -94,4 +104,56 @@ public static User getLoggedInUser()
return loggedInUser;
}

/**
* Returns the Roles that are assigned to a given user in the form of an array.
*
* @param user The {@link User} whose Roles are being retrieved.
*
* @return The array or {@link Role} objects.
*/
public static Role[] getUserRoles(final User user) {
Role[] roles = new Role[]{};
try {
roles = com.dotmarketing.business.APILocator.getRoleAPI().loadRolesForUser(user.getUserId()).toArray(new Role[0]);
} catch (final DotDataException e) {
Logger.error(BrowserAjax.class, String.format("Could not get Roles for User '%s': %s", user.getUserId(),
e.getMessage()), e);
}
return roles;
}

/**
* Returns the list of permissions that a given User has on a given Permissionable object, based on an array of
* specific Roles. The Permissionable object can be a Site or a Folder.
*
* @param permissionable The {@link Permissionable} whose User permissions will be checked.
* @param roles The array of {@link Role} objects that will be used to check the User permissions.
* @param user The {@link User} whose permissions will be checked.
*
* @return The {@link Optional} containing the list of permissions that the User has on the Permissionable object.
*/
public static Optional<List<Integer>> getPermissions(final Permissionable permissionable, final Role[] roles, final User user) {
final PermissionAPI permissionAPI = APILocator.getPermissionAPI();
try {
final List<Integer> permissions = permissionAPI.getPermissionIdsFromRoles(permissionable, roles, user);
return Optional.of(permissions);
} catch (final DotDataException e) {
Logger.error(DwrUtil.class, String.format("Failed to retrieve permissions of User '%s' on parent '%s' for" +
" Roles [ %s ]: %s", user.getUserId(),
permissionable.getPermissionId(), Arrays.toString(roles), e.getMessage()), e);
return Optional.empty();
}
}

/**
* Returns the current HTTP Session object from the Web Context Factory.
*
* @return The {@link HttpSession} object.
*/
public static HttpSession getSession() {
final WebContext ctx = WebContextFactory.get();
final HttpServletRequest request = ctx.getHttpServletRequest();
return request.getSession();
}

}

0 comments on commit 954cbd6

Please sign in to comment.