Worked on permission system design.

pull/208/head
Halil İbrahim Kalkan 7 years ago
parent dbbe2545f1
commit 06a5b136bf

@ -8,10 +8,10 @@
}
@section scripts {
<script type="text/javascript" src="~/modules/identity/helpers/jquery.js"></script>
<script type="text/javascript" src="~/modules/identity/helpers/datatables.extensions.js"></script>
<script type="text/javascript" src="~/modules/identity/helpers/ResourceLoader.js"></script>
<script type="text/javascript" src="~/modules/identity/helpers/ModalManager.js"></script>
<script type="text/javascript" src="~/abp/helpers/jquery.js"></script>
<script type="text/javascript" src="~/abp/helpers/datatables.extensions.js"></script>
<script type="text/javascript" src="~/abp/helpers/ResourceLoader.js"></script>
<script type="text/javascript" src="~/abp/helpers/ModalManager.js"></script>
<script type="text/javascript" src="~/modules/identity/views/roles/index.js"></script>
}

@ -6,7 +6,7 @@ namespace Volo.Abp.Permissions
{
public interface IPermissionChecker
{
Task<bool> IsGrantedAsync([NotNull]string name);
Task<PermissionGrantInfo> CheckAsync([NotNull]string name);
Task<List<PermissionGrantInfo>> GetAllAsync();
}

@ -1,5 +1,4 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
namespace Volo.Abp.Permissions
{
@ -7,10 +6,6 @@ namespace Volo.Abp.Permissions
{
string Name { get; }
//Task SetAsync([NotNull] PermissionDefinition permission, bool isGranted, [CanBeNull] string providerKey);
//Task ClearAsync([NotNull] PermissionDefinition permission, [CanBeNull] string providerKey);
Task<bool> IsGrantedAsync(PermissionDefinition permission);
Task<PermissionValueProviderGrantInfo> CheckAsync(PermissionDefinition permission);
}
}

@ -34,19 +34,11 @@ namespace Volo.Abp.Permissions
);
}
public virtual async Task<bool> IsGrantedAsync(string name)
public Task<PermissionGrantInfo> CheckAsync(string name)
{
var permission = PermissionDefinitionManager.Get(name);
foreach (var provider in Providers)
{
if (await provider.IsGrantedAsync(permission))
{
return true;
}
}
return false;
return GetPermissionGrantInfo(permission);
}
public virtual async Task<List<PermissionGrantInfo>> GetAllAsync()
@ -66,9 +58,10 @@ namespace Volo.Abp.Permissions
{
foreach (var provider in Providers)
{
if (await provider.IsGrantedAsync(permission))
var result = await provider.CheckAsync(permission);
if (result.IsGranted)
{
return new PermissionGrantInfo(permission.Name, true, provider.Name);
return new PermissionGrantInfo(permission.Name, true, provider.Name, result.ProviderKey);
}
}

@ -0,0 +1,12 @@
using System.Threading.Tasks;
namespace Volo.Abp.Permissions
{
public static class PermissionCheckerExtensions
{
public static async Task<bool> IsGrantedAsync(this IPermissionChecker permissionChecker, string name)
{
return (await permissionChecker.CheckAsync(name)).IsGranted;
}
}
}

@ -10,13 +10,16 @@ namespace Volo.Abp.Permissions
public string ProviderName { get; }
public PermissionGrantInfo([NotNull] string name, bool isGranted, [CanBeNull] string providerName = null)
public string ProviderKey { get; }
public PermissionGrantInfo([NotNull] string name, bool isGranted, [CanBeNull] string providerName = null, [CanBeNull] string providerKey = null)
{
Check.NotNull(name, nameof(name));
Name = name;
IsGranted = isGranted;
ProviderName = providerName;
ProviderKey = providerKey;
}
}
}

@ -1,7 +1,4 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Permissions
@ -10,24 +7,13 @@ namespace Volo.Abp.Permissions
{
public abstract string Name { get; }
public ILoggerFactory LoggerFactory { get; set; }
protected IPermissionStore PermissionStore { get; }
protected ILogger Logger => _lazyLogger.Value;
private Lazy<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
protected PermissionValueProvider(IPermissionStore permissionStore)
{
PermissionStore = permissionStore;
}
//public abstract Task<bool?> IsGrantedAsync(PermissionDefinition permission, string providerName, string providerKey);
//public abstract Task SetAsync(PermissionDefinition permission, bool isGranted, string providerKey);
//public abstract Task ClearAsync(PermissionDefinition permission, string providerKey);
public abstract Task<bool> IsGrantedAsync(PermissionDefinition permission);
public abstract Task<PermissionValueProviderGrantInfo> CheckAsync(PermissionDefinition permission);
}
}

@ -0,0 +1,19 @@
using JetBrains.Annotations;
namespace Volo.Abp.Permissions
{
public class PermissionValueProviderGrantInfo
{
public static PermissionValueProviderGrantInfo NonGranted { get; } = new PermissionValueProviderGrantInfo(false);
public virtual bool IsGranted { get; }
public virtual string ProviderKey { get; }
public PermissionValueProviderGrantInfo(bool isGranted, [CanBeNull] string providerKey = null)
{
IsGranted = isGranted;
ProviderKey = providerKey;
}
}
}

@ -18,22 +18,22 @@ namespace Volo.Abp.Session
CurrentUser = currentUser;
}
public override async Task<bool> IsGrantedAsync(PermissionDefinition permission)
public override async Task<PermissionValueProviderGrantInfo> CheckAsync(PermissionDefinition permission)
{
if (CurrentUser.Id == null || !CurrentUser.Roles.Any())
{
return false;
return PermissionValueProviderGrantInfo.NonGranted;
}
foreach (var role in CurrentUser.Roles)
{
if (await PermissionStore.IsGrantedAsync(permission.Name, Name, role))
{
return true;
return new PermissionValueProviderGrantInfo(true, role);
}
}
return false;
return PermissionValueProviderGrantInfo.NonGranted;
}
}
}

@ -17,14 +17,17 @@ namespace Volo.Abp.Session
CurrentUser = currentUser;
}
public override async Task<bool> IsGrantedAsync(PermissionDefinition permission)
public override async Task<PermissionValueProviderGrantInfo> CheckAsync(PermissionDefinition permission)
{
if (CurrentUser.Id == null)
{
return false;
return PermissionValueProviderGrantInfo.NonGranted;
}
return await PermissionStore.IsGrantedAsync(permission.Name, Name, CurrentUser.Id.Value.ToString());
return new PermissionValueProviderGrantInfo(
await PermissionStore.IsGrantedAsync(permission.Name, Name, CurrentUser.Id.Value.ToString()),
CurrentUser.Id.ToString()
);
}
}
}

Loading…
Cancel
Save