2018-06-29 19:52:40 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2020-06-08 13:14:23 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
2020-06-09 13:01:05 +10:00
|
|
|
|
using Umbraco.Core;
|
2020-06-08 13:14:23 +02:00
|
|
|
|
using Umbraco.Core.Mapping;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using Umbraco.Core.Models;
|
2020-09-22 10:01:00 +02:00
|
|
|
|
using Umbraco.Core.Security;
|
2019-01-17 13:20:19 +11:00
|
|
|
|
using Umbraco.Core.Services;
|
2020-06-08 13:14:23 +02:00
|
|
|
|
using Umbraco.Web.BackOffice.Controllers;
|
|
|
|
|
|
using Umbraco.Web.Common.Attributes;
|
|
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2018-10-16 16:41:06 +11:00
|
|
|
|
using Umbraco.Web.Models.Trees;
|
2020-06-08 13:14:23 +02:00
|
|
|
|
using Umbraco.Web.Security;
|
2019-01-17 13:20:19 +11:00
|
|
|
|
using Umbraco.Web.Services;
|
2020-06-08 13:14:23 +02:00
|
|
|
|
using Umbraco.Web.Trees;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Editors
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2020-06-08 13:14:23 +02:00
|
|
|
|
/// The API controller used for using the list of sections
|
2018-06-29 19:52:40 +02:00
|
|
|
|
/// </summary>
|
2020-06-09 13:01:05 +10:00
|
|
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
2018-06-29 19:52:40 +02:00
|
|
|
|
public class SectionController : UmbracoAuthorizedJsonController
|
|
|
|
|
|
{
|
2020-06-08 13:14:23 +02:00
|
|
|
|
private readonly IControllerFactory _controllerFactory;
|
2019-01-23 14:37:33 +00:00
|
|
|
|
private readonly IDashboardService _dashboardService;
|
2020-06-08 13:14:23 +02:00
|
|
|
|
private readonly ILocalizedTextService _localizedTextService;
|
2019-01-17 13:20:19 +11:00
|
|
|
|
private readonly ISectionService _sectionService;
|
2019-01-17 16:40:11 +11:00
|
|
|
|
private readonly ITreeService _treeService;
|
2020-06-08 13:14:23 +02:00
|
|
|
|
private readonly UmbracoMapper _umbracoMapper;
|
2020-09-22 10:01:00 +02:00
|
|
|
|
private readonly IBackofficeSecurityAccessor _backofficeSecurityAccessor;
|
2018-12-05 09:05:47 +01:00
|
|
|
|
|
2020-06-08 13:14:23 +02:00
|
|
|
|
public SectionController(
|
2020-09-22 10:01:00 +02:00
|
|
|
|
IBackofficeSecurityAccessor backofficeSecurityAccessor,
|
2020-06-08 13:14:23 +02:00
|
|
|
|
ILocalizedTextService localizedTextService,
|
|
|
|
|
|
IDashboardService dashboardService, ISectionService sectionService, ITreeService treeService,
|
|
|
|
|
|
UmbracoMapper umbracoMapper, IControllerFactory controllerFactory)
|
2018-12-05 09:05:47 +01:00
|
|
|
|
{
|
2020-09-22 10:01:00 +02:00
|
|
|
|
_backofficeSecurityAccessor = backofficeSecurityAccessor;
|
2020-06-08 13:14:23 +02:00
|
|
|
|
_localizedTextService = localizedTextService;
|
2019-01-23 14:37:33 +00:00
|
|
|
|
_dashboardService = dashboardService;
|
2019-01-17 13:20:19 +11:00
|
|
|
|
_sectionService = sectionService;
|
|
|
|
|
|
_treeService = treeService;
|
2020-06-08 13:14:23 +02:00
|
|
|
|
_umbracoMapper = umbracoMapper;
|
|
|
|
|
|
_controllerFactory = controllerFactory;
|
2018-12-05 09:05:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
public IEnumerable<Section> GetSections()
|
|
|
|
|
|
{
|
2020-09-22 10:01:00 +02:00
|
|
|
|
var sections = _sectionService.GetAllowedSections(_backofficeSecurityAccessor.BackofficeSecurity.GetUserId().ResultOr(0));
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-06-08 13:14:23 +02:00
|
|
|
|
var sectionModels = sections.Select(_umbracoMapper.Map<Section>).ToArray();
|
2019-02-14 12:40:45 +01:00
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
// this is a bit nasty since we'll be proxying via the app tree controller but we sort of have to do that
|
2018-07-21 10:47:29 +02:00
|
|
|
|
// since tree's by nature are controllers and require request contextual data
|
2020-06-08 13:14:23 +02:00
|
|
|
|
var appTreeController =
|
|
|
|
|
|
new ApplicationTreeController(_treeService, _sectionService, _localizedTextService, _controllerFactory)
|
|
|
|
|
|
{
|
|
|
|
|
|
ControllerContext = ControllerContext
|
|
|
|
|
|
};
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-09-22 10:01:00 +02:00
|
|
|
|
var dashboards = _dashboardService.GetDashboards(_backofficeSecurityAccessor.BackofficeSecurity.CurrentUser);
|
2018-12-05 09:05:47 +01:00
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
//now we can add metadata for each section so that the UI knows if there's actually anything at all to render for
|
|
|
|
|
|
//a dashboard for a given section, then the UI can deal with it accordingly (i.e. redirect to the first tree)
|
|
|
|
|
|
foreach (var section in sectionModels)
|
|
|
|
|
|
{
|
2020-06-08 13:14:23 +02:00
|
|
|
|
var hasDashboards = dashboards.TryGetValue(section.Alias, out var dashboardsForSection) &&
|
|
|
|
|
|
dashboardsForSection.Any();
|
2018-12-05 09:05:47 +01:00
|
|
|
|
if (hasDashboards) continue;
|
|
|
|
|
|
|
|
|
|
|
|
// get the first tree in the section and get its root node route path
|
|
|
|
|
|
var sectionRoot = appTreeController.GetApplicationTrees(section.Alias, null, null).Result;
|
|
|
|
|
|
section.RoutePath = GetRoutePathForFirstTree(sectionRoot);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return sectionModels;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 16:41:06 +11:00
|
|
|
|
/// <summary>
|
2020-06-08 13:14:23 +02:00
|
|
|
|
/// Returns the first non root/group node's route path
|
2018-10-16 16:41:06 +11:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="rootNode"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private string GetRoutePathForFirstTree(TreeRootNode rootNode)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!rootNode.IsContainer || !rootNode.ContainsTrees)
|
|
|
|
|
|
return rootNode.RoutePath;
|
|
|
|
|
|
|
2020-06-08 13:14:23 +02:00
|
|
|
|
foreach (var node in rootNode.Children)
|
2018-10-16 16:41:06 +11:00
|
|
|
|
{
|
|
|
|
|
|
if (node is TreeRootNode groupRoot)
|
2020-06-08 13:14:23 +02:00
|
|
|
|
return GetRoutePathForFirstTree(groupRoot); //recurse to get the first tree in the group
|
|
|
|
|
|
return node.RoutePath;
|
2018-10-16 16:41:06 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
/// <summary>
|
2020-06-08 13:14:23 +02:00
|
|
|
|
/// Returns all the sections that the user has access to
|
2018-06-29 19:52:40 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public IEnumerable<Section> GetAllSections()
|
|
|
|
|
|
{
|
2019-01-17 13:20:19 +11:00
|
|
|
|
var sections = _sectionService.GetSections();
|
2020-06-08 13:14:23 +02:00
|
|
|
|
var mapped = sections.Select(_umbracoMapper.Map<Section>);
|
2020-09-22 10:01:00 +02:00
|
|
|
|
if (_backofficeSecurityAccessor.BackofficeSecurity.CurrentUser.IsAdmin())
|
2018-06-29 19:52:40 +02:00
|
|
|
|
return mapped;
|
|
|
|
|
|
|
2020-09-22 10:01:00 +02:00
|
|
|
|
return mapped.Where(x => _backofficeSecurityAccessor.BackofficeSecurity.CurrentUser.AllowedSections.Contains(x.Alias)).ToArray();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|