| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Linq;
- using Abp.Application.Features;
- using Abp.Configuration.Startup;
- using Abp.Dependency;
- using Abp.Localization;
- using Abp.Reflection;
- using Abp.Runtime.Session;
- namespace Abp.Authorization
- {
- public class AuthorizationHelper : IAuthorizationHelper, ITransientDependency
- {
- public IAbpSession AbpSession { get; set; }
- public IPermissionChecker PermissionChecker { get; set; }
- public IFeatureChecker FeatureChecker { get; set; }
- public ILocalizationManager LocalizationManager { get; set; }
- private readonly IFeatureChecker _featureChecker;
- private readonly IAuthorizationConfiguration _authConfiguration;
- public AuthorizationHelper(IFeatureChecker featureChecker, IAuthorizationConfiguration authConfiguration)
- {
- _featureChecker = featureChecker;
- _authConfiguration = authConfiguration;
- AbpSession = NullAbpSession.Instance;
- PermissionChecker = NullPermissionChecker.Instance;
- LocalizationManager = NullLocalizationManager.Instance;
- }
- public virtual async Task AuthorizeAsync(IEnumerable<IAbpAuthorizeAttribute> authorizeAttributes)
- {
- if (!_authConfiguration.IsEnabled)
- {
- return;
- }
- if (!AbpSession.UserId.HasValue)
- {
- throw new AbpAuthorizationException(
- LocalizationManager.GetString(AbpConsts.LocalizationSourceName, "CurrentUserDidNotLoginToTheApplication")
- );
- }
- foreach (var authorizeAttribute in authorizeAttributes)
- {
- await PermissionChecker.AuthorizeAsync(authorizeAttribute.RequireAllPermissions, authorizeAttribute.Permissions);
- }
- }
- public virtual async Task AuthorizeAsync(MethodInfo methodInfo, Type type)
- {
- await CheckFeatures(methodInfo, type);
- await CheckPermissions(methodInfo, type);
- }
- protected virtual async Task CheckFeatures(MethodInfo methodInfo, Type type)
- {
- var featureAttributes = ReflectionHelper.GetAttributesOfMemberAndType<RequiresFeatureAttribute>(methodInfo, type);
- if (featureAttributes.Count <= 0)
- {
- return;
- }
- foreach (var featureAttribute in featureAttributes)
- {
- await _featureChecker.CheckEnabledAsync(featureAttribute.RequiresAll, featureAttribute.Features);
- }
- }
- protected virtual async Task CheckPermissions(MethodInfo methodInfo, Type type)
- {
- if (!_authConfiguration.IsEnabled)
- {
- return;
- }
- if (AllowAnonymous(methodInfo, type))
- {
- return;
- }
- if (ReflectionHelper.IsPropertyGetterSetterMethod(methodInfo, type))
- {
- return;
- }
- var authorizeAttributes =
- ReflectionHelper
- .GetAttributesOfMemberAndType(methodInfo, type)
- .OfType<IAbpAuthorizeAttribute>()
- .ToArray();
- if (!authorizeAttributes.Any())
- {
- return;
- }
- await AuthorizeAsync(authorizeAttributes);
- }
- private static bool AllowAnonymous(MemberInfo memberInfo, Type type)
- {
- return ReflectionHelper
- .GetAttributesOfMemberAndType(memberInfo, type)
- .OfType<IAbpAllowAnonymousAttribute>()
- .Any();
- }
- }
- }
|