Adhered to linting rules for authorization policy handlers, requirements and tests.

This commit is contained in:
Andy Butland
2020-12-06 09:13:29 +01:00
parent 05dc597fc2
commit 61cbb84dd3
37 changed files with 697 additions and 461 deletions

View File

@@ -1,14 +1,16 @@
using Microsoft.AspNetCore.Authorization;
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Linq;
using Umbraco.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Umbraco.Core;
using Umbraco.Core.Security;
using Umbraco.Web.Services;
namespace Umbraco.Web.BackOffice.Authorization
{
/// <summary>
/// Ensures that the current user has access to the section for which the specified tree(s) belongs
/// </summary>
@@ -18,40 +20,35 @@ namespace Umbraco.Web.BackOffice.Authorization
/// </remarks>
public class TreeHandler : MustSatisfyRequirementAuthorizationHandler<TreeRequirement>
{
private readonly ITreeService _treeService;
private readonly IBackOfficeSecurityAccessor _backofficeSecurityAccessor;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
/// <summary>
/// Constructor to set authorization to be based on a tree alias for which application security will be applied
/// Initializes a new instance of the <see cref="TreeHandler"/> class.
/// </summary>
/// <param name="treeService"></param>
/// <param name="backofficeSecurityAccessor"></param>
/// <param name="treeAliases">
/// If the user has access to the application that the treeAlias is specified in, they will be authorized.
/// Multiple trees may be specified.
/// </param>
public TreeHandler(ITreeService treeService, IBackOfficeSecurityAccessor backofficeSecurityAccessor)
/// <param name="treeService">Service for section tree operations.</param>
/// <param name="backOfficeSecurityAccessor">Accessor for back-office security.</param>
public TreeHandler(ITreeService treeService, IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
{
_treeService = treeService ?? throw new ArgumentNullException(nameof(treeService));
_backofficeSecurityAccessor = backofficeSecurityAccessor ?? throw new ArgumentNullException(nameof(backofficeSecurityAccessor));
_backOfficeSecurityAccessor = backOfficeSecurityAccessor ?? throw new ArgumentNullException(nameof(backOfficeSecurityAccessor));
}
/// <inheritdoc/>
protected override Task<bool> IsAuthorized(AuthorizationHandlerContext context, TreeRequirement requirement)
{
var apps = requirement.TreeAliases.Select(x => _treeService
.GetByAlias(x))
var apps = requirement.TreeAliases
.Select(x => _treeService.GetByAlias(x))
.WhereNotNull()
.Select(x => x.SectionAlias)
.Distinct()
.ToArray();
var isAuth = _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser != null
&& apps.Any(app => _backofficeSecurityAccessor.BackOfficeSecurity.UserHasSectionAccess(
app, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser));
var isAuth = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser != null &&
apps.Any(app => _backOfficeSecurityAccessor.BackOfficeSecurity.UserHasSectionAccess(
app, _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser));
return Task.FromResult(isAuth);
}
}
}