From 3565c2824d21b45908d5b9e96150a9e0e3995806 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Wed, 27 Feb 2019 11:20:36 +0300 Subject: [PATCH] Define TenantPermissionValueProvider --- .../Permissions/NullPermissionStore.cs | 3 +- .../PermissionValueCheckContext.cs | 4 +- .../AbpMultiTenancyAbstractionsModule.cs | 9 +++- .../TenantPermissionValueProvider.cs | 45 +++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantPermissionValueProvider.cs diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/NullPermissionStore.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/NullPermissionStore.cs index b5c5cb5a4b..57c76ac6c9 100644 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/NullPermissionStore.cs +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/NullPermissionStore.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; namespace Volo.Abp.Authorization.Permissions { @@ -16,7 +17,7 @@ namespace Volo.Abp.Authorization.Permissions public Task IsGrantedAsync(string name, string providerName, string providerKey) { - return Task.FromResult(false); + return TaskCache.FalseResult; } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionValueCheckContext.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionValueCheckContext.cs index 83602a0c46..bc39384938 100644 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionValueCheckContext.cs +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionValueCheckContext.cs @@ -11,7 +11,9 @@ namespace Volo.Abp.Authorization.Permissions [CanBeNull] public ClaimsPrincipal Principal { get; } - public PermissionValueCheckContext([NotNull] PermissionDefinition permission, [CanBeNull] ClaimsPrincipal principal) + public PermissionValueCheckContext( + [NotNull] PermissionDefinition permission, + [CanBeNull] ClaimsPrincipal principal) { Check.NotNull(permission, nameof(permission)); diff --git a/framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/AbpMultiTenancyAbstractionsModule.cs b/framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/AbpMultiTenancyAbstractionsModule.cs index 6b21f53443..1b72b7d292 100644 --- a/framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/AbpMultiTenancyAbstractionsModule.cs +++ b/framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/AbpMultiTenancyAbstractionsModule.cs @@ -1,4 +1,5 @@ using Volo.Abp.Authorization; +using Volo.Abp.Authorization.Permissions; using Volo.Abp.Data; using Volo.Abp.Modularity; @@ -10,6 +11,12 @@ namespace Volo.Abp.MultiTenancy )] public class AbpMultiTenancyAbstractionsModule : AbpModule //TODO: Rename to AbpMultiTenancyModule? { - + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.ValueProviders.Add(); + }); + } } } diff --git a/framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantPermissionValueProvider.cs b/framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantPermissionValueProvider.cs new file mode 100644 index 0000000000..94bd018823 --- /dev/null +++ b/framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantPermissionValueProvider.cs @@ -0,0 +1,45 @@ +using System.Threading.Tasks; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Security.Claims; + +namespace Volo.Abp.MultiTenancy +{ + public class TenantPermissionValueProvider : PermissionValueProvider + { + public const string ProviderName = "Tenant"; + + public override string Name => ProviderName; + + protected ICurrentTenant CurrentTenant { get; } + + public TenantPermissionValueProvider( + IPermissionStore permissionStore, + ICurrentTenant currentTenant) + : base(permissionStore) + { + CurrentTenant = currentTenant; + } + + public override async Task CheckAsync(PermissionValueCheckContext context) + { + if (!context.Permission.IsFeature) + { + return PermissionGrantResult.Undefined; + } + + var tenantId = context.Principal?.FindFirst(AbpClaimTypes.TenantId)?.Value; + + if (tenantId == null) + { + return PermissionGrantResult.Undefined; + } + + using (CurrentTenant.Change(null)) + { + return await PermissionStore.IsGrantedAsync(context.Permission.Name, Name, tenantId) + ? PermissionGrantResult.Granted + : PermissionGrantResult.Undefined; + } + } + } +}