mirror of https://github.com/abpframework/abp.git
19 changed files with 304 additions and 420 deletions
@ -0,0 +1,32 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Permissions |
|||
{ |
|||
public static class UserPermissionManagerExtensions |
|||
{ |
|||
private const string ProviderName = "User"; //TODO: Share same const with UserPermissionValueProvider
|
|||
|
|||
public static Task<bool> IsGrantedForUserAsync(this IPermissionManager permissionManager, [NotNull] string name, Guid userId) |
|||
{ |
|||
return permissionManager.IsGrantedAsync(name, ProviderName, userId.ToString()); |
|||
} |
|||
|
|||
public static Task<List<string>> GetAllGrantedForUserAsync(this IPermissionManager permissionManager, Guid userId) |
|||
{ |
|||
return permissionManager.GetAllGrantedAsync(ProviderName, userId.ToString()); |
|||
} |
|||
|
|||
public static Task GrantForUserAsync(this IPermissionManager permissionManager, Guid userId, [NotNull] string name) |
|||
{ |
|||
return permissionManager.GrantAsync(name, ProviderName, userId.ToString()); |
|||
} |
|||
|
|||
public static Task RevokeForUserAsync(this IPermissionManager permissionManager, Guid userId, [NotNull] string name) |
|||
{ |
|||
return permissionManager.RevokeAsync(name, ProviderName, userId.ToString()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Permissions |
|||
{ |
|||
public interface IPermissionManager |
|||
{ |
|||
Task<bool> IsGrantedAsync([NotNull]string name, [NotNull] string providerName, [NotNull] string providerKey); |
|||
|
|||
Task<List<string>> GetAllGrantedAsync([NotNull] string providerName, [NotNull] string providerKey); |
|||
|
|||
Task GrantAsync( [NotNull] string providerName, [NotNull] string providerKey, [NotNull] string name); |
|||
|
|||
Task RevokeAsync([NotNull] string name, [NotNull] string providerName, [NotNull] string providerKey); |
|||
} |
|||
} |
|||
@ -0,0 +1,85 @@ |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Guids; |
|||
|
|||
namespace Volo.Abp.Permissions |
|||
{ |
|||
public class PermissionManager : IPermissionManager, ISingletonDependency |
|||
{ |
|||
protected IPermissionGrantRepository PermissionGrantRepository { get; } |
|||
protected IPermissionDefinitionManager PermissionDefinitionManager { get; } |
|||
protected IGuidGenerator GuidGenerator { get; } |
|||
|
|||
public PermissionManager( |
|||
IPermissionDefinitionManager permissionDefinitionManager, |
|||
IPermissionGrantRepository permissionGrantRepository, |
|||
IGuidGenerator guidGenerator) |
|||
{ |
|||
GuidGenerator = guidGenerator; |
|||
PermissionGrantRepository = permissionGrantRepository; |
|||
PermissionDefinitionManager = permissionDefinitionManager; |
|||
} |
|||
|
|||
public async Task<bool> IsGrantedAsync(string providerName, string providerKey, string name) |
|||
{ |
|||
Check.NotNull(providerName, nameof(providerName)); |
|||
Check.NotNull(providerKey, nameof(providerKey)); |
|||
Check.NotNull(name, nameof(name)); |
|||
|
|||
return await PermissionGrantRepository.FindAsync(name, providerName, providerKey) != null; |
|||
} |
|||
|
|||
public async Task<List<string>> GetAllGrantedAsync(string providerName, string providerKey) |
|||
{ |
|||
Check.NotNull(providerName, nameof(providerName)); |
|||
Check.NotNull(providerKey, nameof(providerKey)); |
|||
|
|||
return (await PermissionGrantRepository.GetListAsync(providerName, providerKey)) |
|||
.Select(p => p.Name) |
|||
.ToList(); |
|||
} |
|||
|
|||
public async Task GrantAsync(string providerName, string providerKey, string name) |
|||
{ |
|||
Check.NotNull(providerName, nameof(providerName)); |
|||
Check.NotNull(providerKey, nameof(providerKey)); |
|||
Check.NotNull(name, nameof(name)); |
|||
|
|||
if (await IsGrantedAsync(providerName, providerKey, name)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
await PermissionGrantRepository.InsertAsync( |
|||
new PermissionGrant( |
|||
GuidGenerator.Create(), |
|||
name, |
|||
providerName, |
|||
providerKey |
|||
) |
|||
); |
|||
} |
|||
|
|||
public async Task RevokeAsync(string providerName, string providerKey, string name) |
|||
{ |
|||
Check.NotNull(providerName, nameof(providerName)); |
|||
Check.NotNull(providerKey, nameof(providerKey)); |
|||
Check.NotNull(name, nameof(name)); |
|||
|
|||
if (await IsGrantedAsync(providerName, providerKey, name)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var grant = await PermissionGrantRepository.FindAsync(name, providerName, providerKey); |
|||
if (grant == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
await PermissionGrantRepository.DeleteAsync(grant); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Permissions |
|||
{ |
|||
public interface IPermissionChecker |
|||
{ |
|||
Task<bool> IsGrantedAsync([NotNull]string name); |
|||
|
|||
Task<List<PermissionGrantInfo>> GetAllAsync(); |
|||
} |
|||
} |
|||
@ -1,21 +0,0 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Permissions |
|||
{ |
|||
//TODO: Change fallback to inherit?
|
|||
|
|||
public interface IPermissionManager |
|||
{ |
|||
Task<bool> IsGrantedAsync([NotNull]string name); |
|||
|
|||
Task<bool> IsGrantedAsync([NotNull]string name, [NotNull] string providerName, [CanBeNull] string providerKey, bool fallback = true); |
|||
|
|||
Task<List<PermissionGrantInfo>> GetAllAsync(); |
|||
|
|||
Task<List<PermissionGrantInfo>> GetAllAsync([NotNull] string providerName, [CanBeNull] string providerKey, bool fallback = true); |
|||
|
|||
Task SetAsync([NotNull] string name, bool isGranted, [NotNull] string providerName, [CanBeNull] string providerKey, bool forceToSet = false); |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Permissions |
|||
{ |
|||
public class PermissionChecker : IPermissionChecker, ITransientDependency |
|||
{ |
|||
protected IPermissionDefinitionManager PermissionDefinitionManager { get; } |
|||
|
|||
protected Lazy<List<IPermissionValueProvider>> Providers { get; } |
|||
|
|||
protected PermissionOptions Options { get; } |
|||
|
|||
public PermissionChecker( |
|||
IOptions<PermissionOptions> options, |
|||
IServiceProvider serviceProvider, |
|||
IPermissionDefinitionManager permissionDefinitionManager) |
|||
{ |
|||
PermissionDefinitionManager = permissionDefinitionManager; |
|||
Options = options.Value; |
|||
|
|||
Providers = new Lazy<List<IPermissionValueProvider>>( |
|||
() => Options |
|||
.ValueProviders |
|||
.Select(c => serviceProvider.GetRequiredService(c) as IPermissionValueProvider) |
|||
.ToList(), |
|||
true |
|||
); |
|||
} |
|||
|
|||
public async Task<bool> IsGrantedAsync(string name) |
|||
{ |
|||
var permission = PermissionDefinitionManager.Get(name); |
|||
|
|||
foreach (var provider in Providers.Value) |
|||
{ |
|||
if (await provider.IsGrantedAsync(permission)) |
|||
{ |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public virtual async Task<List<PermissionGrantInfo>> GetAllAsync() |
|||
{ |
|||
var permissionDefinitions = PermissionDefinitionManager.GetAll(); |
|||
var permissionGrantInfos = new Dictionary<string, PermissionGrantInfo>(); |
|||
|
|||
foreach (var permission in permissionDefinitions) |
|||
{ |
|||
permissionGrantInfos[permission.Name] = await GetPermissionGrantInfo(permission); |
|||
} |
|||
|
|||
return permissionGrantInfos.Values.ToList(); |
|||
} |
|||
|
|||
private async Task<PermissionGrantInfo> GetPermissionGrantInfo(PermissionDefinition permission) |
|||
{ |
|||
foreach (var provider in Providers.Value) |
|||
{ |
|||
if (await provider.IsGrantedAsync(permission)) |
|||
{ |
|||
return new PermissionGrantInfo(permission.Name, true, provider.Name); |
|||
} |
|||
} |
|||
|
|||
return new PermissionGrantInfo(permission.Name, false); |
|||
} |
|||
} |
|||
} |
|||
@ -1,168 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Permissions |
|||
{ |
|||
public class PermissionManager : IPermissionManager, ISingletonDependency |
|||
{ |
|||
protected IPermissionDefinitionManager PermissionDefinitionManager { get; } |
|||
|
|||
protected Lazy<List<IPermissionValueProvider>> Providers { get; } |
|||
|
|||
protected PermissionOptions Options { get; } |
|||
|
|||
protected IPermissionStore PermissionStore { get; } |
|||
|
|||
public PermissionManager( |
|||
IOptions<PermissionOptions> options, |
|||
IServiceProvider serviceProvider, |
|||
IPermissionDefinitionManager permissionDefinitionManager, |
|||
IPermissionStore permissionStore) |
|||
{ |
|||
PermissionStore = permissionStore; |
|||
PermissionDefinitionManager = permissionDefinitionManager; |
|||
Options = options.Value; |
|||
|
|||
Providers = new Lazy<List<IPermissionValueProvider>>( |
|||
() => Options |
|||
.ValueProviders |
|||
.Select(c => serviceProvider.GetRequiredService(c) as IPermissionValueProvider) |
|||
.ToList(), |
|||
true |
|||
); |
|||
} |
|||
|
|||
public virtual Task<bool> IsGrantedAsync(string name) |
|||
{ |
|||
Check.NotNull(name, nameof(name)); |
|||
|
|||
return IsGrantedInternalAsync(name, null, null); |
|||
} |
|||
|
|||
public virtual Task<bool> IsGrantedAsync(string name, string providerName, string providerKey, bool fallback = true) |
|||
{ |
|||
Check.NotNull(name, nameof(name)); |
|||
Check.NotNull(providerName, nameof(providerName)); |
|||
|
|||
return IsGrantedInternalAsync(name, providerName, providerKey); |
|||
} |
|||
|
|||
public virtual async Task<bool> IsGrantedInternalAsync(string name, string providerName, string providerKey, bool fallback = true) |
|||
{ |
|||
var permission = PermissionDefinitionManager.Get(name); |
|||
var providers = Enumerable |
|||
.Reverse(Providers.Value); |
|||
|
|||
if (providerName != null) |
|||
{ |
|||
providers = providers.SkipWhile(c => c.Name != providerName); |
|||
} |
|||
|
|||
if (!fallback) |
|||
{ |
|||
providers = providers.TakeWhile(c => c.Name == providerName); |
|||
} |
|||
|
|||
foreach (var provider in providers) |
|||
{ |
|||
var value = await provider.IsGrantedAsync(permission, providerName, providerKey); |
|||
if (value != null) |
|||
{ |
|||
return value.Value; |
|||
} |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public virtual Task<List<PermissionGrantInfo>> GetAllAsync() |
|||
{ |
|||
return GetAllFromProvidersAsync(Providers.Value, null); |
|||
} |
|||
|
|||
public virtual async Task<List<PermissionGrantInfo>> GetAllAsync(string providerName, string providerKey, bool fallback = true) |
|||
{ |
|||
Check.NotNull(providerName, nameof(providerName)); |
|||
|
|||
var providers = Enumerable.Reverse(Providers.Value) |
|||
.SkipWhile(c => c.Name != providerName); |
|||
|
|||
if (!fallback) |
|||
{ |
|||
providers = providers.TakeWhile(c => c.Name == providerName); |
|||
} |
|||
|
|||
var providerList = providers.Reverse().ToList(); |
|||
|
|||
return await GetAllFromProvidersAsync(providerList, providerKey); |
|||
} |
|||
|
|||
public virtual async Task SetAsync(string name, bool isGranted, string providerName, string providerKey, bool forceToSet = false) |
|||
{ |
|||
Check.NotNull(name, nameof(name)); |
|||
Check.NotNull(providerName, nameof(providerName)); |
|||
|
|||
var permission = PermissionDefinitionManager.Get(name); |
|||
|
|||
var providers = Enumerable |
|||
.Reverse(Providers.Value) |
|||
.SkipWhile(p => p.Name != providerName) |
|||
.ToList(); |
|||
|
|||
if (!providers.Any()) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
if (providers.Count > 1 && !forceToSet) |
|||
{ |
|||
//Clear the value if it's same as it's fallback value
|
|||
var fallbackValue = await IsGrantedInternalAsync(name, providers[1].Name, providerKey); |
|||
if (fallbackValue == isGranted) |
|||
{ |
|||
return; |
|||
} |
|||
} |
|||
|
|||
providers = providers |
|||
.TakeWhile(p => p.Name == providerName) |
|||
.ToList(); //Getting list for case of there are more than one provider with same name
|
|||
|
|||
foreach (var provider in providers) |
|||
{ |
|||
await provider.SetAsync(permission, isGranted, providerKey); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task<List<PermissionGrantInfo>> GetAllFromProvidersAsync( |
|||
[NotNull] List<IPermissionValueProvider> providers, |
|||
[CanBeNull] string providerKey) |
|||
{ |
|||
var permissionDefinitions = PermissionDefinitionManager.GetAll(); |
|||
var permissionGrantInfos = new Dictionary<string, PermissionGrantInfo>(); |
|||
|
|||
foreach (var permission in permissionDefinitions) |
|||
{ |
|||
permissionGrantInfos[permission.Name] = new PermissionGrantInfo(permission.Name, false); |
|||
|
|||
foreach (var provider in providers) |
|||
{ |
|||
var value = await provider.IsGrantedAsync(permission, provider.Name, providerKey); |
|||
if (value != null) |
|||
{ |
|||
permissionGrantInfos[permission.Name] = |
|||
new PermissionGrantInfo(permission.Name, value.Value, provider.Name); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return permissionGrantInfos.Values.ToList(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,41 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Session; |
|||
|
|||
namespace Volo.Abp.Permissions |
|||
{ |
|||
public static class UserPermissionManagerExtensions |
|||
{ |
|||
public static Task<bool> IsGrantedForUserAsync(this IPermissionManager permissionManager, [NotNull] string name, Guid userId, bool fallback = true) |
|||
{ |
|||
return permissionManager.IsGrantedAsync(name, UserPermissionValueProvider.ProviderName, userId.ToString(), fallback); |
|||
} |
|||
|
|||
public static Task<bool> IsGrantedForCurrentUserAsync(this IPermissionManager permissionManager, [NotNull] string name, bool fallback = true) |
|||
{ |
|||
return permissionManager.IsGrantedAsync(name, UserPermissionValueProvider.ProviderName, null, fallback); |
|||
} |
|||
|
|||
public static Task<List<PermissionGrantInfo>> GetAllForUserAsync(this IPermissionManager permissionManager, Guid userId, bool fallback = true) |
|||
{ |
|||
return permissionManager.GetAllAsync(UserPermissionValueProvider.ProviderName, userId.ToString(), fallback); |
|||
} |
|||
|
|||
public static Task<List<PermissionGrantInfo>> GetAllForCurrentUserAsync(this IPermissionManager permissionManager, bool fallback = true) |
|||
{ |
|||
return permissionManager.GetAllAsync(UserPermissionValueProvider.ProviderName, null, fallback); |
|||
} |
|||
|
|||
public static Task SetForUserAsync(this IPermissionManager permissionManager, Guid userId, [NotNull] string name, bool isGranted, bool forceToSet = false) |
|||
{ |
|||
return permissionManager.SetAsync(name, isGranted, UserPermissionValueProvider.ProviderName, userId.ToString(), forceToSet); |
|||
} |
|||
|
|||
public static Task SetForCurrentUserAsync(this IPermissionManager permissionManager, [NotNull] string name, bool isGranted, bool forceToSet = false) |
|||
{ |
|||
return permissionManager.SetAsync(name, isGranted, UserPermissionValueProvider.ProviderName, null, forceToSet); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue