Created
April 24, 2018 19:10
-
-
Save Ibro/8cce711b1ea4d3c1e5a10965827e6cd2 to your computer and use it in GitHub Desktop.
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
public class InsertCategoryHandler : AuthorizationHandler<InsertCategoryRequirement, Category>, IAuthorizationRequirement | |
{ | |
private readonly ISecurityService _securityService; | |
public InsertCategoryHandler(ISecurityService securityService) => _securityService = securityService; | |
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, | |
InsertCategoryRequirement requirement, Category category) | |
{ | |
if (!(context.Resource is Category)) | |
{ | |
context.Fail(); | |
return; | |
} | |
var userId = context.User.GetUserId(); | |
var hasAdminAccess = await _securityService.HasAdmin(userId); | |
if (hasAdminAccess) | |
{ | |
context.Succeed(requirement); | |
return; | |
} | |
if (string.IsNullOrEmpty(category.Id) && category.UserId == userId) | |
{ | |
context.Succeed(requirement); | |
return; | |
} | |
context.Fail(); | |
} | |
} | |
public class InsertCategoryRequirement : IAuthorizationRequirement | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment