mirror of https://github.com/abpframework/abp.git
8 changed files with 114 additions and 3 deletions
@ -0,0 +1,25 @@ |
|||
using JetBrains.Annotations; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
|
|||
namespace Volo.Abp.Authorization |
|||
{ |
|||
public class PermissionsRequirement : IAuthorizationRequirement |
|||
{ |
|||
public string[] PermissionNames { get; } |
|||
|
|||
public bool RequiresAll { get; } |
|||
|
|||
public PermissionsRequirement([NotNull]string[] permissionNames, bool requiresAll) |
|||
{ |
|||
Check.NotNull(permissionNames, nameof(permissionNames)); |
|||
|
|||
PermissionNames = permissionNames; |
|||
RequiresAll = requiresAll; |
|||
} |
|||
|
|||
public override string ToString() |
|||
{ |
|||
return $"PermissionsRequirement: {string.Join(", ", PermissionNames)}"; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Volo.Abp.Authorization.Permissions; |
|||
|
|||
namespace Volo.Abp.Authorization |
|||
{ |
|||
public class PermissionsRequirementHandler : AuthorizationHandler<PermissionsRequirement> |
|||
{ |
|||
private readonly IPermissionChecker _permissionChecker; |
|||
|
|||
public PermissionsRequirementHandler(IPermissionChecker permissionChecker) |
|||
{ |
|||
_permissionChecker = permissionChecker; |
|||
} |
|||
|
|||
protected override async Task HandleRequirementAsync( |
|||
AuthorizationHandlerContext context, |
|||
PermissionsRequirement requirement) |
|||
{ |
|||
var multiplePermissionGrantResult = await _permissionChecker.IsGrantedAsync(context.User, requirement.PermissionNames); |
|||
|
|||
if (requirement.RequiresAll ? |
|||
multiplePermissionGrantResult.AllGranted : |
|||
multiplePermissionGrantResult.Result.Any(x => x.Value == PermissionGrantResult.Granted)) |
|||
{ |
|||
context.Succeed(requirement); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue