diff --git a/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RolePermissionManagementProvider.cs b/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RolePermissionManagementProvider.cs index 57a8a0975e..17d30c6845 100644 --- a/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RolePermissionManagementProvider.cs +++ b/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RolePermissionManagementProvider.cs @@ -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 CheckAsync(string name, string providerName, string providerKey) + public override async Task 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); diff --git a/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/UserPermissionManagementProvider.cs b/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/UserPermissionManagementProvider.cs index f0969a758a..5aa3eac7e5 100644 --- a/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/UserPermissionManagementProvider.cs +++ b/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/UserPermissionManagementProvider.cs @@ -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 CheckAsync(string name, string providerName, string providerKey) + public override async Task 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 ); } diff --git a/src/Volo.Abp.Permissions.Application/Volo/Abp/Permissions/PermissionAppService.cs b/src/Volo.Abp.Permissions.Application/Volo/Abp/Permissions/PermissionAppService.cs index 5de9cef456..c5e67aea0e 100644 --- a/src/Volo.Abp.Permissions.Application/Volo/Abp/Permissions/PermissionAppService.cs +++ b/src/Volo.Abp.Permissions.Application/Volo/Abp/Permissions/PermissionAppService.cs @@ -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); + } } } } diff --git a/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/IPermissionManagementProvider.cs b/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/IPermissionManagementProvider.cs index 0b1d952b42..e1c15d3f98 100644 --- a/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/IPermissionManagementProvider.cs +++ b/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/IPermissionManagementProvider.cs @@ -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 CheckAsync(string name, string providerName, string providerKey); + Task 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); } } \ No newline at end of file diff --git a/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/IPermissionManager.cs b/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/IPermissionManager.cs index 71020f2358..9a47905494 100644 --- a/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/IPermissionManager.cs +++ b/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/IPermissionManager.cs @@ -10,8 +10,6 @@ namespace Volo.Abp.Permissions Task> 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); } } \ No newline at end of file diff --git a/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/PermissionManagementProvider.cs b/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/PermissionManagementProvider.cs new file mode 100644 index 0000000000..fad9a9ab64 --- /dev/null +++ b/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/PermissionManagementProvider.cs @@ -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 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); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/PermissionManager.cs b/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/PermissionManager.cs index 7e1d31f2bd..11e4f34c29 100644 --- a/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/PermissionManager.cs +++ b/src/Volo.Abp.Permissions.Domain/Volo/Abp/Permissions/PermissionManager.cs @@ -22,7 +22,7 @@ namespace Volo.Abp.Permissions protected PermissionManagementOptions Options { get; } private readonly Lazy> _lazyProviders; - + public PermissionManager( IPermissionDefinitionManager permissionDefinitionManager, IPermissionGrantRepository permissionGrantRepository, @@ -44,66 +44,6 @@ namespace Volo.Abp.Permissions ); } - //public async Task 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> 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 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 GetInternalAsync(PermissionDefinition permissionDefinition, string providerName, string providerKey) { var result = new PermissionWithGrantedProviders(permissionDefinition.Name, false);