using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Net; using System.Net.Http.Formatting; using System.Threading.Tasks; using System.Web.Http; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.ContentEditing; using Umbraco.Core.Persistence; using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Models.Trees; using Umbraco.Web.Mvc; using Umbraco.Web.Services; using Umbraco.Web.WebApi; using Umbraco.Web.WebApi.Filters; using Constants = Umbraco.Core.Constants; namespace Umbraco.Web.Trees { [AngularJsonOnlyConfiguration] [PluginController("UmbracoTrees")] public class ApplicationTreeController : UmbracoAuthorizedApiController { private readonly TreeControllerResolver _treeControllerResolver; private readonly IApplicationTreeService _treeService; public ApplicationTreeController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, IProfilingLogger logger, IRuntimeState runtimeState, TreeControllerResolver treeControllerResolver, IApplicationTreeService treeService) : base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, runtimeState) { _treeControllerResolver = treeControllerResolver; _treeService = treeService; } /// /// Returns the tree nodes for an application /// /// The application to load tree for /// An optional single tree alias, if specified will only load the single tree for the request app /// /// An optional bool (defaults to true), if set to false it will also load uninitialized trees /// [HttpQueryStringFilter("queryStrings")] public async Task GetApplicationTrees(string application, string tree, FormDataCollection queryStrings/*, bool onlyInitialized = true*/) { application = application.CleanForXss(); if (string.IsNullOrEmpty(application)) throw new HttpResponseException(HttpStatusCode.NotFound); //find all tree definitions that have the current application alias var groupedTrees = _treeService.GetGroupedApplicationTrees(application); var allTrees = groupedTrees.Values.SelectMany(x => x).ToList(); if (string.IsNullOrEmpty(tree) == false || allTrees.Count == 1) { var apptree = !tree.IsNullOrWhiteSpace() ? allTrees.FirstOrDefault(x => x.Alias == tree) : allTrees.FirstOrDefault(); if (apptree == null) throw new HttpResponseException(HttpStatusCode.NotFound); var result = await GetRootForSingleAppTree( apptree, Constants.System.Root.ToString(CultureInfo.InvariantCulture), queryStrings, application); //this will be null if it cannot convert to a single root section if (result != null) { return result; } } //Don't apply fancy grouping logic futher down, if we only have one group of items var hasGroups = groupedTrees.Count > 1; if (!hasGroups) { var collection = new TreeNodeCollection(); foreach (var apptree in allTrees) { //return the root nodes for each tree in the app var rootNode = await GetRootForMultipleAppTree(apptree, queryStrings); //This could be null if the tree decides not to return it's root (i.e. the member type tree does this when not in umbraco membership mode) if (rootNode != null) { collection.Add(rootNode); } } if(collection.Count > 0) { var multiTree = TreeRootNode.CreateMultiTreeRoot(collection); multiTree.Name = Services.TextService.Localize("sections/" + application); return multiTree; } //Otherwise its a application/section with no trees (aka a full screen app) //For example we do not have a Forms tree definied in C# & can not attribute with [Tree(isSingleNodeTree:true0] var rootId = Constants.System.Root.ToString(CultureInfo.InvariantCulture); var section = Services.TextService.Localize("sections/" + application); return TreeRootNode.CreateSingleTreeRoot(rootId, null, null, section, TreeNodeCollection.Empty, true); } var rootNodeGroups = new List(); //Group trees by [CoreTree] attribute with a TreeGroup property foreach (var treeSectionGroup in groupedTrees) { var treeGroupName = treeSectionGroup.Key; var groupNodeCollection = new TreeNodeCollection(); foreach (var appTree in treeSectionGroup.Value) { var rootNode = await GetRootForMultipleAppTree(appTree, queryStrings); if (rootNode != null) { //Add to a new list/collection groupNodeCollection.Add(rootNode); } } //If treeGroupName == null then its third party if (treeGroupName.IsNullOrWhiteSpace()) { //This is used for the localisation key //treeHeaders/thirdPartyGroup treeGroupName = "thirdPartyGroup"; } if (groupNodeCollection.Count > 0) { var groupRoot = TreeRootNode.CreateGroupNode(groupNodeCollection, application); groupRoot.Name = Services.TextService.Localize("treeHeaders/" + treeGroupName); rootNodeGroups.Add(groupRoot); } } return TreeRootNode.CreateGroupedMultiTreeRoot(new TreeNodeCollection(rootNodeGroups.OrderBy(x => x.Name))); } /// /// Get the root node for an application with multiple trees /// /// /// /// private async Task GetRootForMultipleAppTree(ApplicationTree configTree, FormDataCollection queryStrings) { if (configTree == null) throw new ArgumentNullException(nameof(configTree)); try { var byControllerAttempt = await _treeControllerResolver.TryGetRootNodeFromControllerTree(configTree, queryStrings, ControllerContext); if (byControllerAttempt.Success) { return byControllerAttempt.Result; } } catch (HttpResponseException) { //if this occurs its because the user isn't authorized to view that tree, in this case since we are loading multiple trees we //will just return null so that it's not added to the list. return null; } throw new ApplicationException("Could not get root node for tree type " + configTree.Alias); } /// /// Get the root node for an application with one tree /// /// /// /// /// /// private async Task GetRootForSingleAppTree(ApplicationTree configTree, string id, FormDataCollection queryStrings, string application) { var rootId = Constants.System.Root.ToString(CultureInfo.InvariantCulture); if (configTree == null) throw new ArgumentNullException(nameof(configTree)); var byControllerAttempt = _treeControllerResolver.TryLoadFromControllerTree(configTree, id, queryStrings, ControllerContext); if (byControllerAttempt.Success) { var rootNode = await _treeControllerResolver.TryGetRootNodeFromControllerTree(configTree, queryStrings, ControllerContext); if (rootNode.Success == false) { //This should really never happen if we've successfully got the children above. throw new InvalidOperationException("Could not create root node for tree " + configTree.Alias); } var treeAttribute = _treeControllerResolver.GetTreeAttribute(configTree); var sectionRoot = TreeRootNode.CreateSingleTreeRoot( rootId, rootNode.Result.ChildNodesUrl, rootNode.Result.MenuUrl, rootNode.Result.Name, byControllerAttempt.Result, treeAttribute.IsSingleNodeTree); //assign the route path based on the root node, this means it will route there when the section is navigated to //and no dashboards will be available for this section sectionRoot.RoutePath = rootNode.Result.RoutePath; foreach (var d in rootNode.Result.AdditionalData) { sectionRoot.AdditionalData[d.Key] = d.Value; } return sectionRoot; } throw new ApplicationException("Could not render a tree for type " + configTree.Alias); } } }