diff --git a/ExaminePeek/Auth/HasUmbracoPermissionHandler.cs b/ExaminePeek/Auth/HasUmbracoPermissionHandler.cs new file mode 100644 index 0000000..4f58f36 --- /dev/null +++ b/ExaminePeek/Auth/HasUmbracoPermissionHandler.cs @@ -0,0 +1,37 @@ +using Microsoft.AspNetCore.Authorization; +using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Security.Authorization; +using Umbraco.Cms.Core.Services; +using Umbraco.Extensions; + +namespace ExaminePeek.Auth +{ + public class HasUmbracoPermissionHandler : AuthorizationHandler + { + private readonly IAuthorizationHelper _authorizationHelper; + private readonly IUserService _userService; + + public HasUmbracoPermissionHandler(IAuthorizationHelper authorizationHelper, IUserService userService) + { + _authorizationHelper = authorizationHelper; + _userService = userService; + } + + protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasUmbracoPermissionRequirement requirement) + { + var umbracoUser = _authorizationHelper.GetUmbracoUser(context.User); + umbracoUser. + var permissions = umbracoUser.GetPermissions(Constants.System.RootString, _userService); + var hasPermission = permissions.Contains(requirement.Permission); + + if (hasPermission) + { + context.Succeed(requirement); + return Task.CompletedTask; + } + + context.Fail(); + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/ExaminePeek/Auth/HasUmbracoPermissionRequirement.cs b/ExaminePeek/Auth/HasUmbracoPermissionRequirement.cs new file mode 100644 index 0000000..5391fcd --- /dev/null +++ b/ExaminePeek/Auth/HasUmbracoPermissionRequirement.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Authorization; + +namespace ExaminePeek.Auth +{ + public class HasUmbracoPermissionRequirement : IAuthorizationRequirement + { + public HasUmbracoPermissionRequirement(string permission) => Permission = permission; + public string Permission { get; } + } +} \ No newline at end of file diff --git a/ExaminePeek/Composers/ExaminePeekComposer.cs b/ExaminePeek/Composers/ExaminePeekComposer.cs index 633debe..d08e222 100644 --- a/ExaminePeek/Composers/ExaminePeekComposer.cs +++ b/ExaminePeek/Composers/ExaminePeekComposer.cs @@ -1,8 +1,13 @@ -using Microsoft.Extensions.DependencyInjection; +using ExaminePeek.Auth; +using ExaminePeek.Extensions; +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; +using OpenIddict.Validation.AspNetCore; using Swashbuckle.AspNetCore.SwaggerGen; using System.Reflection; using Umbraco.Cms.Api.Management.OpenApi; +using Umbraco.Cms.Api.Management.Security.Authorization.UserGroup; using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.DependencyInjection; @@ -40,6 +45,16 @@ public void Compose(IUmbracoBuilder builder) opt.IncludeXmlComments(xmlPath); } }); + + builder.Services.AddSingleton(); + builder.Services.Configure(opt => + { + opt.AddPolicy("HasExaminePeekPermission", policyBuilder => + { + policyBuilder.AuthenticationSchemes.Add(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme); + policyBuilder.RequireUmbracoPermission("ExaminePeek.Enabled"); + }); + }); } // PR: https://github.com/umbraco/Umbraco-CMS/pull/15699 diff --git a/ExaminePeek/Controllers/ExaminePeekControllerBase.cs b/ExaminePeek/Controllers/ExaminePeekControllerBase.cs index 72c5d18..03c26f2 100644 --- a/ExaminePeek/Controllers/ExaminePeekControllerBase.cs +++ b/ExaminePeek/Controllers/ExaminePeekControllerBase.cs @@ -1,14 +1,13 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Umbraco.Cms.Api.Common.Attributes; -using Umbraco.Cms.Web.Common.Authorization; using Umbraco.Cms.Web.Common.Routing; namespace ExaminePeek.Controllers { [ApiController] [BackOfficeRoute("examinepeek/api/v{version:apiVersion}")] - [Authorize(Policy = AuthorizationPolicies.SectionAccessContent)] + [Authorize(Policy = "HasExaminePeekPermission")] [MapToApi("ExaminePeek")] public class ExaminePeekControllerBase : ControllerBase { diff --git a/ExaminePeek/Extensions/PolicyBuilderExtensions.cs b/ExaminePeek/Extensions/PolicyBuilderExtensions.cs new file mode 100644 index 0000000..36a7e24 --- /dev/null +++ b/ExaminePeek/Extensions/PolicyBuilderExtensions.cs @@ -0,0 +1,13 @@ +using ExaminePeek.Auth; +using Microsoft.AspNetCore.Authorization; + +namespace ExaminePeek.Extensions +{ + public static class PolicyBuilderExtensions + { + public static void RequireUmbracoPermission(this AuthorizationPolicyBuilder builder, string permission) + { + builder.Requirements.Add(new HasUmbracoPermissionRequirement(permission)); + } + } +} \ No newline at end of file