From e5d1d8f623e9615930a18b865e6436b302eeeb46 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Mon, 4 Mar 2019 18:34:18 +0300 Subject: [PATCH] Remove IAuthorizationEnabled. Auth and feature refactor. --- .../AuthorizationInterceptorRegistrar.cs | 26 +++++++++++++++++-- .../Authorization/IAuthorizationEnabled.cs | 7 ----- .../MethodInvocationAuthorizationService.cs | 25 +++++++++++------- .../Application/AbpDddApplicationModule.cs | 1 - .../Services/ApplicationService.cs | 1 - .../Volo/Abp/Features/FeatureInterceptor.cs | 8 +++--- .../Features/FeatureInterceptorRegistrar.cs | 4 +-- .../MethodInvocationFeatureCheckerService.cs | 24 +++++++++++------ 8 files changed, 62 insertions(+), 34 deletions(-) delete mode 100644 framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/IAuthorizationEnabled.cs diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptorRegistrar.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptorRegistrar.cs index 76a12b6dfe..69a285ddc8 100644 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptorRegistrar.cs +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptorRegistrar.cs @@ -1,4 +1,8 @@ -using Volo.Abp.DependencyInjection; +using System; +using System.Linq; +using System.Reflection; +using Microsoft.AspNetCore.Authorization; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.Authorization { @@ -6,10 +10,28 @@ namespace Volo.Abp.Authorization { public static void RegisterIfNeeded(IOnServiceRegistredContext context) { - if (typeof(IAuthorizationEnabled).IsAssignableFrom(context.ImplementationType)) + if (ShouldIntercept(context.ImplementationType)) { context.Interceptors.TryAdd(); } } + + private static bool ShouldIntercept(Type type) + { + return type.IsDefined(typeof(AuthorizeAttribute), true) || + AnyMethodHasAuthorizeAttribute(type); + } + + private static bool AnyMethodHasAuthorizeAttribute(Type implementationType) + { + return implementationType + .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) + .Any(HasAuthorizeAttribute); + } + + private static bool HasAuthorizeAttribute(MemberInfo methodInfo) + { + return methodInfo.IsDefined(typeof(AuthorizeAttribute), true); + } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/IAuthorizationEnabled.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/IAuthorizationEnabled.cs deleted file mode 100644 index 8563a98a8c..0000000000 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/IAuthorizationEnabled.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Volo.Abp.Authorization -{ - public interface IAuthorizationEnabled - { - - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs index 1cca959ebe..56baba8ce4 100644 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/MethodInvocationAuthorizationService.cs @@ -1,4 +1,6 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Volo.Abp.Clients; @@ -30,8 +32,7 @@ namespace Volo.Abp.Authorization return; } - var authorizationAttributes = GetAuthorizationDataAttributes(context); - foreach (var authorizationAttribute in authorizationAttributes) + foreach (var authorizationAttribute in GetAuthorizationDataAttributes(context.Method)) { await CheckAsync(authorizationAttribute); } @@ -42,17 +43,23 @@ namespace Volo.Abp.Authorization return context.Method.GetCustomAttributes(true).OfType().Any(); } - protected virtual IAuthorizeData[] GetAuthorizationDataAttributes(MethodInvocationAuthorizationContext context) + protected virtual IEnumerable GetAuthorizationDataAttributes(MethodInfo methodInfo) { - var classAttributes = context.Method.DeclaringType + var attributes = methodInfo .GetCustomAttributes(true) .OfType(); - var methodAttributes = context.Method - .GetCustomAttributes(true) - .OfType(); + if (methodInfo.IsPublic) + { + attributes = attributes + .Union( + methodInfo.DeclaringType + .GetCustomAttributes(true) + .OfType() + ); + } - return classAttributes.Union(methodAttributes).ToArray(); + return attributes; } protected async Task CheckAsync(IAuthorizeData authorizationAttribute) diff --git a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/AbpDddApplicationModule.cs b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/AbpDddApplicationModule.cs index fcdc29c454..e5ade49532 100644 --- a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/AbpDddApplicationModule.cs +++ b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/AbpDddApplicationModule.cs @@ -33,7 +33,6 @@ namespace Volo.Abp.Application options.IgnoredInterfaces.AddIfNotContains(typeof(IRemoteService)); options.IgnoredInterfaces.AddIfNotContains(typeof(IApplicationService)); options.IgnoredInterfaces.AddIfNotContains(typeof(IUnitOfWorkEnabled)); - options.IgnoredInterfaces.AddIfNotContains(typeof(IAuthorizationEnabled)); }); } } diff --git a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs index b4f53aff0a..48865f38f2 100644 --- a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs +++ b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs @@ -26,7 +26,6 @@ namespace Volo.Abp.Application.Services IAvoidDuplicateCrossCuttingConcerns, IValidationEnabled, IUnitOfWorkEnabled, - IAuthorizationEnabled, IAuditingEnabled, ITransientDependency { diff --git a/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureInterceptor.cs b/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureInterceptor.cs index 2f684d99ef..a0b15674f6 100644 --- a/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureInterceptor.cs +++ b/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureInterceptor.cs @@ -8,12 +8,12 @@ namespace Volo.Abp.Features { public class FeatureInterceptor : AbpInterceptor, ITransientDependency { - private readonly IMethodInvocationFeatureCheckerService _methodInvocationAuthorizationService; + private readonly IMethodInvocationFeatureCheckerService _methodInvocationFeatureCheckerService; public FeatureInterceptor( - IMethodInvocationFeatureCheckerService methodInvocationAuthorizationService) + IMethodInvocationFeatureCheckerService methodInvocationFeatureCheckerService) { - _methodInvocationAuthorizationService = methodInvocationAuthorizationService; + _methodInvocationFeatureCheckerService = methodInvocationFeatureCheckerService; } public override void Intercept(IAbpMethodInvocation invocation) @@ -46,7 +46,7 @@ namespace Volo.Abp.Features protected virtual Task CheckFeaturesAsync(IAbpMethodInvocation invocation) { - return _methodInvocationAuthorizationService.CheckAsync( + return _methodInvocationFeatureCheckerService.CheckAsync( new MethodInvocationFeatureCheckerContext( invocation.Method ) diff --git a/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureInterceptorRegistrar.cs b/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureInterceptorRegistrar.cs index 4ad03c9aff..8cb6cd7ea5 100644 --- a/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureInterceptorRegistrar.cs +++ b/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureInterceptorRegistrar.cs @@ -18,10 +18,10 @@ namespace Volo.Abp.Features private static bool ShouldIntercept(Type type) { return type.IsDefined(typeof(RequiresFeatureAttribute), true) || - AnyMethodRequiresFeatureAttribute(type); + AnyMethodHasRequiresFeatureAttribute(type); } - private static bool AnyMethodRequiresFeatureAttribute(Type implementationType) + private static bool AnyMethodHasRequiresFeatureAttribute(Type implementationType) { return implementationType .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) diff --git a/framework/src/Volo.Abp.Features/Volo/Abp/Features/MethodInvocationFeatureCheckerService.cs b/framework/src/Volo.Abp.Features/Volo/Abp/Features/MethodInvocationFeatureCheckerService.cs index 28ac25fdd5..da7becc61f 100644 --- a/framework/src/Volo.Abp.Features/Volo/Abp/Features/MethodInvocationFeatureCheckerService.cs +++ b/framework/src/Volo.Abp.Features/Volo/Abp/Features/MethodInvocationFeatureCheckerService.cs @@ -1,4 +1,6 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; @@ -21,7 +23,7 @@ namespace Volo.Abp.Features return; } - foreach (var requiresFeatureAttribute in GetRequiredFeatureAttributes(context)) + foreach (var requiresFeatureAttribute in GetRequiredFeatureAttributes(context.Method)) { await _featureChecker.CheckEnabledAsync(requiresFeatureAttribute.RequiresAll, requiresFeatureAttribute.Features); } @@ -35,17 +37,23 @@ namespace Volo.Abp.Features .Any(); } - protected virtual RequiresFeatureAttribute[] GetRequiredFeatureAttributes(MethodInvocationFeatureCheckerContext context) + protected virtual IEnumerable GetRequiredFeatureAttributes(MethodInfo methodInfo) { - var classAttributes = context.Method.DeclaringType + var attributes = methodInfo .GetCustomAttributes(true) .OfType(); - var methodAttributes = context.Method - .GetCustomAttributes(true) - .OfType(); + if (methodInfo.IsPublic) + { + attributes = attributes + .Union( + methodInfo.DeclaringType + .GetCustomAttributes(true) + .OfType() + ); + } - return classAttributes.Union(methodAttributes).ToArray(); + return attributes; } } } \ No newline at end of file