AuthorizationInterceptorRegistrar.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using Abp.Application.Features;
  5. using Abp.Dependency;
  6. using Castle.Core;
  7. using Castle.MicroKernel;
  8. namespace Abp.Authorization
  9. {
  10. /// <summary>
  11. /// This class is used to register interceptors on the Application Layer.
  12. /// </summary>
  13. internal static class AuthorizationInterceptorRegistrar
  14. {
  15. public static void Initialize(IIocManager iocManager)
  16. {
  17. iocManager.IocContainer.Kernel.ComponentRegistered += Kernel_ComponentRegistered;
  18. }
  19. private static void Kernel_ComponentRegistered(string key, IHandler handler)
  20. {
  21. if (ShouldIntercept(handler.ComponentModel.Implementation))
  22. {
  23. handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(AuthorizationInterceptor)));
  24. }
  25. }
  26. private static bool ShouldIntercept(Type type)
  27. {
  28. if (SelfOrMethodsDefinesAttribute<AbpAuthorizeAttribute>(type))
  29. {
  30. return true;
  31. }
  32. if (SelfOrMethodsDefinesAttribute<RequiresFeatureAttribute>(type))
  33. {
  34. return true;
  35. }
  36. return false;
  37. }
  38. private static bool SelfOrMethodsDefinesAttribute<TAttr>(Type type)
  39. {
  40. if (type.GetTypeInfo().IsDefined(typeof(TAttr), true))
  41. {
  42. return true;
  43. }
  44. return type
  45. .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
  46. .Any(m => m.IsDefined(typeof(TAttr), true));
  47. }
  48. }
  49. }