mirror of https://github.com/abpframework/abp.git
11 changed files with 150 additions and 4 deletions
@ -0,0 +1,45 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Volo.Abp.Aspects; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.DynamicProxy; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.Authorization |
|||
{ |
|||
public class AuthorizationInterceptor : AbpInterceptor, ITransientDependency |
|||
{ |
|||
private readonly IMethodInvocationAuthorizationService _methodInvocationAuthorizationService; |
|||
|
|||
public AuthorizationInterceptor(IMethodInvocationAuthorizationService methodInvocationAuthorizationService) |
|||
{ |
|||
_methodInvocationAuthorizationService = methodInvocationAuthorizationService; |
|||
} |
|||
|
|||
public override void Intercept(IAbpMethodInvocation invocation) |
|||
{ |
|||
AsyncHelper.RunSync(() => InterceptAsync(invocation)); |
|||
} |
|||
|
|||
public override async Task InterceptAsync(IAbpMethodInvocation invocation) |
|||
{ |
|||
if (AbpCrossCuttingConcerns.IsApplied(invocation.TargetObject, AbpCrossCuttingConcerns.Authorization)) |
|||
{ |
|||
await invocation.ProceedAsync(); |
|||
return; |
|||
} |
|||
|
|||
await AuthorizeAsync(invocation); |
|||
await invocation.ProceedAsync(); |
|||
} |
|||
|
|||
protected virtual Task AuthorizeAsync(IAbpMethodInvocation invocation) |
|||
{ |
|||
return _methodInvocationAuthorizationService.CheckAsync( |
|||
new MethodInvocationAuthorizationContext( |
|||
invocation.Method |
|||
) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Authorization |
|||
{ |
|||
public interface IMethodInvocationAuthorizationService |
|||
{ |
|||
Task CheckAsync(MethodInvocationAuthorizationContext context); |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System.Reflection; |
|||
|
|||
namespace Volo.Abp.Authorization |
|||
{ |
|||
public class MethodInvocationAuthorizationContext |
|||
{ |
|||
public MethodInfo Method { get; } |
|||
|
|||
public MethodInvocationAuthorizationContext(MethodInfo method) |
|||
{ |
|||
Method = method; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Authorization |
|||
{ |
|||
public class MethodInvocationAuthorizationService : IMethodInvocationAuthorizationService, ITransientDependency |
|||
{ |
|||
private readonly IAuthorizationService _authorizationService; |
|||
|
|||
public MethodInvocationAuthorizationService(IAuthorizationService authorizationService) |
|||
{ |
|||
_authorizationService = authorizationService; |
|||
} |
|||
|
|||
public async Task CheckAsync(MethodInvocationAuthorizationContext context) |
|||
{ |
|||
//TODO: Fully implement! (allow anonymous... etc.)
|
|||
|
|||
var authorizationAttributes = GetAuthorizationDataAttributes(context); |
|||
foreach (var authorizationAttribute in authorizationAttributes) |
|||
{ |
|||
await CheckAsync(authorizationAttribute); |
|||
} |
|||
} |
|||
|
|||
protected virtual IAuthorizeData[] GetAuthorizationDataAttributes(MethodInvocationAuthorizationContext context) |
|||
{ |
|||
return context.Method |
|||
.GetCustomAttributes(true) |
|||
.OfType<IAuthorizeData>() |
|||
.ToArray(); |
|||
} |
|||
|
|||
protected async Task CheckAsync(IAuthorizeData authorizationAttribute) |
|||
{ |
|||
await _authorizationService.CheckAsync(authorizationAttribute.Policy); |
|||
//TODO: What about roles and other props?
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
using Volo.Abp.Authorization; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Application.Services |
|||
{ |
|||
public static class AuthorizationInterceptorRegistrar |
|||
{ |
|||
public static void RegisterIfNeeded(IOnServiceRegistredContext context) |
|||
{ |
|||
if (typeof(IApplicationService).IsAssignableFrom(context.ImplementationType)) |
|||
{ |
|||
context.Interceptors.TryAdd<AuthorizationInterceptor>(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue