AuthorizationHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using System.Linq;
  6. using Abp.Application.Features;
  7. using Abp.Configuration.Startup;
  8. using Abp.Dependency;
  9. using Abp.Localization;
  10. using Abp.Reflection;
  11. using Abp.Runtime.Session;
  12. namespace Abp.Authorization
  13. {
  14. public class AuthorizationHelper : IAuthorizationHelper, ITransientDependency
  15. {
  16. public IAbpSession AbpSession { get; set; }
  17. public IPermissionChecker PermissionChecker { get; set; }
  18. public IFeatureChecker FeatureChecker { get; set; }
  19. public ILocalizationManager LocalizationManager { get; set; }
  20. private readonly IFeatureChecker _featureChecker;
  21. private readonly IAuthorizationConfiguration _authConfiguration;
  22. public AuthorizationHelper(IFeatureChecker featureChecker, IAuthorizationConfiguration authConfiguration)
  23. {
  24. _featureChecker = featureChecker;
  25. _authConfiguration = authConfiguration;
  26. AbpSession = NullAbpSession.Instance;
  27. PermissionChecker = NullPermissionChecker.Instance;
  28. LocalizationManager = NullLocalizationManager.Instance;
  29. }
  30. public virtual async Task AuthorizeAsync(IEnumerable<IAbpAuthorizeAttribute> authorizeAttributes)
  31. {
  32. if (!_authConfiguration.IsEnabled)
  33. {
  34. return;
  35. }
  36. if (!AbpSession.UserId.HasValue)
  37. {
  38. throw new AbpAuthorizationException(
  39. LocalizationManager.GetString(AbpConsts.LocalizationSourceName, "CurrentUserDidNotLoginToTheApplication")
  40. );
  41. }
  42. foreach (var authorizeAttribute in authorizeAttributes)
  43. {
  44. await PermissionChecker.AuthorizeAsync(authorizeAttribute.RequireAllPermissions, authorizeAttribute.Permissions);
  45. }
  46. }
  47. public virtual async Task AuthorizeAsync(MethodInfo methodInfo, Type type)
  48. {
  49. await CheckFeatures(methodInfo, type);
  50. await CheckPermissions(methodInfo, type);
  51. }
  52. protected virtual async Task CheckFeatures(MethodInfo methodInfo, Type type)
  53. {
  54. var featureAttributes = ReflectionHelper.GetAttributesOfMemberAndType<RequiresFeatureAttribute>(methodInfo, type);
  55. if (featureAttributes.Count <= 0)
  56. {
  57. return;
  58. }
  59. foreach (var featureAttribute in featureAttributes)
  60. {
  61. await _featureChecker.CheckEnabledAsync(featureAttribute.RequiresAll, featureAttribute.Features);
  62. }
  63. }
  64. protected virtual async Task CheckPermissions(MethodInfo methodInfo, Type type)
  65. {
  66. if (!_authConfiguration.IsEnabled)
  67. {
  68. return;
  69. }
  70. if (AllowAnonymous(methodInfo, type))
  71. {
  72. return;
  73. }
  74. if (ReflectionHelper.IsPropertyGetterSetterMethod(methodInfo, type))
  75. {
  76. return;
  77. }
  78. var authorizeAttributes =
  79. ReflectionHelper
  80. .GetAttributesOfMemberAndType(methodInfo, type)
  81. .OfType<IAbpAuthorizeAttribute>()
  82. .ToArray();
  83. if (!authorizeAttributes.Any())
  84. {
  85. return;
  86. }
  87. await AuthorizeAsync(authorizeAttributes);
  88. }
  89. private static bool AllowAnonymous(MemberInfo memberInfo, Type type)
  90. {
  91. return ReflectionHelper
  92. .GetAttributesOfMemberAndType(memberInfo, type)
  93. .OfType<IAbpAllowAnonymousAttribute>()
  94. .Any();
  95. }
  96. }
  97. }