| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Linq;
- using System.Reflection;
- using Abp.Application.Features;
- using Abp.Dependency;
- using Castle.Core;
- using Castle.MicroKernel;
- namespace Abp.Authorization
- {
- /// <summary>
- /// This class is used to register interceptors on the Application Layer.
- /// </summary>
- internal static class AuthorizationInterceptorRegistrar
- {
- public static void Initialize(IIocManager iocManager)
- {
- iocManager.IocContainer.Kernel.ComponentRegistered += Kernel_ComponentRegistered;
- }
- private static void Kernel_ComponentRegistered(string key, IHandler handler)
- {
- if (ShouldIntercept(handler.ComponentModel.Implementation))
- {
- handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(AuthorizationInterceptor)));
- }
- }
- private static bool ShouldIntercept(Type type)
- {
- if (SelfOrMethodsDefinesAttribute<AbpAuthorizeAttribute>(type))
- {
- return true;
- }
- if (SelfOrMethodsDefinesAttribute<RequiresFeatureAttribute>(type))
- {
- return true;
- }
- return false;
- }
- private static bool SelfOrMethodsDefinesAttribute<TAttr>(Type type)
- {
- if (type.GetTypeInfo().IsDefined(typeof(TAttr), true))
- {
- return true;
- }
- return type
- .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
- .Any(m => m.IsDefined(typeof(TAttr), true));
- }
- }
- }
|