Last active
May 29, 2018 18:58
-
-
Save wellingtonjhn/74b7405703e3f55c29b716cf672fbe52 to your computer and use it in GitHub Desktop.
Policy Authorization Requirement
This file contains hidden or 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
| public class DeleteUserRequirementHandler : AuthorizationHandler<DeleteUserRequirement> | |
| { | |
| private const string AdministratorRoleName = "Administrator"; | |
| private AuthorizationHandlerContext _context; | |
| protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, DeleteUserRequirement requirement) | |
| { | |
| _context = context; | |
| var isAdministrator = IsAdministrator(); | |
| var canDeleteUser = HasRequirements(requirement); | |
| if (isAdministrator && canDeleteUser) | |
| { | |
| context.Succeed(requirement); | |
| } | |
| return Task.CompletedTask; | |
| } | |
| private bool IsAdministrator() => | |
| GetClaim(ClaimTypes.Role, AdministratorRoleName); | |
| private bool HasRequirements(DeleteUserRequirement requirement) => | |
| GetClaim("permissions", requirement.RequiredPermission); | |
| private bool GetClaim(string type, string value) => _context.User.HasClaim(type, value); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment