Files
Umbraco-CMS/src/Umbraco.Web/Trees/ContentTreeController.cs

247 lines
10 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Net;
using System.Net.Http.Formatting;
using System.Web.Http;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Services;
2017-05-30 18:13:11 +02:00
using Umbraco.Web.Composing;
using Umbraco.Web.Models.Trees;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi.Filters;
using Umbraco.Web._Legacy.Actions;
using Constants = Umbraco.Core.Constants;
namespace Umbraco.Web.Trees
{
//We will not allow the tree to render unless the user has access to any of the sections that the tree gets rendered
// this is not ideal but until we change permissions to be tree based (not section) there's not much else we can do here.
[UmbracoApplicationAuthorize(
2016-07-29 15:48:32 +02:00
Constants.Applications.Content,
Constants.Applications.Media,
Constants.Applications.Users,
Constants.Applications.Settings,
Constants.Applications.Developer,
Constants.Applications.Members)]
[Tree(Constants.Applications.Content, Constants.Trees.Content)]
[PluginController("UmbracoTrees")]
[CoreTree]
public class ContentTreeController : ContentTreeControllerBase
{
protected override TreeNode CreateRootNode(FormDataCollection queryStrings)
{
2016-07-29 15:48:32 +02:00
var node = base.CreateRootNode(queryStrings);
//if the user's start node is not default, then ensure the root doesn't have a menu
if (Security.CurrentUser.StartContentId != Constants.System.Root)
{
node.MenuUrl = "";
}
2016-03-15 16:39:17 +01:00
node.Name = Services.TextService.Localize("sections/"+ Constants.Trees.Content);
return node;
}
2017-05-12 14:49:44 +02:00
protected override int RecycleBinId => Constants.System.RecycleBinContent;
2017-05-12 14:49:44 +02:00
protected override bool RecycleBinSmells => Services.ContentService.RecycleBinSmells();
2017-05-12 14:49:44 +02:00
protected override int UserStartNode => Security.CurrentUser.StartContentId;
2016-07-29 15:48:32 +02:00
/// <summary>
/// Creates a tree node for a content item based on an UmbracoEntity
/// </summary>
/// <param name="e"></param>
/// <param name="parentId"></param>
/// <param name="queryStrings"></param>
/// <returns></returns>
protected override TreeNode GetSingleTreeNode(IUmbracoEntity e, string parentId, FormDataCollection queryStrings)
{
var entity = (UmbracoEntity) e;
var allowedUserOptions = GetAllowedUserMenuItemsForNode(e);
if (CanUserAccessNode(e, allowedUserOptions))
{
//Special check to see if it ia a container, if so then we'll hide children.
var isContainer = e.IsContainer(); // && (queryStrings.Get("isDialog") != "true");
var node = CreateTreeNode(
2017-05-12 14:49:44 +02:00
entity,
Constants.ObjectTypes.DocumentGuid,
parentId,
queryStrings,
2017-05-12 14:49:44 +02:00
entity.HasChildren && isContainer == false);
node.AdditionalData.Add("contentType", entity.ContentTypeAlias);
if (isContainer)
{
node.AdditionalData.Add("isContainer", true);
node.SetContainerStyle();
}
2016-07-29 15:48:32 +02:00
if (entity.IsPublished == false)
node.SetNotPublishedStyle();
if (entity.HasPendingChanges)
node.SetHasUnpublishedVersionStyle();
if (Services.PublicAccessService.IsProtected(e.Path))
node.SetProtectedStyle();
return node;
}
return null;
}
protected override MenuItemCollection PerformGetMenuForNode(string id, FormDataCollection queryStrings)
{
if (id == Constants.System.Root.ToInvariantString())
{
var menu = new MenuItemCollection();
//if the user's start node is not the root then ensure the root menu is empty/doesn't exist
if (Security.CurrentUser.StartContentId != Constants.System.Root)
{
return menu;
}
//set the default to create
menu.DefaultMenuAlias = ActionNew.Instance.Alias;
// we need to get the default permissions as you can't set permissions on the very root node
var permission = Services.UserService.GetPermissions(Security.CurrentUser, Constants.System.Root).First();
var nodeActions = global::Umbraco.Web._Legacy.Actions.Action.FromEntityPermission(permission)
.Select(x => new MenuItem(x));
//these two are the standard items
2016-03-15 16:58:13 +01:00
menu.Items.Add<ActionNew>(Services.TextService.Localize("actions", ActionNew.Instance.Alias));
menu.Items.Add<ActionSort>(Services.TextService.Localize("actions", ActionSort.Instance.Alias), true).ConvertLegacyMenuItem(null, "content", "content");
//filter the standard items
FilterUserAllowedMenuItems(menu, nodeActions);
if (menu.Items.Any())
{
menu.Items.Last().SeperatorBefore = true;
}
// add default actions for *all* users
2016-03-15 16:58:13 +01:00
menu.Items.Add<ActionRePublish>(Services.TextService.Localize("actions", ActionRePublish.Instance.Alias)).ConvertLegacyMenuItem(null, "content", "content");
menu.Items.Add<RefreshNode, ActionRefresh>(Services.TextService.Localize("actions", ActionRefresh.Instance.Alias), true);
2016-07-29 15:48:32 +02:00
return menu;
}
//return a normal node menu:
int iid;
if (int.TryParse(id, out iid) == false)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
var item = Services.EntityService.Get(iid, UmbracoObjectTypes.Document);
if (item == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
var nodeMenu = GetAllNodeMenuItems(item);
var allowedMenuItems = GetAllowedUserMenuItemsForNode(item);
2016-07-29 15:48:32 +02:00
FilterUserAllowedMenuItems(nodeMenu, allowedMenuItems);
//if the media item is in the recycle bin, don't have a default menu, just show the regular menu
if (item.Path.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Contains(RecycleBinId.ToInvariantString()))
{
nodeMenu.DefaultMenuAlias = null;
2016-03-15 16:58:13 +01:00
nodeMenu.Items.Insert(2, new MenuItem(ActionRestore.Instance, Services.TextService.Localize("actions", ActionRestore.Instance.Alias)));
}
else
{
//set the default to create
2016-07-29 15:48:32 +02:00
nodeMenu.DefaultMenuAlias = ActionNew.Instance.Alias;
}
2016-07-29 15:48:32 +02:00
return nodeMenu;
}
protected override UmbracoObjectTypes UmbracoObjectType
{
get { return UmbracoObjectTypes.Document; }
}
/// <summary>
/// Returns true or false if the current user has access to the node based on the user's allowed start node (path) access
/// </summary>
/// <param name="id"></param>
/// <param name="queryStrings"></param>
/// <returns></returns>
protected override bool HasPathAccess(string id, FormDataCollection queryStrings)
{
2017-05-12 14:49:44 +02:00
var entity = GetEntityFromId(id);
if (entity == null)
return false;
var content = Services.ContentService.GetById(entity.Id);
if (content == null)
return false;
2017-05-12 14:49:44 +02:00
return Security.CurrentUser.HasPathAccess(content);
}
/// <summary>
/// Returns a collection of all menu items that can be on a content node
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
protected MenuItemCollection GetAllNodeMenuItems(IUmbracoEntity item)
{
var menu = new MenuItemCollection();
2016-07-29 15:48:32 +02:00
AddActionNode<ActionNew>(item, menu);
AddActionNode<ActionDelete>(item, menu);
//need to ensure some of these are converted to the legacy system - until we upgrade them all to be angularized.
2016-07-29 15:48:32 +02:00
AddActionNode<ActionMove>(item, menu, true);
AddActionNode<ActionCopy>(item, menu);
AddActionNode<ActionChangeDocType>(item, menu, convert: true);
2016-03-15 16:58:13 +01:00
2016-07-29 15:48:32 +02:00
AddActionNode<ActionSort>(item, menu, true, true);
2016-03-15 16:58:13 +01:00
2016-07-29 15:48:32 +02:00
AddActionNode<ActionRollback>(item, menu, convert: true);
AddActionNode<ActionAudit>(item, menu, convert: true);
AddActionNode<ActionPublish>(item, menu, true, true);
AddActionNode<ActionToPublish>(item, menu, convert: true);
AddActionNode<ActionAssignDomain>(item, menu, convert: true);
AddActionNode<ActionRights>(item, menu, convert: true);
AddActionNode<ActionProtect>(item, menu, true, true);
2016-07-29 15:48:32 +02:00
AddActionNode<ActionNotify>(item, menu, true, true);
AddActionNode<ActionSendToTranslate>(item, menu, convert: true);
AddActionNode<RefreshNode, ActionRefresh>(item, menu, true);
return menu;
}
2016-07-29 15:48:32 +02:00
private void AddActionNode<TAction>(IUmbracoEntity item, MenuItemCollection menu, bool hasSeparator = false, bool convert = false)
where TAction : IAction
{
var menuItem = menu.Items.Add<TAction>(Services.TextService.Localize("actions", Current.Actions.GetAction<TAction>().Alias), hasSeparator);
if (convert) menuItem.ConvertLegacyMenuItem(item, "content", "content");
}
private void AddActionNode<TItem, TAction>(IUmbracoEntity item, MenuItemCollection menu, bool hasSeparator = false, bool convert = false)
where TItem : MenuItem, new()
where TAction : IAction
{
var menuItem = menu.Items.Add<TItem, TAction>(Services.TextService.Localize("actions", Current.Actions.GetAction<TAction>().Alias), hasSeparator);
if (convert) menuItem.ConvertLegacyMenuItem(item, "content", "content");
}
}
2017-07-20 11:21:28 +02:00
}