mirror of https://github.com/abpframework/abp.git
7 changed files with 93 additions and 1 deletions
@ -0,0 +1,43 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authorization; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Security.Claims; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization |
||||
|
{ |
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class AbpAuthorizationService : DefaultAuthorizationService, IAbpAuthorizationService, ITransientDependency |
||||
|
{ |
||||
|
private readonly ICurrentPrincipalAccessor _currentPrincipalAccessor; |
||||
|
|
||||
|
public AbpAuthorizationService( |
||||
|
IAuthorizationPolicyProvider policyProvider, |
||||
|
IAuthorizationHandlerProvider handlers, |
||||
|
ILogger<DefaultAuthorizationService> logger, |
||||
|
IAuthorizationHandlerContextFactory contextFactory, |
||||
|
IAuthorizationEvaluator evaluator, |
||||
|
IOptions<AuthorizationOptions> options, |
||||
|
ICurrentPrincipalAccessor currentPrincipalAccessor) |
||||
|
: base( |
||||
|
policyProvider, |
||||
|
handlers, |
||||
|
logger, |
||||
|
contextFactory, |
||||
|
evaluator, |
||||
|
options) |
||||
|
{ |
||||
|
_currentPrincipalAccessor = currentPrincipalAccessor; |
||||
|
} |
||||
|
|
||||
|
public async Task CheckAsync(string policyName) |
||||
|
{ |
||||
|
var result = await AuthorizeAsync(_currentPrincipalAccessor.Principal, null, policyName); |
||||
|
if (!result.Succeeded) |
||||
|
{ |
||||
|
throw new AbpAuthorizationException("Authorization failed! Given policy has not granted: " + policyName); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authorization; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization |
||||
|
{ |
||||
|
public static class AbpAuthorizationServiceExtensions |
||||
|
{ |
||||
|
public static Task CheckAsync(this IAuthorizationService authorizationService, string policyName) |
||||
|
{ |
||||
|
return authorizationService.AsAbpAuthorizationService().CheckAsync(policyName); |
||||
|
} |
||||
|
|
||||
|
private static IAbpAuthorizationService AsAbpAuthorizationService(this IAuthorizationService authorizationService) |
||||
|
{ |
||||
|
if (!(authorizationService is IAbpAuthorizationService abpAuthorizationService)) |
||||
|
{ |
||||
|
throw new AbpException($"{nameof(authorizationService)} should implement {typeof(IAbpAuthorizationService).FullName}"); |
||||
|
} |
||||
|
|
||||
|
return abpAuthorizationService; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authorization; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization |
||||
|
{ |
||||
|
public interface IAbpAuthorizationService : IAuthorizationService |
||||
|
{ |
||||
|
Task CheckAsync(string policyName); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue