mirror of https://github.com/abpframework/abp.git
committed by
GitHub
65 changed files with 582 additions and 28 deletions
@ -0,0 +1,3 @@ |
|||||
|
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
||||
|
<ConfigureAwait ContinueOnCapturedContext="false" /> |
||||
|
</Weavers> |
||||
@ -0,0 +1,30 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
||||
|
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> |
||||
|
<xs:element name="Weavers"> |
||||
|
<xs:complexType> |
||||
|
<xs:all> |
||||
|
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> |
||||
|
<xs:complexType> |
||||
|
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> |
||||
|
</xs:complexType> |
||||
|
</xs:element> |
||||
|
</xs:all> |
||||
|
<xs:attribute name="VerifyAssembly" type="xs:boolean"> |
||||
|
<xs:annotation> |
||||
|
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> |
||||
|
</xs:annotation> |
||||
|
</xs:attribute> |
||||
|
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> |
||||
|
<xs:annotation> |
||||
|
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> |
||||
|
</xs:annotation> |
||||
|
</xs:attribute> |
||||
|
<xs:attribute name="GenerateXsd" type="xs:boolean"> |
||||
|
<xs:annotation> |
||||
|
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> |
||||
|
</xs:annotation> |
||||
|
</xs:attribute> |
||||
|
</xs:complexType> |
||||
|
</xs:element> |
||||
|
</xs:schema> |
||||
@ -0,0 +1,25 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="..\..\..\configureawait.props" /> |
||||
|
<Import Project="..\..\..\common.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netstandard2.0</TargetFramework> |
||||
|
<AssemblyName>Volo.Abp.Authorization.Abstractions</AssemblyName> |
||||
|
<PackageId>Volo.Abp.Authorization.Abstractions</PackageId> |
||||
|
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="$(MicrosoftPackageVersion)" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\Volo.Abp.MultiTenancy\Volo.Abp.MultiTenancy.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,13 @@ |
|||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpMultiTenancyModule) |
||||
|
)] |
||||
|
public class AbpAuthorizationAbstractionsModule : AbpModule |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.Permissions |
||||
|
{ |
||||
|
public interface IPermissionStateManager |
||||
|
{ |
||||
|
Task<bool> IsEnabledAsync(PermissionDefinition permission); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.Permissions |
||||
|
{ |
||||
|
public interface IPermissionStateProvider |
||||
|
{ |
||||
|
Task<bool> IsEnabledAsync(PermissionStateContext context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.Permissions |
||||
|
{ |
||||
|
public static class PermissionDefinitionExtensions |
||||
|
{ |
||||
|
public static PermissionDefinition AddStateProviders( |
||||
|
[NotNull] this PermissionDefinition permissionDefinition, |
||||
|
[NotNull] params IPermissionStateProvider[] permissionStateProviders) |
||||
|
{ |
||||
|
Check.NotNull(permissionDefinition, nameof(permissionDefinition)); |
||||
|
Check.NotNull(permissionStateProviders, nameof(permissionStateProviders)); |
||||
|
|
||||
|
permissionDefinition.StateProviders.AddRange(permissionStateProviders); |
||||
|
|
||||
|
return permissionDefinition; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.Permissions |
||||
|
{ |
||||
|
public class PermissionStateContext |
||||
|
{ |
||||
|
public IServiceProvider ServiceProvider { get; set; } |
||||
|
|
||||
|
public PermissionDefinition Permission { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,51 @@ |
|||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.Permissions |
||||
|
{ |
||||
|
public class PermissionStateManager : IPermissionStateManager, ITransientDependency |
||||
|
{ |
||||
|
protected IServiceProvider ServiceProvider { get; } |
||||
|
protected AbpPermissionOptions Options { get; } |
||||
|
|
||||
|
public PermissionStateManager(IServiceProvider serviceProvider, IOptions<AbpPermissionOptions> options) |
||||
|
{ |
||||
|
ServiceProvider = serviceProvider; |
||||
|
Options = options.Value; |
||||
|
} |
||||
|
|
||||
|
public async Task<bool> IsEnabledAsync(PermissionDefinition permission) |
||||
|
{ |
||||
|
using (var scope = ServiceProvider.CreateScope()) |
||||
|
{ |
||||
|
var context = new PermissionStateContext |
||||
|
{ |
||||
|
Permission = permission, |
||||
|
ServiceProvider = scope.ServiceProvider.GetRequiredService<ICachedServiceProvider>() |
||||
|
}; |
||||
|
|
||||
|
foreach (var provider in permission.StateProviders) |
||||
|
{ |
||||
|
if (!await provider.IsEnabledAsync(context)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
foreach (IPermissionStateProvider provider in Options.GlobalStateProviders.Select(x => ServiceProvider.GetRequiredService(x))) |
||||
|
{ |
||||
|
if (!await provider.IsEnabledAsync(context)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.DependencyInjection |
||||
|
{ |
||||
|
[ExposeServices(typeof(ICachedServiceProvider))] |
||||
|
public class CachedServiceProvider : ICachedServiceProvider, IScopedDependency |
||||
|
{ |
||||
|
protected IServiceProvider ServiceProvider { get; } |
||||
|
|
||||
|
protected IDictionary<Type, object> CachedServices { get; } |
||||
|
|
||||
|
public CachedServiceProvider(IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
ServiceProvider = serviceProvider; |
||||
|
|
||||
|
CachedServices = new Dictionary<Type, object> |
||||
|
{ |
||||
|
{typeof(IServiceProvider), serviceProvider} |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
public object GetService(Type serviceType) |
||||
|
{ |
||||
|
return CachedServices.GetOrAdd( |
||||
|
serviceType, |
||||
|
() => ServiceProvider.GetService(serviceType) |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.DependencyInjection |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Provides services by caching the resolved services.
|
||||
|
/// It caches all type of services including transients.
|
||||
|
/// This service's lifetime is scoped and it should be used
|
||||
|
/// for a limited scope.
|
||||
|
/// </summary>
|
||||
|
public interface ICachedServiceProvider : IServiceProvider |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
using JetBrains.Annotations; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
|
||||
|
namespace Volo.Abp.Features |
||||
|
{ |
||||
|
public static class FeatureDefinitionExtensions |
||||
|
{ |
||||
|
public static PermissionDefinition RequireFeatures( |
||||
|
[NotNull] this PermissionDefinition permissionDefinition, |
||||
|
params string[] features) |
||||
|
{ |
||||
|
return permissionDefinition.RequireFeatures(true, features); |
||||
|
} |
||||
|
|
||||
|
public static PermissionDefinition RequireFeatures( |
||||
|
[NotNull] this PermissionDefinition permissionDefinition, |
||||
|
bool requiresAll, |
||||
|
params string[] features) |
||||
|
{ |
||||
|
Check.NotNull(permissionDefinition, nameof(permissionDefinition)); |
||||
|
Check.NotNullOrEmpty(features, nameof(features)); |
||||
|
|
||||
|
return permissionDefinition.AddStateProviders( |
||||
|
new RequireFeaturesPermissionStateProvider(requiresAll, features) |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
|
||||
|
namespace Volo.Abp.Features |
||||
|
{ |
||||
|
public class RequireFeaturesPermissionStateProvider : IPermissionStateProvider |
||||
|
{ |
||||
|
private readonly string[] _featureNames; |
||||
|
private readonly bool _requiresAll; |
||||
|
|
||||
|
public RequireFeaturesPermissionStateProvider(params string[] featureNames) |
||||
|
: this(true, featureNames) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public RequireFeaturesPermissionStateProvider(bool requiresAll, params string[] featureNames) |
||||
|
{ |
||||
|
Check.NotNullOrEmpty(featureNames, nameof(featureNames)); |
||||
|
|
||||
|
_requiresAll = requiresAll; |
||||
|
_featureNames = featureNames; |
||||
|
} |
||||
|
|
||||
|
public async Task<bool> IsEnabledAsync(PermissionStateContext context) |
||||
|
{ |
||||
|
var feature = context.ServiceProvider.GetRequiredService<IFeatureChecker>(); |
||||
|
return await feature.IsEnabledAsync(_requiresAll, _featureNames); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
using JetBrains.Annotations; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
|
||||
|
namespace Volo.Abp.GlobalFeatures |
||||
|
{ |
||||
|
public static class GlobalFeatureDefinitionExtensions |
||||
|
{ |
||||
|
public static PermissionDefinition RequireGlobalFeatures( |
||||
|
this PermissionDefinition permissionDefinition, |
||||
|
params string[] globalFeatures) |
||||
|
{ |
||||
|
return permissionDefinition.RequireGlobalFeatures(true, globalFeatures); |
||||
|
} |
||||
|
|
||||
|
public static PermissionDefinition RequireGlobalFeatures( |
||||
|
[NotNull] this PermissionDefinition permissionDefinition, |
||||
|
bool requiresAll, |
||||
|
params string[] globalFeatures) |
||||
|
{ |
||||
|
Check.NotNull(permissionDefinition, nameof(permissionDefinition)); |
||||
|
Check.NotNullOrEmpty(globalFeatures, nameof(globalFeatures)); |
||||
|
|
||||
|
return permissionDefinition.AddStateProviders( |
||||
|
new RequireGlobalFeaturesPermissionStateProvider(requiresAll, globalFeatures) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
|
||||
|
namespace Volo.Abp.GlobalFeatures |
||||
|
{ |
||||
|
public class RequireGlobalFeaturesPermissionStateProvider : IPermissionStateProvider |
||||
|
{ |
||||
|
private readonly string[] _globalFeatureNames; |
||||
|
private readonly bool _requiresAll; |
||||
|
|
||||
|
public RequireGlobalFeaturesPermissionStateProvider(params string[] globalFeatureNames) |
||||
|
: this(true, globalFeatureNames) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public RequireGlobalFeaturesPermissionStateProvider(bool requiresAll, params string[] globalFeatureNames) |
||||
|
{ |
||||
|
Check.NotNullOrEmpty(globalFeatureNames, nameof(globalFeatureNames)); |
||||
|
|
||||
|
_requiresAll = requiresAll; |
||||
|
_globalFeatureNames = globalFeatureNames; |
||||
|
} |
||||
|
|
||||
|
public Task<bool> IsEnabledAsync(PermissionStateContext context) |
||||
|
{ |
||||
|
bool isEnabled = _requiresAll |
||||
|
? _globalFeatureNames.All(x => GlobalFeatureManager.Instance.IsEnabled(x)) |
||||
|
: _globalFeatureNames.Any(x => GlobalFeatureManager.Instance.IsEnabled(x)); |
||||
|
|
||||
|
return Task.FromResult(isEnabled); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
using System; |
||||
|
using System.Security.Claims; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Volo.Abp.Security.Claims; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization |
||||
|
{ |
||||
|
public abstract class PermissionStateProvider_Tests : AuthorizationTestBase |
||||
|
{ |
||||
|
protected IPermissionStateManager PermissionStateManager { get; } |
||||
|
protected IPermissionDefinitionManager PermissionDefinitionManager { get; } |
||||
|
protected ICurrentPrincipalAccessor CurrentPrincipalAccessor { get; } |
||||
|
|
||||
|
public PermissionStateProvider_Tests() |
||||
|
{ |
||||
|
PermissionStateManager = GetRequiredService<IPermissionStateManager>(); |
||||
|
PermissionDefinitionManager = GetRequiredService<IPermissionDefinitionManager>(); |
||||
|
CurrentPrincipalAccessor = GetRequiredService<ICurrentPrincipalAccessor>(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class SpecifyPermissionStateProvider : PermissionStateProvider_Tests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public async Task PermissionState_Test() |
||||
|
{ |
||||
|
var myPermission1 = PermissionDefinitionManager.Get("MyPermission1"); |
||||
|
myPermission1.StateProviders.ShouldContain(x => x.GetType() == typeof(TestRequireEditionPermissionStateProvider)); |
||||
|
|
||||
|
(await PermissionStateManager.IsEnabledAsync(myPermission1)).ShouldBeFalse(); |
||||
|
|
||||
|
using (CurrentPrincipalAccessor.Change(new Claim(AbpClaimTypes.EditionId, Guid.NewGuid().ToString()))) |
||||
|
{ |
||||
|
(await PermissionStateManager.IsEnabledAsync(myPermission1)).ShouldBeTrue(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class GlobalPermissionStateProvider : PermissionStateProvider_Tests |
||||
|
{ |
||||
|
protected override void AfterAddApplication(IServiceCollection services) |
||||
|
{ |
||||
|
services.Configure<AbpPermissionOptions>(options => options.GlobalStateProviders.Add<TestGlobalRequireRolePermissionStateProvider>()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Global_PermissionState_Test() |
||||
|
{ |
||||
|
var myPermission2 = PermissionDefinitionManager.Get("MyPermission2"); |
||||
|
|
||||
|
(await PermissionStateManager.IsEnabledAsync(myPermission2)).ShouldBeFalse(); |
||||
|
|
||||
|
using (CurrentPrincipalAccessor.Change(new Claim(AbpClaimTypes.Role, "admin"))) |
||||
|
{ |
||||
|
(await PermissionStateManager.IsEnabledAsync(myPermission2)).ShouldBeTrue(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Security.Claims; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization |
||||
|
{ |
||||
|
public class TestGlobalRequireRolePermissionStateProvider : IPermissionStateProvider, ITransientDependency |
||||
|
{ |
||||
|
public Task<bool> IsEnabledAsync(PermissionStateContext context) |
||||
|
{ |
||||
|
var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>(); |
||||
|
return Task.FromResult(currentPrincipalAccessor.Principal != null && currentPrincipalAccessor.Principal.IsInRole("admin")); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
using System.Security.Principal; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Volo.Abp.Security.Claims; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization |
||||
|
{ |
||||
|
public class TestRequireEditionPermissionStateProvider : IPermissionStateProvider |
||||
|
{ |
||||
|
public Task<bool> IsEnabledAsync(PermissionStateContext context) |
||||
|
{ |
||||
|
var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>(); |
||||
|
return Task.FromResult(currentPrincipalAccessor.Principal?.FindEditionId() != null); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Volo.Abp.Security.Claims; |
||||
|
|
||||
|
namespace Volo.Abp.PermissionManagement |
||||
|
{ |
||||
|
public class TestRequireRolePermissionStateProvider : IPermissionStateProvider |
||||
|
{ |
||||
|
private readonly List<string> _allowRoles = new List<string>(); |
||||
|
|
||||
|
public TestRequireRolePermissionStateProvider(params string[] roles) |
||||
|
{ |
||||
|
_allowRoles.AddRange(roles); |
||||
|
} |
||||
|
|
||||
|
public Task<bool> IsEnabledAsync(PermissionStateContext context) |
||||
|
{ |
||||
|
var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>(); |
||||
|
return Task.FromResult(currentPrincipalAccessor.Principal != null && _allowRoles.Any(role => currentPrincipalAccessor.Principal.IsInRole(role))); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue