mirror of https://github.com/abpframework/abp.git
10 changed files with 8793 additions and 76 deletions
File diff suppressed because it is too large
@ -0,0 +1,61 @@ |
|||
using System.Security.Principal; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Microsoft.AspNetCore.Authorization.Infrastructure; |
|||
using Volo.Abp.Authorization.Permissions; |
|||
|
|||
namespace Volo.Blogging.Comments |
|||
{ |
|||
public class CommentAuthorizationHandler : AuthorizationHandler<OperationAuthorizationRequirement, Comment> |
|||
{ |
|||
private readonly IPermissionChecker _permissionChecker; |
|||
|
|||
public CommentAuthorizationHandler(IPermissionChecker permissionChecker) |
|||
{ |
|||
_permissionChecker = permissionChecker; |
|||
} |
|||
|
|||
protected override async Task HandleRequirementAsync( |
|||
AuthorizationHandlerContext context, |
|||
OperationAuthorizationRequirement requirement, |
|||
Comment resource) |
|||
{ |
|||
if (requirement.Name == CommonOperations.Delete.Name && await HasDeletePermission(context, resource)) |
|||
{ |
|||
context.Succeed(requirement); |
|||
return; |
|||
} |
|||
|
|||
if (requirement.Name == CommonOperations.Update.Name && await HasUpdatePermission(context, resource)) |
|||
{ |
|||
context.Succeed(requirement); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
private async Task<bool> HasDeletePermission(AuthorizationHandlerContext context, Comment resource) |
|||
{ |
|||
if (await _permissionChecker.IsGrantedAsync(context.User, BloggingPermissions.Comments.Delete)) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
private async Task<bool> HasUpdatePermission(AuthorizationHandlerContext context, Comment resource) |
|||
{ |
|||
if (resource.CreatorId != null && resource.CreatorId == context.User.FindUserId()) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
if (await _permissionChecker.IsGrantedAsync(context.User, BloggingPermissions.Comments.Update)) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using Microsoft.AspNetCore.Authorization.Infrastructure; |
|||
|
|||
namespace Volo.Blogging |
|||
{ |
|||
public static class CommonOperations |
|||
{ |
|||
public static OperationAuthorizationRequirement Update = new OperationAuthorizationRequirement { Name = nameof(Update) }; |
|||
public static OperationAuthorizationRequirement Delete = new OperationAuthorizationRequirement { Name = nameof(Delete) }; |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
using System.Security.Principal; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Microsoft.AspNetCore.Authorization.Infrastructure; |
|||
using Volo.Abp.Authorization.Permissions; |
|||
|
|||
namespace Volo.Blogging.Posts |
|||
{ |
|||
public class PostAuthorizationHandler : AuthorizationHandler<OperationAuthorizationRequirement, Post> |
|||
{ |
|||
private readonly IPermissionChecker _permissionChecker; |
|||
|
|||
public PostAuthorizationHandler(IPermissionChecker permissionChecker) |
|||
{ |
|||
_permissionChecker = permissionChecker; |
|||
} |
|||
|
|||
protected override async Task HandleRequirementAsync( |
|||
AuthorizationHandlerContext context, |
|||
OperationAuthorizationRequirement requirement, |
|||
Post resource) |
|||
{ |
|||
if (requirement.Name == CommonOperations.Delete.Name && await HasDeletePermission(context, resource)) |
|||
{ |
|||
context.Succeed(requirement); |
|||
return; |
|||
} |
|||
|
|||
if (requirement.Name == CommonOperations.Update.Name && await HasUpdatePermission(context, resource)) |
|||
{ |
|||
context.Succeed(requirement); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
private async Task<bool> HasDeletePermission(AuthorizationHandlerContext context, Post resource) |
|||
{ |
|||
if (await _permissionChecker.IsGrantedAsync(context.User, BloggingPermissions.Comments.Delete)) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
private async Task<bool> HasUpdatePermission(AuthorizationHandlerContext context, Post resource) |
|||
{ |
|||
if (resource.CreatorId != null && resource.CreatorId == context.User.FindUserId()) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
if (await _permissionChecker.IsGrantedAsync(context.User, BloggingPermissions.Comments.Update)) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue