Completed basic update screen for permissions

pull/208/head
Halil İbrahim Kalkan 7 years ago
parent 52b6f31807
commit 1040f573ca

@ -1,30 +1,33 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Guids;
using Volo.Abp.Permissions;
namespace Volo.Abp.Identity
{
public class RolePermissionManagementProvider : IPermissionManagementProvider
public class RolePermissionManagementProvider : PermissionManagementProvider
{
public string Name => "Role";
public override string Name => "Role";
private readonly IIdentityUserRepository _identityUserRepository;
private readonly IPermissionGrantRepository _permissionGrantRepository;
public RolePermissionManagementProvider(
IPermissionGrantRepository permissionGrantRepository,
IPermissionGrantRepository permissionGrantRepository,
IGuidGenerator guidGenerator,
IIdentityUserRepository identityUserRepository)
: base(
permissionGrantRepository,
guidGenerator)
{
_permissionGrantRepository = permissionGrantRepository;
_identityUserRepository = identityUserRepository;
}
public async Task<PermissionValueProviderGrantInfo> CheckAsync(string name, string providerName, string providerKey)
public override async Task<PermissionValueProviderGrantInfo> CheckAsync(string name, string providerName, string providerKey)
{
if (providerName == Name)
{
return new PermissionValueProviderGrantInfo(
await _permissionGrantRepository.FindAsync(name, providerName, providerKey) != null,
await PermissionGrantRepository.FindAsync(name, providerName, providerKey) != null,
providerKey
);
}
@ -32,12 +35,11 @@ namespace Volo.Abp.Identity
if (providerName == "User")
{
var userId = Guid.Parse(providerKey);
var roleNames = await _identityUserRepository.GetRoleNamesAsync(userId);
foreach (var roleName in roleNames)
{
var pg = await _permissionGrantRepository.FindAsync(name, providerName, roleName);
var pg = await PermissionGrantRepository.FindAsync(name, providerName, roleName);
if (pg != null)
{
return new PermissionValueProviderGrantInfo(true, roleName);

@ -1,21 +1,23 @@
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Permissions;
namespace Volo.Abp.Identity
{
public class UserPermissionManagementProvider : IPermissionManagementProvider, ITransientDependency
public class UserPermissionManagementProvider : PermissionManagementProvider
{
public string Name => "User";
public override string Name => "User";
private readonly IPermissionGrantRepository _permissionGrantRepository;
public UserPermissionManagementProvider(IPermissionGrantRepository permissionGrantRepository)
public UserPermissionManagementProvider(IPermissionGrantRepository
permissionGrantRepository,
IGuidGenerator guidGenerator)
: base(
permissionGrantRepository,
guidGenerator)
{
_permissionGrantRepository = permissionGrantRepository;
}
public async Task<PermissionValueProviderGrantInfo> CheckAsync(string name, string providerName, string providerKey)
public override async Task<PermissionValueProviderGrantInfo> CheckAsync(string name, string providerName, string providerKey)
{
if (providerName != Name)
{
@ -23,7 +25,7 @@ namespace Volo.Abp.Identity
}
return new PermissionValueProviderGrantInfo(
await _permissionGrantRepository.FindAsync(name, providerName, providerKey) != null,
await PermissionGrantRepository.FindAsync(name, providerName, providerKey) != null,
providerKey
);
}

@ -73,7 +73,10 @@ namespace Volo.Abp.Permissions
public async Task UpdateAsync(string providerName, string providerKey, UpdatePermissionsDto input)
{
foreach (var permission in input.Permissions)
{
await _permissionManager.SetAsync(permission.Name, providerName, providerKey, permission.IsGranted);
}
}
}
}

@ -1,12 +1,17 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Permissions
{
public interface IPermissionManagementProvider : ISingletonDependency //TODO: Create an abstract base class.
public interface IPermissionManagementProvider : ISingletonDependency
{
string Name { get; }
Task<PermissionValueProviderGrantInfo> CheckAsync(string name, string providerName, string providerKey);
Task<PermissionValueProviderGrantInfo> CheckAsync([NotNull] string name, [NotNull] string providerName, [NotNull] string providerKey);
Task GrantAsync([NotNull] string name, [NotNull] string providerKey);
Task RevokeAsync([NotNull] string name, [NotNull] string providerKey);
}
}

@ -10,8 +10,6 @@ namespace Volo.Abp.Permissions
Task<List<PermissionWithGrantedProviders>> GetAllAsync([NotNull] string providerName, [NotNull] string providerKey);
//Task GrantAsync([NotNull] string name, [NotNull] string providerName, [NotNull] string providerKey);
//Task RevokeAsync([NotNull] string name, [NotNull] string providerName, [NotNull] string providerKey);
Task SetAsync(string name, string providerName, string providerKey, bool isGranted);
}
}

@ -0,0 +1,54 @@
using System.Threading.Tasks;
using Volo.Abp.Guids;
namespace Volo.Abp.Permissions
{
public abstract class PermissionManagementProvider : IPermissionManagementProvider
{
public abstract string Name { get; }
protected IPermissionGrantRepository PermissionGrantRepository { get; }
protected IGuidGenerator GuidGenerator { get; }
protected PermissionManagementProvider(
IPermissionGrantRepository permissionGrantRepository,
IGuidGenerator guidGenerator)
{
PermissionGrantRepository = permissionGrantRepository;
GuidGenerator = guidGenerator;
}
public abstract Task<PermissionValueProviderGrantInfo> CheckAsync(string name, string providerName, string providerKey);
public virtual async Task GrantAsync(string name, string providerKey)
{
var grant = await PermissionGrantRepository.FindAsync(name, Name, providerKey);
if (grant != null)
{
return;
}
await PermissionGrantRepository.InsertAsync(
new PermissionGrant(
GuidGenerator.Create(),
name,
Name,
providerKey
)
);
}
public virtual async Task RevokeAsync(string name, string providerKey)
{
var grant = await PermissionGrantRepository.FindAsync(name, Name, providerKey);
if (grant == null)
{
return;
}
await PermissionGrantRepository.DeleteAsync(grant);
}
}
}

@ -22,7 +22,7 @@ namespace Volo.Abp.Permissions
protected PermissionManagementOptions Options { get; }
private readonly Lazy<List<IPermissionManagementProvider>> _lazyProviders;
public PermissionManager(
IPermissionDefinitionManager permissionDefinitionManager,
IPermissionGrantRepository permissionGrantRepository,
@ -44,66 +44,6 @@ namespace Volo.Abp.Permissions
);
}
//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 name, string providerName, string providerKey)
//{
// Check.NotNull(name, nameof(name));
// Check.NotNull(providerName, nameof(providerName));
// Check.NotNull(providerKey, nameof(providerKey));
// 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);
//}
public async Task<PermissionWithGrantedProviders> GetAsync(string name, string providerName, string providerKey)
{
return await GetInternalAsync(PermissionDefinitionManager.Get(name), providerName, providerKey);
@ -121,6 +61,31 @@ namespace Volo.Abp.Permissions
return results;
}
public async Task SetAsync(string name, string providerName, string providerKey, bool isGranted)
{
var currentGrantInfo = await GetAsync(name, providerName, providerKey);
if (currentGrantInfo.IsGranted == isGranted)
{
return;
}
if (currentGrantInfo.IsGranted == false)
{
var provider = ManagementProviders.FirstOrDefault(m => m.Name == providerName);
await provider.GrantAsync(name, providerKey);
}
else
{
if (currentGrantInfo.Providers.All(p => p.Name != providerName))
{
return;
}
var provider = ManagementProviders.FirstOrDefault(m => m.Name == providerName);
await provider.RevokeAsync(name, providerKey);
}
}
public async Task<PermissionWithGrantedProviders> GetInternalAsync(PermissionDefinition permissionDefinition, string providerName, string providerKey)
{
var result = new PermissionWithGrantedProviders(permissionDefinition.Name, false);

Loading…
Cancel
Save