Start work on controllers

This commit is contained in:
Nikolaj Geisle
2022-04-01 11:09:51 +02:00
parent 0fc310cc4e
commit 1a6f0e4d7b
71 changed files with 665 additions and 543 deletions

View File

@@ -7,6 +7,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Umbraco.Cms.Core.Collections;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Models.Trees;
@@ -53,9 +54,9 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
public async Task<ActionResult<IEnumerable<Section>>> GetSections()
{
var sections = _sectionService.GetAllowedSections(_backofficeSecurityAccessor.BackOfficeSecurity.GetUserId().Result ?? 0);
var sections = _sectionService.GetAllowedSections(_backofficeSecurityAccessor.BackOfficeSecurity?.GetUserId().Result ?? 0);
var sectionModels = sections.Select(_umbracoMapper.Map<Section>).ToArray();
var sectionModels = sections.Select(_umbracoMapper.Map<Section>).WhereNotNull().ToArray();
// this is a bit nasty since we'll be proxying via the app tree controller but we sort of have to do that
// since tree's by nature are controllers and require request contextual data
@@ -65,24 +66,28 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
ControllerContext = ControllerContext
};
var dashboards = _dashboardService.GetDashboards(_backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser);
var dashboards = _dashboardService.GetDashboards(_backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser);
//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)
{
var hasDashboards = dashboards.TryGetValue(section.Alias, out var dashboardsForSection) &&
var hasDashboards = section?.Alias is not null && dashboards.TryGetValue(section.Alias, out var dashboardsForSection) &&
dashboardsForSection.Any();
if (hasDashboards) continue;
// get the first tree in the section and get its root node route path
var sectionRoot = await appTreeController.GetApplicationTrees(section.Alias, null, null);
var sectionRoot = await appTreeController.GetApplicationTrees(section?.Alias, null, null);
if (!(sectionRoot.Result is null))
{
return sectionRoot.Result;
}
section.RoutePath = GetRoutePathForFirstTree(sectionRoot.Value);
if (section is not null)
{
section.RoutePath = GetRoutePathForFirstTree(sectionRoot.Value!);
}
}
return sectionModels;
@@ -93,16 +98,19 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
/// </summary>
/// <param name="rootNode"></param>
/// <returns></returns>
private string GetRoutePathForFirstTree(TreeRootNode rootNode)
private string? GetRoutePathForFirstTree(TreeRootNode rootNode)
{
if (!rootNode.IsContainer || !rootNode.ContainsTrees)
return rootNode.RoutePath;
foreach (var node in rootNode.Children)
if (rootNode.Children is not null)
{
if (node is TreeRootNode groupRoot)
return GetRoutePathForFirstTree(groupRoot); //recurse to get the first tree in the group
return node.RoutePath;
foreach (var node in rootNode.Children)
{
if (node is TreeRootNode groupRoot)
return GetRoutePathForFirstTree(groupRoot); //recurse to get the first tree in the group
return node.RoutePath;
}
}
return string.Empty;
@@ -112,14 +120,14 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
/// Returns all the sections that the user has access to
/// </summary>
/// <returns></returns>
public IEnumerable<Section> GetAllSections()
public IEnumerable<Section?> GetAllSections()
{
var sections = _sectionService.GetSections();
var mapped = sections.Select(_umbracoMapper.Map<Section>);
if (_backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.IsAdmin())
if (_backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.IsAdmin() ?? false)
return mapped;
return mapped.Where(x => _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.AllowedSections.Contains(x.Alias)).ToArray();
return mapped.Where(x => _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.AllowedSections.Contains(x?.Alias) ?? false).ToArray();
}
}
}