Define TenantPermissionValueProvider

pull/848/head
Halil ibrahim Kalkan 6 years ago
parent 79da56dd13
commit 3565c2824d

@ -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<bool> IsGrantedAsync(string name, string providerName, string providerKey)
{
return Task.FromResult(false);
return TaskCache.FalseResult;
}
}
}

@ -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));

@ -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<PermissionOptions>(options =>
{
options.ValueProviders.Add<TenantPermissionValueProvider>();
});
}
}
}

@ -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<PermissionGrantResult> 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;
}
}
}
}
Loading…
Cancel
Save