Added Authorization interceptor for application services.

pull/216/head
Halil İbrahim Kalkan 8 years ago
parent 650421f459
commit dcf6be4882

@ -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?
}
}
}

@ -23,6 +23,12 @@ namespace Volo.Abp.Collections
/// <typeparam name="T">Type</typeparam>
void Add<T>() where T : TBaseType;
/// <summary>
/// Adds a type to list if it's not already in the list.
/// </summary>
/// <typeparam name="T">Type</typeparam>
void TryAdd<T>() where T : TBaseType;
/// <summary>
/// Checks if a type exists in the list.
/// </summary>

@ -60,6 +60,16 @@ namespace Volo.Abp.Collections
_typeList.Add(typeof(T));
}
public void TryAdd<T>() where T : TBaseType
{
if (Contains<T>())
{
return;
}
Add<T>();
}
/// <inheritdoc/>
public void Add(Type item)
{

@ -14,6 +14,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Authorization\Volo.Abp.Authorization.csproj" />
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" />
<ProjectReference Include="..\Volo.Abp.Data\Volo.Abp.Data.csproj" />
<ProjectReference Include="..\Volo.Abp.EventBus\Volo.Abp.EventBus.csproj" />

@ -1,6 +1,7 @@
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Application.Services;
using Volo.Abp.Authorization;
using Volo.Abp.Data;
using Volo.Abp.EventBus;
using Volo.Abp.Guids;
@ -15,6 +16,8 @@ using Volo.Abp.Validation;
namespace Volo.Abp
{
//TODO: Consider to split this DDD package by layers!
[DependsOn(typeof(AbpGuidsModule))]
[DependsOn(typeof(AbpDataModule))]
[DependsOn(typeof(AbpObjectMappingModule))]
@ -22,6 +25,7 @@ namespace Volo.Abp
[DependsOn(typeof(AbpThreadingModule))]
[DependsOn(typeof(AbpEventBusModule))]
[DependsOn(typeof(AbpValidationModule))]
[DependsOn(typeof(AbpAuthorizationModule))]
[DependsOn(typeof(AbpHttpAbstractionsModule))]
public class AbpDddModule : AbpModule
{
@ -29,6 +33,7 @@ namespace Volo.Abp
{
services.OnRegistred(UnitOfWorkInterceptorRegistrar.RegisterIfNeeded);
services.OnRegistred(ValidationInterceptorRegistrar.RegisterIfNeeded);
services.OnRegistred(AuthorizationInterceptorRegistrar.RegisterIfNeeded);
}
public override void ConfigureServices(IServiceCollection services)

@ -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>();
}
}
}
}

@ -9,8 +9,7 @@ namespace Volo.Abp.Application.Services
{
if (typeof(IApplicationService).IsAssignableFrom(context.ImplementationType))
{
//TODO: Notice that it may add the interceptor more than one for every exposed service type!?
context.Interceptors.Add<ValidationInterceptor>();
context.Interceptors.TryAdd<ValidationInterceptor>();
}
}
}

@ -9,8 +9,7 @@ namespace Volo.Abp.Uow
{
if (UnitOfWorkHelper.IsUnitOfWorkType(context.ImplementationType.GetTypeInfo()))
{
//TODO: Notice that it may add the interceptor more than one for every exposed service type!?
context.Interceptors.Add<UnitOfWorkInterceptor>();
context.Interceptors.TryAdd<UnitOfWorkInterceptor>();
}
}
}

Loading…
Cancel
Save