-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the C# Auth Policy bits to protect the API to only users who hav…
…e the permission
- Loading branch information
1 parent
9135d10
commit 16e9cd7
Showing
5 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HasUmbracoPermissionRequirement> | ||
{ | ||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} |