diff --git a/modules/permission-management/README.md b/modules/permission-management/README.md new file mode 100644 index 0000000000..086d6f3d30 --- /dev/null +++ b/modules/permission-management/README.md @@ -0,0 +1,2 @@ +# abp-permissionmanagement +Permission management module for ABP framework. diff --git a/modules/permission-management/Volo.Abp.PermissionManagement.sln b/modules/permission-management/Volo.Abp.PermissionManagement.sln new file mode 100644 index 0000000000..fb3873eb6c --- /dev/null +++ b/modules/permission-management/Volo.Abp.PermissionManagement.sln @@ -0,0 +1,102 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B559B878-38F7-49CC-BC06-43A32D68C1A7}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{63DA4A89-5908-4F37-B7E6-525AEEF20C77}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.Domain.Shared", "src\Volo.Abp.PermissionManagement.Domain.Shared\Volo.Abp.PermissionManagement.Domain.Shared.csproj", "{0B94EC37-84A7-487A-A764-335E81F3214B}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.Domain", "src\Volo.Abp.PermissionManagement.Domain\Volo.Abp.PermissionManagement.Domain.csproj", "{3D53B3FC-E6F1-4125-A502-1C5EFFAA1F10}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.Application.Contracts", "src\Volo.Abp.PermissionManagement.Application.Contracts\Volo.Abp.PermissionManagement.Application.Contracts.csproj", "{A375FAF9-4D87-4C7C-8E42-6EF87FB89F33}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.Application", "src\Volo.Abp.PermissionManagement.Application\Volo.Abp.PermissionManagement.Application.csproj", "{1B205C45-B5D7-4A75-9745-776A987E47F8}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.EntityFrameworkCore", "src\Volo.Abp.PermissionManagement.EntityFrameworkCore\Volo.Abp.PermissionManagement.EntityFrameworkCore.csproj", "{0A6772C8-A77C-4E85-8380-4870534E552C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.Web", "src\Volo.Abp.PermissionManagement.Web\Volo.Abp.PermissionManagement.Web.csproj", "{97A386F8-DAE0-4BEA-ADE5-1D57C3336515}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.Tests", "test\Volo.Abp.PermissionManagement.Tests\Volo.Abp.PermissionManagement.Tests.csproj", "{48297098-79D0-413B-939C-6C432142C42D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.PermissionManagement.TestBase", "test\Volo.Abp.PermissionManagement.TestBase\Volo.Abp.PermissionManagement.TestBase.csproj", "{49259427-CAEB-4FAE-81E4-848F789487EA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests", "test\Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests\Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests.csproj", "{6E710DE0-F3B1-45D7-85BA-F39D839D9B65}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.PermissionManagement.MongoDB", "src\Volo.Abp.PermissionManagement.MongoDB\Volo.Abp.PermissionManagement.MongoDB.csproj", "{903C1729-CEBC-4BCD-A102-8D1B1B5465AF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.PermissionManagement.MongoDB.Tests", "test\Volo.Abp.PermissionManagement.MongoDB.Tests\Volo.Abp.PermissionManagement.MongoDB.Tests.csproj", "{AFA65E08-1F17-4BBE-96A6-80F3CFE22A75}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0B94EC37-84A7-487A-A764-335E81F3214B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0B94EC37-84A7-487A-A764-335E81F3214B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0B94EC37-84A7-487A-A764-335E81F3214B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0B94EC37-84A7-487A-A764-335E81F3214B}.Release|Any CPU.Build.0 = Release|Any CPU + {3D53B3FC-E6F1-4125-A502-1C5EFFAA1F10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3D53B3FC-E6F1-4125-A502-1C5EFFAA1F10}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3D53B3FC-E6F1-4125-A502-1C5EFFAA1F10}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3D53B3FC-E6F1-4125-A502-1C5EFFAA1F10}.Release|Any CPU.Build.0 = Release|Any CPU + {A375FAF9-4D87-4C7C-8E42-6EF87FB89F33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A375FAF9-4D87-4C7C-8E42-6EF87FB89F33}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A375FAF9-4D87-4C7C-8E42-6EF87FB89F33}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A375FAF9-4D87-4C7C-8E42-6EF87FB89F33}.Release|Any CPU.Build.0 = Release|Any CPU + {1B205C45-B5D7-4A75-9745-776A987E47F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1B205C45-B5D7-4A75-9745-776A987E47F8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1B205C45-B5D7-4A75-9745-776A987E47F8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1B205C45-B5D7-4A75-9745-776A987E47F8}.Release|Any CPU.Build.0 = Release|Any CPU + {0A6772C8-A77C-4E85-8380-4870534E552C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0A6772C8-A77C-4E85-8380-4870534E552C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0A6772C8-A77C-4E85-8380-4870534E552C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0A6772C8-A77C-4E85-8380-4870534E552C}.Release|Any CPU.Build.0 = Release|Any CPU + {97A386F8-DAE0-4BEA-ADE5-1D57C3336515}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {97A386F8-DAE0-4BEA-ADE5-1D57C3336515}.Debug|Any CPU.Build.0 = Debug|Any CPU + {97A386F8-DAE0-4BEA-ADE5-1D57C3336515}.Release|Any CPU.ActiveCfg = Release|Any CPU + {97A386F8-DAE0-4BEA-ADE5-1D57C3336515}.Release|Any CPU.Build.0 = Release|Any CPU + {48297098-79D0-413B-939C-6C432142C42D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {48297098-79D0-413B-939C-6C432142C42D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {48297098-79D0-413B-939C-6C432142C42D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {48297098-79D0-413B-939C-6C432142C42D}.Release|Any CPU.Build.0 = Release|Any CPU + {49259427-CAEB-4FAE-81E4-848F789487EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {49259427-CAEB-4FAE-81E4-848F789487EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {49259427-CAEB-4FAE-81E4-848F789487EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {49259427-CAEB-4FAE-81E4-848F789487EA}.Release|Any CPU.Build.0 = Release|Any CPU + {6E710DE0-F3B1-45D7-85BA-F39D839D9B65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6E710DE0-F3B1-45D7-85BA-F39D839D9B65}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6E710DE0-F3B1-45D7-85BA-F39D839D9B65}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6E710DE0-F3B1-45D7-85BA-F39D839D9B65}.Release|Any CPU.Build.0 = Release|Any CPU + {903C1729-CEBC-4BCD-A102-8D1B1B5465AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {903C1729-CEBC-4BCD-A102-8D1B1B5465AF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {903C1729-CEBC-4BCD-A102-8D1B1B5465AF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {903C1729-CEBC-4BCD-A102-8D1B1B5465AF}.Release|Any CPU.Build.0 = Release|Any CPU + {AFA65E08-1F17-4BBE-96A6-80F3CFE22A75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AFA65E08-1F17-4BBE-96A6-80F3CFE22A75}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AFA65E08-1F17-4BBE-96A6-80F3CFE22A75}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AFA65E08-1F17-4BBE-96A6-80F3CFE22A75}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {0B94EC37-84A7-487A-A764-335E81F3214B} = {B559B878-38F7-49CC-BC06-43A32D68C1A7} + {3D53B3FC-E6F1-4125-A502-1C5EFFAA1F10} = {B559B878-38F7-49CC-BC06-43A32D68C1A7} + {A375FAF9-4D87-4C7C-8E42-6EF87FB89F33} = {B559B878-38F7-49CC-BC06-43A32D68C1A7} + {1B205C45-B5D7-4A75-9745-776A987E47F8} = {B559B878-38F7-49CC-BC06-43A32D68C1A7} + {0A6772C8-A77C-4E85-8380-4870534E552C} = {B559B878-38F7-49CC-BC06-43A32D68C1A7} + {97A386F8-DAE0-4BEA-ADE5-1D57C3336515} = {B559B878-38F7-49CC-BC06-43A32D68C1A7} + {48297098-79D0-413B-939C-6C432142C42D} = {63DA4A89-5908-4F37-B7E6-525AEEF20C77} + {49259427-CAEB-4FAE-81E4-848F789487EA} = {63DA4A89-5908-4F37-B7E6-525AEEF20C77} + {6E710DE0-F3B1-45D7-85BA-F39D839D9B65} = {63DA4A89-5908-4F37-B7E6-525AEEF20C77} + {903C1729-CEBC-4BCD-A102-8D1B1B5465AF} = {B559B878-38F7-49CC-BC06-43A32D68C1A7} + {AFA65E08-1F17-4BBE-96A6-80F3CFE22A75} = {63DA4A89-5908-4F37-B7E6-525AEEF20C77} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8FC7DF78-5E2D-489F-9D43-147D2ABAA112} + EndGlobalSection +EndGlobal diff --git a/modules/permission-management/common.props b/modules/permission-management/common.props new file mode 100644 index 0000000000..f00a3fc2cb --- /dev/null +++ b/modules/permission-management/common.props @@ -0,0 +1,16 @@ + + + latest + 0.3.0 + $(NoWarn);CS1591 + http://www.aspnetboilerplate.com/images/abp_nupkg.png + http://abp.io + git + https://github.com/volosoft/abp/ + + + + + + + \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo.Abp.PermissionManagement.Application.Contracts.csproj b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo.Abp.PermissionManagement.Application.Contracts.csproj new file mode 100644 index 0000000000..aa882a1351 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo.Abp.PermissionManagement.Application.Contracts.csproj @@ -0,0 +1,23 @@ + + + + + + netstandard2.0 + Volo.Abp.PermissionManagement.Application.Contracts + Volo.Abp.PermissionManagement.Application.Contracts + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + + + + diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/AbpPermissionManagementApplicationContractsModule.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/AbpPermissionManagementApplicationContractsModule.cs new file mode 100644 index 0000000000..e3f9289b7f --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/AbpPermissionManagementApplicationContractsModule.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Application; +using Volo.Abp.Modularity; + +namespace Volo.Abp.PermissionManagement +{ + [DependsOn(typeof(AbpDddApplicationModule))] + [DependsOn(typeof(AbpPermissionManagementDomainSharedModule))] + public class AbpPermissionManagementApplicationContractsModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/GetPermissionListResultDto.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/GetPermissionListResultDto.cs new file mode 100644 index 0000000000..02a56e5c09 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/GetPermissionListResultDto.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace Volo.Abp.PermissionManagement +{ + public class GetPermissionListResultDto + { + public string EntityDisplayName { get; set; } + + public List Groups { get; set; } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/PermissionGrantInfoDto.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/PermissionGrantInfoDto.cs new file mode 100644 index 0000000000..de99509fd6 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/PermissionGrantInfoDto.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionGrantInfoDto + { + public string Name { get; set; } + + public string DisplayName { get; set; } + + public string ParentName { get; set; } + + public bool IsGranted { get; set; } + + public List Providers { get; set; } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/PermissionGroupDto.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/PermissionGroupDto.cs new file mode 100644 index 0000000000..45b64b73f6 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/PermissionGroupDto.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionGroupDto + { + public string Name { get; set; } + + public string DisplayName { get; set; } + + public List Permissions { get; set; } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/ProviderInfoDto.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/ProviderInfoDto.cs new file mode 100644 index 0000000000..ad816cd77f --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/ProviderInfoDto.cs @@ -0,0 +1,9 @@ +namespace Volo.Abp.PermissionManagement +{ + public class ProviderInfoDto + { + public string ProviderName { get; set; } + + public string ProviderKey { get; set; } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/UpdatePermissionDto.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/UpdatePermissionDto.cs new file mode 100644 index 0000000000..9c61cc33a4 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/UpdatePermissionDto.cs @@ -0,0 +1,9 @@ +namespace Volo.Abp.PermissionManagement +{ + public class UpdatePermissionDto + { + public string Name { get; set; } + + public bool IsGranted { get; set; } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/UpdatePermissionsDto.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/UpdatePermissionsDto.cs new file mode 100644 index 0000000000..f16626567d --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application.Contracts/Volo/Abp/PermissionManagement/UpdatePermissionsDto.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.PermissionManagement +{ + public class UpdatePermissionsDto + { + public UpdatePermissionDto[] Permissions { get; set; } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo.Abp.PermissionManagement.Application.csproj b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo.Abp.PermissionManagement.Application.csproj new file mode 100644 index 0000000000..892a20e55f --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo.Abp.PermissionManagement.Application.csproj @@ -0,0 +1,21 @@ + + + + + + netstandard2.0 + Volo.Abp.PermissionManagement.Application + Volo.Abp.PermissionManagement.Application + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + + diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo/Abp/PermissionManagement/AbpPermissionManagementApplicationModule.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo/Abp/PermissionManagement/AbpPermissionManagementApplicationModule.cs new file mode 100644 index 0000000000..7efc7ecc26 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo/Abp/PermissionManagement/AbpPermissionManagementApplicationModule.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; + +namespace Volo.Abp.PermissionManagement +{ + [DependsOn(typeof(AbpPermissionManagementDomainModule))] + [DependsOn(typeof(AbpPermissionManagementApplicationContractsModule))] + public class AbpPermissionManagementApplicationModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo/Abp/PermissionManagement/IPermissionAppServiceHelper.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo/Abp/PermissionManagement/IPermissionAppServiceHelper.cs new file mode 100644 index 0000000000..03751b801a --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo/Abp/PermissionManagement/IPermissionAppServiceHelper.cs @@ -0,0 +1,12 @@ +using System.Threading.Tasks; +using JetBrains.Annotations; + +namespace Volo.Abp.PermissionManagement +{ + public interface IPermissionAppServiceHelper + { + Task GetAsync([NotNull] string providerName, [NotNull] string providerKey); + + Task UpdateAsync([NotNull] string providerName, [NotNull] string providerKey, UpdatePermissionsDto input); + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo/Abp/PermissionManagement/PermissionAppServiceHelper.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo/Abp/PermissionManagement/PermissionAppServiceHelper.cs new file mode 100644 index 0000000000..9a2dfc3b24 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Application/Volo/Abp/PermissionManagement/PermissionAppServiceHelper.cs @@ -0,0 +1,82 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Extensions.Localization; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionAppServiceHelper : IPermissionAppServiceHelper, ITransientDependency + { + private readonly IPermissionManager _permissionManager; + private readonly IPermissionDefinitionManager _permissionDefinitionManager; + private readonly IStringLocalizerFactory _stringLocalizerFactory; + + public PermissionAppServiceHelper( + IPermissionManager permissionManager, + IPermissionDefinitionManager permissionDefinitionManager, + IStringLocalizerFactory stringLocalizerFactory) + { + _permissionManager = permissionManager; + _permissionDefinitionManager = permissionDefinitionManager; + _stringLocalizerFactory = stringLocalizerFactory; + } + + public async Task GetAsync(string providerName, string providerKey) + { + var result = new GetPermissionListResultDto + { + EntityDisplayName = providerKey, + Groups = new List() + }; + + foreach (var group in _permissionDefinitionManager.GetGroups()) + { + var groupDto = new PermissionGroupDto + { + Name = group.Name, + DisplayName = group.DisplayName.Localize(_stringLocalizerFactory), + Permissions = new List() + }; + + foreach (var permission in group.GetPermissionsWithChildren()) + { + var grantInfoDto = new PermissionGrantInfoDto + { + Name = permission.Name, + DisplayName = permission.DisplayName.Localize(_stringLocalizerFactory), + ParentName = permission.Parent?.Name, + Providers = new List() + }; + + var grantInfo = await _permissionManager.GetAsync(permission.Name, providerName, providerKey); + + grantInfoDto.IsGranted = grantInfo.IsGranted; + + foreach (var provider in grantInfo.Providers) + { + grantInfoDto.Providers.Add(new ProviderInfoDto + { + ProviderName = provider.Name, + ProviderKey = provider.Key, + }); + } + + groupDto.Permissions.Add(grantInfoDto); + } + + result.Groups.Add(groupDto); + } + + return result; + } + + 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/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain.Shared/Volo.Abp.PermissionManagement.Domain.Shared.csproj b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain.Shared/Volo.Abp.PermissionManagement.Domain.Shared.csproj new file mode 100644 index 0000000000..add8513cec --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain.Shared/Volo.Abp.PermissionManagement.Domain.Shared.csproj @@ -0,0 +1,20 @@ + + + + + + netstandard2.0 + Volo.Abp.PermissionManagement.Domain.Shared + Volo.Abp.PermissionManagement.Domain.Shared + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain.Shared/Volo/Abp/PermissionManagement/AbpPermissionManagementDomainSharedModule.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain.Shared/Volo/Abp/PermissionManagement/AbpPermissionManagementDomainSharedModule.cs new file mode 100644 index 0000000000..a9666c0e26 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain.Shared/Volo/Abp/PermissionManagement/AbpPermissionManagementDomainSharedModule.cs @@ -0,0 +1,13 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; + +namespace Volo.Abp.PermissionManagement +{ + public class AbpPermissionManagementDomainSharedModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain.Shared/Volo/Abp/PermissionManagement/PermissionGrantConsts.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain.Shared/Volo/Abp/PermissionManagement/PermissionGrantConsts.cs new file mode 100644 index 0000000000..8d579638ec --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain.Shared/Volo/Abp/PermissionManagement/PermissionGrantConsts.cs @@ -0,0 +1,11 @@ +namespace Volo.Abp.PermissionManagement +{ + public static class PermissionGrantConsts + { + public const int MaxNameLength = 128; + + public const int MaxProviderNameLength = 64; + + public const int MaxProviderKeyLength = 64; + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo.Abp.PermissionManagement.Domain.csproj b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo.Abp.PermissionManagement.Domain.csproj new file mode 100644 index 0000000000..a0bfd28266 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo.Abp.PermissionManagement.Domain.csproj @@ -0,0 +1,25 @@ + + + + + + netstandard2.0 + Volo.Abp.PermissionManagement.Domain + Volo.Abp.PermissionManagement.Domain + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + + + + + + diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/AbpPermissionManagementConsts.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/AbpPermissionManagementConsts.cs new file mode 100644 index 0000000000..4c0966a8af --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/AbpPermissionManagementConsts.cs @@ -0,0 +1,9 @@ +namespace Volo.Abp.PermissionManagement +{ + public static class AbpPermissionManagementConsts + { + public const string DefaultDbTablePrefix = "Abp"; + + public const string DefaultDbSchema = null; + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/AbpPermissionManagementDomainModule.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/AbpPermissionManagementDomainModule.cs new file mode 100644 index 0000000000..714c019e7f --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/AbpPermissionManagementDomainModule.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Authorization; +using Volo.Abp.Caching; +using Volo.Abp.Domain; +using Volo.Abp.Json; +using Volo.Abp.Modularity; + +namespace Volo.Abp.PermissionManagement +{ + [DependsOn(typeof(AbpAuthorizationModule))] + [DependsOn(typeof(AbpDddDomainModule))] + [DependsOn(typeof(AbpPermissionManagementDomainSharedModule))] + [DependsOn(typeof(AbpCachingModule))] + [DependsOn(typeof(AbpJsonModule))] + public class AbpPermissionManagementDomainModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/IPermissionGrantRepository.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/IPermissionGrantRepository.cs new file mode 100644 index 0000000000..886d546073 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/IPermissionGrantRepository.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories; + +namespace Volo.Abp.PermissionManagement +{ + public interface IPermissionGrantRepository : IBasicRepository + { + Task FindAsync(string name, string providerName, string providerKey); + + Task> GetListAsync(string providerName, string providerKey); + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/IPermissionManagementProvider.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/IPermissionManagementProvider.cs new file mode 100644 index 0000000000..a749ce5abe --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/IPermissionManagementProvider.cs @@ -0,0 +1,16 @@ +using System.Threading.Tasks; +using JetBrains.Annotations; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.PermissionManagement +{ + public interface IPermissionManagementProvider : ISingletonDependency + { + string Name { get; } + + Task CheckAsync([NotNull] string name, [NotNull] string providerName, [NotNull] string providerKey); + + Task SetAsync([NotNull] string name, [NotNull] string providerKey, bool isGranted); + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/IPermissionManager.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/IPermissionManager.cs new file mode 100644 index 0000000000..888f06235e --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/IPermissionManager.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using JetBrains.Annotations; + +namespace Volo.Abp.PermissionManagement +{ + //TODO: Write extension methods for simple IsGranted check + + public interface IPermissionManager + { + Task GetAsync(string permissionName, string providerName, string providerKey); + + Task> GetAllAsync([NotNull] string providerName, [NotNull] string providerKey); + + Task SetAsync(string permissionName, string providerName, string providerKey, bool isGranted); + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrant.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrant.cs new file mode 100644 index 0000000000..cd8d7d3395 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrant.cs @@ -0,0 +1,42 @@ +using System; +using JetBrains.Annotations; +using Volo.Abp.Domain.Entities; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionGrant : Entity, IMultiTenant + { + public virtual Guid? TenantId { get; protected set; } + + [NotNull] + public virtual string Name { get; protected set; } + + [NotNull] + public virtual string ProviderName { get; protected set; } + + [NotNull] + public virtual string ProviderKey { get; protected set; } + + protected PermissionGrant() + { + + } + + public PermissionGrant( + Guid id, + [NotNull] string name, + [NotNull] string providerName , + [NotNull] string providerKey, + Guid? tenantId = null) + { + Check.NotNull(name, nameof(name)); + + Id = id; + Name = name; + ProviderName = providerName; + ProviderKey = providerKey; + TenantId = tenantId; + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrantCacheItem.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrantCacheItem.cs new file mode 100644 index 0000000000..285bc117bc --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrantCacheItem.cs @@ -0,0 +1,28 @@ +using System; + +namespace Volo.Abp.PermissionManagement +{ + [Serializable] + public class PermissionGrantCacheItem + { + public string Name { get; set; } + + public bool IsGranted { get; set; } + + public PermissionGrantCacheItem() + { + + } + + public PermissionGrantCacheItem(string name, bool isGranted) + { + Name = name; + IsGranted = isGranted; + } + + public static string CalculateCacheKey(string name, string providerName, string providerKey) + { + return "pn:" + providerName + ",pk:" + providerKey + ",n:" + name; + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrantCacheItemInvalidator.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrantCacheItemInvalidator.cs new file mode 100644 index 0000000000..ea12b461fd --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrantCacheItemInvalidator.cs @@ -0,0 +1,41 @@ +using System.Threading.Tasks; +using Volo.Abp.Caching; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Entities.Events; +using Volo.Abp.EventBus; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionGrantCacheItemInvalidator : IEventHandler>, ITransientDependency + { + protected ICurrentTenant CurrentTenant { get; } + + protected IDistributedCache Cache { get; } + + public PermissionGrantCacheItemInvalidator(IDistributedCache cache, ICurrentTenant currentTenant) + { + Cache = cache; + CurrentTenant = currentTenant; + } + + public virtual async Task HandleEventAsync(EntityChangedEventData eventData) + { + var cacheKey = CalculateCacheKey( + eventData.Entity.Name, + eventData.Entity.ProviderName, + eventData.Entity.ProviderKey + ); + + using (CurrentTenant.Change(eventData.Entity.TenantId)) + { + await Cache.RemoveAsync(cacheKey); + } + } + + protected virtual string CalculateCacheKey(string name, string providerName, string providerKey) + { + return PermissionGrantCacheItem.CalculateCacheKey(name, providerName, providerKey); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionManagementOptions.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionManagementOptions.cs new file mode 100644 index 0000000000..07bf361acc --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionManagementOptions.cs @@ -0,0 +1,14 @@ +using Volo.Abp.Collections; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionManagementOptions + { + public ITypeList ManagementProviders { get; } + + public PermissionManagementOptions() + { + ManagementProviders = new TypeList(); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionManagementProvider.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionManagementProvider.cs new file mode 100644 index 0000000000..01251029ee --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionManagementProvider.cs @@ -0,0 +1,79 @@ +using System.Threading.Tasks; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Guids; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.PermissionManagement +{ + public abstract class PermissionManagementProvider : IPermissionManagementProvider + { + public abstract string Name { get; } + + protected IPermissionGrantRepository PermissionGrantRepository { get; } + + protected IGuidGenerator GuidGenerator { get; } + + protected ICurrentTenant CurrentTenant { get; } + + protected PermissionManagementProvider( + IPermissionGrantRepository permissionGrantRepository, + IGuidGenerator guidGenerator, + ICurrentTenant currentTenant) + { + PermissionGrantRepository = permissionGrantRepository; + GuidGenerator = guidGenerator; + CurrentTenant = currentTenant; + } + + + public virtual async Task CheckAsync(string name, string providerName, string providerKey) + { + if (providerName != Name) + { + return PermissionValueProviderGrantInfo.NonGranted; + } + + return new PermissionValueProviderGrantInfo( + await PermissionGrantRepository.FindAsync(name, providerName, providerKey) != null, + providerKey + ); + } + + public virtual Task SetAsync(string name, string providerKey, bool isGranted) + { + return isGranted + ? GrantAsync(name, providerKey) + : RevokeAsync(name, providerKey); + } + + protected virtual async Task GrantAsync(string name, string providerKey) + { + var permissionGrant = await PermissionGrantRepository.FindAsync(name, Name, providerKey); + if (permissionGrant != null) + { + return; + } + + await PermissionGrantRepository.InsertAsync( + new PermissionGrant( + GuidGenerator.Create(), + name, + Name, + providerKey, + CurrentTenant.Id + ) + ); + } + + protected virtual async Task RevokeAsync(string name, string providerKey) + { + var permissionGrant = await PermissionGrantRepository.FindAsync(name, Name, providerKey); + if (permissionGrant == null) + { + return; + } + + await PermissionGrantRepository.DeleteAsync(permissionGrant); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionManager.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionManager.cs new file mode 100644 index 0000000000..18dff9f825 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionManager.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Guids; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionManager : IPermissionManager, ISingletonDependency + { + protected IPermissionGrantRepository PermissionGrantRepository { get; } + + protected IPermissionDefinitionManager PermissionDefinitionManager { get; } + + protected IGuidGenerator GuidGenerator { get; } + + protected IReadOnlyList ManagementProviders => _lazyProviders.Value; + + protected PermissionManagementOptions Options { get; } + + private readonly Lazy> _lazyProviders; + + public PermissionManager( + IPermissionDefinitionManager permissionDefinitionManager, + IPermissionGrantRepository permissionGrantRepository, + IServiceProvider serviceProvider, + IGuidGenerator guidGenerator, + IOptions options) + { + GuidGenerator = guidGenerator; + PermissionGrantRepository = permissionGrantRepository; + PermissionDefinitionManager = permissionDefinitionManager; + Options = options.Value; + + _lazyProviders = new Lazy>( + () => Options + .ManagementProviders + .Select(c => serviceProvider.GetRequiredService(c) as IPermissionManagementProvider) + .ToList(), + true + ); + } + + public async Task GetAsync(string permissionName, string providerName, string providerKey) + { + return await GetInternalAsync(PermissionDefinitionManager.Get(permissionName), providerName, providerKey); + } + + public async Task> GetAllAsync(string providerName, string providerKey) + { + var results = new List(); + + foreach (var permissionDefinition in PermissionDefinitionManager.GetPermissions()) + { + results.Add(await GetInternalAsync(permissionDefinition, providerName, providerKey)); + } + + return results; + } + + public async Task SetAsync(string permissionName, string providerName, string providerKey, bool isGranted) + { + var currentGrantInfo = await GetAsync(permissionName, providerName, providerKey); + if (currentGrantInfo.IsGranted == isGranted) + { + return; + } + + var provider = ManagementProviders.FirstOrDefault(m => m.Name == providerName); + if (provider == null) + { + throw new AbpException("Unknown permission management provider: " + providerName); + } + + await provider.SetAsync(permissionName, providerKey, isGranted); + } + + protected virtual async Task GetInternalAsync(PermissionDefinition permissionDefinition, string providerName, string providerKey) + { + var result = new PermissionWithGrantedProviders(permissionDefinition.Name, false); + + foreach (var provider in ManagementProviders) + { + var providerResult = await provider.CheckAsync(permissionDefinition.Name, providerName, providerKey); + if (providerResult.IsGranted) + { + result.IsGranted = true; + result.Providers.Add(new PermissionValueProviderInfo(provider.Name, providerResult.ProviderKey)); + } + } + + return result; + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionStore.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionStore.cs new file mode 100644 index 0000000000..c312a094e7 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionStore.cs @@ -0,0 +1,55 @@ +using System.Threading.Tasks; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Caching; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionStore : IPermissionStore, ITransientDependency + { + protected IPermissionGrantRepository PermissionGrantRepository { get; } + + protected IDistributedCache Cache { get; } + + public PermissionStore( + IPermissionGrantRepository permissionGrantRepository, + IDistributedCache cache) + { + PermissionGrantRepository = permissionGrantRepository; + Cache = cache; + } + + public async Task IsGrantedAsync(string name, string providerName, string providerKey) + { + return (await GetCacheItemAsync(name, providerName, providerKey)).IsGranted; + } + + protected virtual async Task GetCacheItemAsync(string name, string providerName, string providerKey) + { + var cacheKey = CalculateCacheKey(name, providerName, providerKey); + var cacheItem = await Cache.GetAsync(cacheKey); + + if (cacheItem != null) + { + return cacheItem; + } + + cacheItem = new PermissionGrantCacheItem( + name, + await PermissionGrantRepository.FindAsync(name, providerName, providerKey) != null + ); + + await Cache.SetAsync( + cacheKey, + cacheItem + ); + + return cacheItem; + } + + protected virtual string CalculateCacheKey(string name, string providerName, string providerKey) + { + return PermissionGrantCacheItem.CalculateCacheKey(name, providerName, providerKey); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionValueProviderInfo.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionValueProviderInfo.cs new file mode 100644 index 0000000000..18228ee79a --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionValueProviderInfo.cs @@ -0,0 +1,20 @@ +using JetBrains.Annotations; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionValueProviderInfo + { + public string Name { get; } + + public string Key { get; } + + public PermissionValueProviderInfo([NotNull]string name, [NotNull]string key) + { + Check.NotNull(name, nameof(name)); + Check.NotNull(key, nameof(key)); + + Name = name; + Key = key; + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionWithGrantedProviders.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionWithGrantedProviders.cs new file mode 100644 index 0000000000..6e4e2f46e3 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionWithGrantedProviders.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using JetBrains.Annotations; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionWithGrantedProviders + { + public string Name { get; } + + public bool IsGranted { get; set; } + + public List Providers { get; set; } + + public PermissionWithGrantedProviders([NotNull] string name, bool isGranted) + { + Check.NotNull(name, nameof(name)); + + Name = name; + IsGranted = isGranted; + + Providers = new List(); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo.Abp.PermissionManagement.EntityFrameworkCore.csproj b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo.Abp.PermissionManagement.EntityFrameworkCore.csproj new file mode 100644 index 0000000000..4882a40f1a --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo.Abp.PermissionManagement.EntityFrameworkCore.csproj @@ -0,0 +1,22 @@ + + + + + + netstandard2.0 + Volo.Abp.PermissionManagement.EntityFrameworkCore + Volo.Abp.PermissionManagement.EntityFrameworkCore + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + + + diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/AbpPermissionManagementDbContextModelBuilderExtensions.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/AbpPermissionManagementDbContextModelBuilderExtensions.cs new file mode 100644 index 0000000000..ceb14be6d5 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/AbpPermissionManagementDbContextModelBuilderExtensions.cs @@ -0,0 +1,32 @@ +using JetBrains.Annotations; +using Microsoft.EntityFrameworkCore; + +namespace Volo.Abp.PermissionManagement.EntityFrameworkCore +{ + public static class AbpPermissionManagementDbContextModelBuilderExtensions + { + public static void ConfigurePermissionManagement( + [NotNull] this ModelBuilder builder, + [CanBeNull] string tablePrefix = AbpPermissionManagementConsts.DefaultDbTablePrefix, + [CanBeNull] string schema = AbpPermissionManagementConsts.DefaultDbSchema) + { + Check.NotNull(builder, nameof(builder)); + + if (tablePrefix == null) + { + tablePrefix = ""; + } + + builder.Entity(b => + { + b.ToTable(tablePrefix + "PermissionGrants", schema); + + b.Property(x => x.Name).HasMaxLength(PermissionGrantConsts.MaxNameLength).IsRequired(); + b.Property(x => x.ProviderName).HasMaxLength(PermissionGrantConsts.MaxProviderNameLength).IsRequired(); + b.Property(x => x.ProviderKey).HasMaxLength(PermissionGrantConsts.MaxProviderKeyLength).IsRequired(); + + b.HasIndex(x => new {x.Name, x.ProviderName, x.ProviderKey}); + }); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/AbpPermissionManagementEntityFrameworkCoreModule.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/AbpPermissionManagementEntityFrameworkCoreModule.cs new file mode 100644 index 0000000000..cd9b296ae0 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/AbpPermissionManagementEntityFrameworkCoreModule.cs @@ -0,0 +1,23 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Modularity; + +namespace Volo.Abp.PermissionManagement.EntityFrameworkCore +{ + [DependsOn(typeof(AbpPermissionManagementDomainModule))] + [DependsOn(typeof(AbpEntityFrameworkCoreModule))] + public class AbpPermissionManagementEntityFrameworkCoreModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAbpDbContext(options => + { + options.AddDefaultRepositories(); + + options.AddRepository(); + }); + + services.AddAssemblyOf(); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/EfCorePermissionGrantRepository.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/EfCorePermissionGrantRepository.cs new file mode 100644 index 0000000000..413a960c7c --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/EfCorePermissionGrantRepository.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Volo.Abp.Domain.Repositories.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; + +namespace Volo.Abp.PermissionManagement.EntityFrameworkCore +{ + public class EfCorePermissionGrantRepository : EfCoreRepository, IPermissionGrantRepository + { + public EfCorePermissionGrantRepository(IDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + + } + + public async Task FindAsync(string name, string providerName, string providerKey) + { + return await DbSet + .FirstOrDefaultAsync(s => + s.Name == name && + s.ProviderName == providerName && + s.ProviderKey == providerKey + ); + } + + public async Task> GetListAsync(string providerName, string providerKey) + { + return await DbSet + .Where(s => + s.ProviderName == providerName && + s.ProviderKey == providerKey + ).ToListAsync(); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/IPermissionManagementDbContext.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/IPermissionManagementDbContext.cs new file mode 100644 index 0000000000..0dffabcba7 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/IPermissionManagementDbContext.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; +using Volo.Abp.Data; +using Volo.Abp.EntityFrameworkCore; + +namespace Volo.Abp.PermissionManagement.EntityFrameworkCore +{ + [ConnectionStringName("AbpPermissionManagement")] + public interface IPermissionManagementDbContext : IEfCoreDbContext + { + DbSet PermissionGrants { get; set; } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/PermissionManagementDbContext.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/PermissionManagementDbContext.cs new file mode 100644 index 0000000000..ab4f73ceb2 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/PermissionManagementDbContext.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using Volo.Abp.Data; +using Volo.Abp.EntityFrameworkCore; + +namespace Volo.Abp.PermissionManagement.EntityFrameworkCore +{ + [ConnectionStringName("AbpPermissionManagement")] + public class PermissionManagementDbContext : AbpDbContext, IPermissionManagementDbContext + { + public static string TablePrefix { get; set; } = AbpPermissionManagementConsts.DefaultDbTablePrefix; + + public static string Schema { get; set; } = AbpPermissionManagementConsts.DefaultDbSchema; + + public DbSet PermissionGrants { get; set; } + + public PermissionManagementDbContext(DbContextOptions options) + : base(options) + { + + } + + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); + + builder.ConfigurePermissionManagement(TablePrefix, Schema); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo.Abp.PermissionManagement.MongoDB.csproj b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo.Abp.PermissionManagement.MongoDB.csproj new file mode 100644 index 0000000000..19bad4d630 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo.Abp.PermissionManagement.MongoDB.csproj @@ -0,0 +1,22 @@ + + + + + + netstandard2.0 + Volo.Abp.PermissionManagement.MongoDB + Volo.Abp.PermissionManagement.MongoDB + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + + + diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementBsonClassMap.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementBsonClassMap.cs new file mode 100644 index 0000000000..707aa66365 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementBsonClassMap.cs @@ -0,0 +1,21 @@ +using MongoDB.Bson.Serialization; +using Volo.Abp.Threading; + +namespace Volo.Abp.PermissionManagement.MongoDB +{ + public static class AbpPermissionManagementBsonClassMap + { + private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); + + public static void Configure() + { + OneTimeRunner.Run(() => + { + BsonClassMap.RegisterClassMap(map => + { + map.AutoMap(); + }); + }); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementMongoDbContextExtensions.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementMongoDbContextExtensions.cs new file mode 100644 index 0000000000..a00f0cbfc3 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementMongoDbContextExtensions.cs @@ -0,0 +1,24 @@ +using System; +using Volo.Abp.MongoDB; + +namespace Volo.Abp.PermissionManagement.MongoDB +{ + public static class AbpPermissionManagementMongoDbContextExtensions + { + public static void ConfigurePermissionManagement( + this IMongoModelBuilder builder, + Action optionsAction = null) + { + Check.NotNull(builder, nameof(builder)); + + var options = new PermissionManagementMongoModelBuilderConfigurationOptions(); + + optionsAction?.Invoke(options); + + builder.Entity(b => + { + b.CollectionName = options.CollectionPrefix + "PermissionGrants"; + }); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementMongoDbModule.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementMongoDbModule.cs new file mode 100644 index 0000000000..10e1b9c78f --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementMongoDbModule.cs @@ -0,0 +1,28 @@ +using Microsoft.Extensions.DependencyInjection; +using MongoDB.Driver; +using Volo.Abp.Modularity; +using Volo.Abp.MongoDB; + +namespace Volo.Abp.PermissionManagement.MongoDB +{ + [DependsOn( + typeof(AbpPermissionManagementDomainModule), + typeof(AbpMongoDbModule) + )] + public class AbpPermissionManagementMongoDbModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + AbpPermissionManagementBsonClassMap.Configure(); + + services.AddMongoDbContext(options => + { + options.AddDefaultRepositories(); + + options.AddRepository(); + }); + + services.AddAssemblyOf(); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/IPermissionManagementMongoDbContext.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/IPermissionManagementMongoDbContext.cs new file mode 100644 index 0000000000..35977a8570 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/IPermissionManagementMongoDbContext.cs @@ -0,0 +1,12 @@ +using MongoDB.Driver; +using Volo.Abp.Data; +using Volo.Abp.MongoDB; + +namespace Volo.Abp.PermissionManagement.MongoDB +{ + [ConnectionStringName("AbpPermissionManagement")] + public interface IPermissionManagementMongoDbContext : IAbpMongoDbContext + { + IMongoCollection PermissionGrants { get; } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/MongoPermissionGrantRepository.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/MongoPermissionGrantRepository.cs new file mode 100644 index 0000000000..70614c890d --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/MongoPermissionGrantRepository.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.MongoDB; + +namespace Volo.Abp.PermissionManagement.MongoDB +{ + public class MongoPermissionGrantRepository : MongoDbRepository, IPermissionGrantRepository + { + public MongoPermissionGrantRepository(IMongoDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + + } + + public async Task FindAsync(string name, string providerName, string providerKey) + { + return await GetMongoQueryable() + .FirstOrDefaultAsync(s => + s.Name == name && + s.ProviderName == providerName && + s.ProviderKey == providerKey + ); + } + + public async Task> GetListAsync(string providerName, string providerKey) + { + return await GetMongoQueryable() + .Where(s => + s.ProviderName == providerName && + s.ProviderKey == providerKey + ).ToListAsync(); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/PermissionManagementMongoDbContext.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/PermissionManagementMongoDbContext.cs new file mode 100644 index 0000000000..4e8c449ac6 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/PermissionManagementMongoDbContext.cs @@ -0,0 +1,24 @@ +using MongoDB.Driver; +using Volo.Abp.Data; +using Volo.Abp.MongoDB; + +namespace Volo.Abp.PermissionManagement.MongoDB +{ + [ConnectionStringName("AbpPermissionManagement")] + public class PermissionManagementMongoDbContext : AbpMongoDbContext, IPermissionManagementMongoDbContext + { + public static string CollectionPrefix { get; set; } = AbpPermissionManagementConsts.DefaultDbTablePrefix; + + public IMongoCollection PermissionGrants => Collection(); + + protected override void CreateModel(IMongoModelBuilder modelBuilder) + { + base.CreateModel(modelBuilder); + + modelBuilder.ConfigurePermissionManagement(options => + { + options.CollectionPrefix = CollectionPrefix; + }); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/PermissionManagementMongoModelBuilderConfigurationOptions.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/PermissionManagementMongoModelBuilderConfigurationOptions.cs new file mode 100644 index 0000000000..930c811c0c --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.MongoDB/Volo/Abp/PermissionManagement/MongoDb/PermissionManagementMongoModelBuilderConfigurationOptions.cs @@ -0,0 +1,13 @@ +using JetBrains.Annotations; +using Volo.Abp.MongoDB; + +namespace Volo.Abp.PermissionManagement.MongoDB +{ + public class PermissionManagementMongoModelBuilderConfigurationOptions : MongoModelBuilderConfigurationOptions + { + public PermissionManagementMongoModelBuilderConfigurationOptions([NotNull] string tablePrefix = AbpPermissionManagementConsts.DefaultDbTablePrefix) + : base(tablePrefix) + { + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/AbpPermissionManagementWebAutoMapperProfile.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/AbpPermissionManagementWebAutoMapperProfile.cs new file mode 100644 index 0000000000..631779b8ae --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/AbpPermissionManagementWebAutoMapperProfile.cs @@ -0,0 +1,18 @@ +using AutoMapper; +using Volo.Abp.PermissionManagement.Web.Pages.AbpPermissionManagement; + +namespace Volo.Abp.PermissionManagement.Web +{ + public class AbpPermissionManagementWebAutoMapperProfile : Profile + { + public AbpPermissionManagementWebAutoMapperProfile() + { + CreateMap(); + + CreateMap() + .ForMember(p => p.Depth, opts => opts.Ignore()); + + CreateMap(); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/AbpPermissionManagementWebModule.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/AbpPermissionManagementWebModule.cs new file mode 100644 index 0000000000..075b4574d7 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/AbpPermissionManagementWebModule.cs @@ -0,0 +1,47 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.AspNetCore.Mvc.Localization; +using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap; +using Volo.Abp.AutoMapper; +using Volo.Abp.Localization; +using Volo.Abp.Modularity; +using Volo.Abp.PermissionManagement.Web.Localization.Resources.AbpPermissionManagement; +using Volo.Abp.VirtualFileSystem; + +namespace Volo.Abp.PermissionManagement.Web +{ + [DependsOn(typeof(AbpPermissionManagementApplicationContractsModule))] + [DependsOn(typeof(AbpAspNetCoreMvcUiBootstrapModule))] + [DependsOn(typeof(AbpAutoMapperModule))] + public class AbpPermissionManagementWebModule : AbpModule + { + public override void PreConfigureServices(IServiceCollection services) + { + services.PreConfigure(options => + { + options.AddAssemblyResource(typeof(AbpPermissionManagementResource)); + }); + } + + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + + services.Configure(options => + { + options.FileSets.AddEmbedded("Volo.Abp.PermissionManagement.Web"); + }); + + services.Configure(options => + { + options.Resources + .Add("en") + .AddVirtualJson("/Localization/Resources/AbpPermissionManagement"); + }); + + services.Configure(options => + { + options.AddProfile(validate: true); + }); + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Localization/Resources/AbpPermissionManagement/AbpPermissionManagementResource.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Localization/Resources/AbpPermissionManagement/AbpPermissionManagementResource.cs new file mode 100644 index 0000000000..648423ce5a --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Localization/Resources/AbpPermissionManagement/AbpPermissionManagementResource.cs @@ -0,0 +1,15 @@ +using Localization.Resources.AbpUi; +using Volo.Abp.Localization; +using Volo.Abp.Localization.Resources.AbpValidation; + +namespace Volo.Abp.PermissionManagement.Web.Localization.Resources.AbpPermissionManagement +{ + [InheritResource( + typeof(AbpValidationResource), + typeof(AbpUiResource))] + [LocalizationResourceName("AbpPermissionManagement")] + public class AbpPermissionManagementResource + { + + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Localization/Resources/AbpPermissionManagement/en.json b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Localization/Resources/AbpPermissionManagement/en.json new file mode 100644 index 0000000000..0886572861 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Localization/Resources/AbpPermissionManagement/en.json @@ -0,0 +1,6 @@ +{ + "culture": "en", + "texts": { + "Permissions": "Permissions" + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Localization/Resources/AbpPermissionManagement/tr.json b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Localization/Resources/AbpPermissionManagement/tr.json new file mode 100644 index 0000000000..061b93fd86 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Localization/Resources/AbpPermissionManagement/tr.json @@ -0,0 +1,6 @@ +{ + "culture": "tr", + "texts": { + "Permissions": "İzinler" + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/IPermissionAppServiceHelper.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/IPermissionAppServiceHelper.cs new file mode 100644 index 0000000000..1dec0a5ca2 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/IPermissionAppServiceHelper.cs @@ -0,0 +1,12 @@ +using System.Threading.Tasks; +using JetBrains.Annotations; + +namespace Volo.Abp.PermissionManagement.Web.Pages.AbpPermissionManagement +{ + public interface IPermissionAppServiceGateway + { + Task GetAsync([NotNull] string providerName, [NotNull] string providerKey); + + Task UpdateAsync([NotNull] string providerName, [NotNull] string providerKey, UpdatePermissionsDto input); + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/NotImplementedPermissionAppServiceGateway.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/NotImplementedPermissionAppServiceGateway.cs new file mode 100644 index 0000000000..bee3c00d8b --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/NotImplementedPermissionAppServiceGateway.cs @@ -0,0 +1,18 @@ +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.PermissionManagement.Web.Pages.AbpPermissionManagement +{ + public class NotImplementedPermissionAppServiceGateway : IPermissionAppServiceGateway, ISingletonDependency + { + public Task GetAsync(string providerName, string providerKey) + { + throw new System.NotImplementedException(); + } + + public Task UpdateAsync(string providerName, string providerKey, UpdatePermissionsDto input) + { + throw new System.NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/PermissionManagementModal.cshtml b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/PermissionManagementModal.cshtml new file mode 100644 index 0000000000..1648856f9a --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/PermissionManagementModal.cshtml @@ -0,0 +1,58 @@ +@page +@using Microsoft.AspNetCore.Mvc.Localization +@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Modal +@using Volo.Abp.PermissionManagement.Web.Localization.Resources.AbpPermissionManagement +@using Volo.Abp.PermissionManagement.Web.Pages.AbpPermissionManagement +@model PermissionManagementModal +@inject IHtmlLocalizer L +@{ + Layout = null; +} + + + + + + + + + + + + + + + @for (var i = 0; i < Model.Groups.Count; i++) + { + @Model.Groups[i].DisplayName + } + + + + + @for (var i = 0; i < Model.Groups.Count; i++) + { + + @for (var j = 0; j < Model.Groups[i].Permissions.Count; j++) + { + + + } + + } + + + + + + + + + + + diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/PermissionManagementModal.cshtml.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/PermissionManagementModal.cshtml.cs new file mode 100644 index 0000000000..b1b3148aa3 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/PermissionManagementModal.cshtml.cs @@ -0,0 +1,139 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; +using Volo.Abp.PermissionManagement.Web.Utils; + +namespace Volo.Abp.PermissionManagement.Web.Pages.AbpPermissionManagement +{ + public class PermissionManagementModal : AbpPageModel + { + private readonly IPermissionAppServiceGateway _permissionAppServiceGateway; + + [Required] + [HiddenInput] + [BindProperty(SupportsGet = true)] + public string ProviderName { get; set; } + + [Required] + [HiddenInput] + [BindProperty(SupportsGet = true)] + public string ProviderKey { get; set; } + + [BindProperty] + public List Groups { get; set; } + + public string EntityDisplayName { get; set; } + + public PermissionManagementModal(IPermissionAppServiceGateway permissionAppServiceGateway) + { + _permissionAppServiceGateway = permissionAppServiceGateway; + } + + public async Task OnGetAsync() + { + ValidateModel(); + + var result = await _permissionAppServiceGateway.GetAsync(ProviderName, ProviderKey); + + EntityDisplayName = result.EntityDisplayName; + + Groups = ObjectMapper + .Map, List>(result.Groups) + .OrderBy(g => g.DisplayName) + .ToList(); + + foreach (var group in Groups) + { + new FlatTreeDepthFinder().SetDepths(group.Permissions); + } + } + + public async Task OnPostAsync() + { + ValidateModel(); + + var updatePermissionDtos = Groups + .SelectMany(g => g.Permissions) + .Select(p => new UpdatePermissionDto + { + Name = p.Name, + IsGranted = p.IsGranted + }) + .ToArray(); + + await _permissionAppServiceGateway.UpdateAsync( + ProviderName, + ProviderKey, + new UpdatePermissionsDto + { + Permissions = updatePermissionDtos + } + ); + + return NoContent(); + } + + public class PermissionGroupViewModel + { + public string Name { get; set; } + + public string DisplayName { get; set; } + + public List Permissions { get; set; } + + public string GetNormalizedGroupName() + { + return Name.Replace(".", "_"); + } + } + + public class PermissionGrantInfoViewModel : IFlatTreeItem + { + [Required] + [HiddenInput] + public string Name { get; set; } + + public string DisplayName { get; set; } + + public int Depth { get; set; } + + public string ParentName { get; set; } + + public bool IsGranted { get; set; } + + public List Providers { get; set; } + + public bool IsDisabled(string currentProviderName) + { + return IsGranted && Providers.All(p => p.ProviderName != currentProviderName); + } + + public string GetShownName(string currentProviderName) + { + if (!IsDisabled(currentProviderName)) + { + return DisplayName; + } + + return string.Format( + "{0} ({1})", + DisplayName, + Providers + .Where(p => p.ProviderName != currentProviderName) + .Select(p => p.ProviderName) + .JoinAsString(", ") + ); + } + } + + public class ProviderInfoViewModel + { + public string ProviderName { get; set; } + + public string ProviderKey { get; set; } + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/_ViewImports.cshtml b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/_ViewImports.cshtml new file mode 100644 index 0000000000..d1ac64721f --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Pages/AbpPermissionManagement/_ViewImports.cshtml @@ -0,0 +1,2 @@ +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bootstrap \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Properties/launchSettings.json b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Properties/launchSettings.json new file mode 100644 index 0000000000..6cb3381b6c --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:55926/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Volo.Abp.PermissionManagement.Web": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:55927/" + } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Utils/FlatTreeDepthFinder.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Utils/FlatTreeDepthFinder.cs new file mode 100644 index 0000000000..a9e691324c --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Utils/FlatTreeDepthFinder.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; + +namespace Volo.Abp.PermissionManagement.Web.Utils +{ + public class FlatTreeDepthFinder + where T : class, IFlatTreeItem + { + public void SetDepths(List items) + { + SetDepths(items, null, 0); + } + + private static void SetDepths(List items, string currentParent, int currentDepth) + { + foreach (var item in items) + { + if (item.ParentName == currentParent) + { + item.Depth = currentDepth; + SetDepths(items, item.Name, currentDepth + 1); + } + } + } + } +} diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Utils/IFlatTreeItem.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Utils/IFlatTreeItem.cs new file mode 100644 index 0000000000..21590ead24 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Utils/IFlatTreeItem.cs @@ -0,0 +1,11 @@ +namespace Volo.Abp.PermissionManagement.Web.Utils +{ + public interface IFlatTreeItem + { + string Name { get; } + + string ParentName { get; } + + int Depth { get; set; } + } +} \ No newline at end of file diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Volo.Abp.PermissionManagement.Web.csproj b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Volo.Abp.PermissionManagement.Web.csproj new file mode 100644 index 0000000000..b18b0da565 --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/Volo.Abp.PermissionManagement.Web.csproj @@ -0,0 +1,27 @@ + + + + + + netstandard2.0 + Volo.Abp.PermissionManagement.Web + Volo.Abp.PermissionManagement.Web + true + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + Library + + + + + + + + + + + + + + + + diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/wwwroot/pages/abp-permission-management/permission-management-modal.js b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/wwwroot/pages/abp-permission-management/permission-management-modal.js new file mode 100644 index 0000000000..fa4815e16e --- /dev/null +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Web/wwwroot/pages/abp-permission-management/permission-management-modal.js @@ -0,0 +1,54 @@ +var abp = abp || {}; +(function ($) { + abp.modals = abp.modals || {}; + + abp.modals.PermissionManagement = function () { + function checkParents($tab, $checkBox) { + var parentName = $checkBox.closest('.form-check').attr('data-parent-name'); + if (!parentName) { + return; + } + + $tab.find('.form-check') + .filter('[data-permission-name="' + parentName + '"]') + .find('input[type="checkbox"]') + .each(function() { + var $parent = $(this); + $parent.prop('checked', true); + checkParents($tab, $parent); + }); + } + + function uncheckChildren($tab, $checkBox) { + var permissionName = $checkBox.closest('.form-check').attr('data-permission-name'); + if (!permissionName) { + return; + } + + $tab.find('.form-check') + .filter('[data-parent-name="' + permissionName + '"]') + .find('input[type="checkbox"]') + .each(function () { + var $child = $(this); + $child.prop('checked', false); + uncheckChildren($tab, $child); + }); + } + + this.initDom = function($el) { + $el.find('.tab-pane').each(function () { + var $tab = $(this); + $tab.find('input[type="checkbox"]').each(function () { + var $checkBox = $(this); + $checkBox.change(function () { + if ($checkBox.is(':checked')) { + checkParents($tab, $checkBox); + } else { + uncheckChildren($tab, $checkBox); + } + }); + }); + }); + }; + }; +})(jQuery); \ No newline at end of file diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests.csproj b/modules/permission-management/test/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests.csproj new file mode 100644 index 0000000000..afd0a14d89 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests.csproj @@ -0,0 +1,24 @@ + + + + netcoreapp2.0 + Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests + Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests + true + false + false + false + + + + + + + + + + + + + + \ No newline at end of file diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests/Volo/Abp/PermissionManagement/EntityFrameworkCore/AbpPermissionManagementEntityFrameworkCoreTestModule.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests/Volo/Abp/PermissionManagement/EntityFrameworkCore/AbpPermissionManagementEntityFrameworkCoreTestModule.cs new file mode 100644 index 0000000000..5bccbccf6e --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests/Volo/Abp/PermissionManagement/EntityFrameworkCore/AbpPermissionManagementEntityFrameworkCoreTestModule.cs @@ -0,0 +1,37 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Modularity; +using Volo.Abp.Uow; + +namespace Volo.Abp.PermissionManagement.EntityFrameworkCore +{ + [DependsOn( + typeof(AbpPermissionManagementEntityFrameworkCoreModule), + typeof(AbpPermissionManagementTestBaseModule))] + public class AbpPermissionManagementEntityFrameworkCoreTestModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddEntityFrameworkInMemoryDatabase(); + + var databaseName = Guid.NewGuid().ToString(); + + services.Configure(options => + { + options.Configure(context => + { + context.DbContextOptions.UseInMemoryDatabase(databaseName); + }); + }); + + services.Configure(options => + { + options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; //EF in-memory database does not support transactions + }); + + services.AddAssemblyOf(); + } + } +} diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests/Volo/Abp/PermissionManagement/EntityFrameworkCore/PermissionGrantRepository_Tests.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests/Volo/Abp/PermissionManagement/EntityFrameworkCore/PermissionGrantRepository_Tests.cs new file mode 100644 index 0000000000..fefdbe2e80 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.EntityFrameworkCore.Tests/Volo/Abp/PermissionManagement/EntityFrameworkCore/PermissionGrantRepository_Tests.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.PermissionManagement.EntityFrameworkCore +{ + public class PermissionGrantRepository_Tests : PermissionGrantRepository_Tests + { + + } +} diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.MongoDB.Tests/Volo.Abp.PermissionManagement.MongoDB.Tests.csproj b/modules/permission-management/test/Volo.Abp.PermissionManagement.MongoDB.Tests/Volo.Abp.PermissionManagement.MongoDB.Tests.csproj new file mode 100644 index 0000000000..cb155cea88 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.MongoDB.Tests/Volo.Abp.PermissionManagement.MongoDB.Tests.csproj @@ -0,0 +1,24 @@ + + + + netcoreapp2.0 + Volo.Abp.PermissionManagement.MongoDB.Tests + Volo.Abp.PermissionManagement.MongoDB.Tests + true + false + false + false + + + + + + + + + + + + + + \ No newline at end of file diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.MongoDB.Tests/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementMongoDbTestModule.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.MongoDB.Tests/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementMongoDbTestModule.cs new file mode 100644 index 0000000000..fe8b507a97 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.MongoDB.Tests/Volo/Abp/PermissionManagement/MongoDb/AbpPermissionManagementMongoDbTestModule.cs @@ -0,0 +1,33 @@ +using Microsoft.Extensions.DependencyInjection; +using Mongo2Go; +using Volo.Abp.Data; +using Volo.Abp.Modularity; +using Volo.Abp.PermissionManagement.MongoDB; + +namespace Volo.Abp.PermissionManagement.MongoDb +{ + [DependsOn( + typeof(AbpPermissionManagementMongoDbModule), + typeof(AbpPermissionManagementTestBaseModule))] + public class AbpPermissionManagementMongoDbTestModule : AbpModule + { + private MongoDbRunner _mongoDbRunner; + + public override void ConfigureServices(IServiceCollection services) + { + _mongoDbRunner = MongoDbRunner.Start(); + + services.Configure(options => + { + options.ConnectionStrings.Default = _mongoDbRunner.ConnectionString; + }); + + services.AddAssemblyOf(); + } + + public override void OnApplicationShutdown(ApplicationShutdownContext context) + { + _mongoDbRunner.Dispose(); + } + } +} diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.MongoDB.Tests/Volo/Abp/PermissionManagement/MongoDb/PermissionGrantRepository_Tests.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.MongoDB.Tests/Volo/Abp/PermissionManagement/MongoDb/PermissionGrantRepository_Tests.cs new file mode 100644 index 0000000000..53fd2d7e35 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.MongoDB.Tests/Volo/Abp/PermissionManagement/MongoDb/PermissionGrantRepository_Tests.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.PermissionManagement.MongoDb +{ + public class PermissionGrantRepository_Tests : PermissionGrantRepository_Tests + { + + } +} diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo.Abp.PermissionManagement.TestBase.csproj b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo.Abp.PermissionManagement.TestBase.csproj new file mode 100644 index 0000000000..da2626cc3d --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo.Abp.PermissionManagement.TestBase.csproj @@ -0,0 +1,29 @@ + + + + netcoreapp2.0 + Volo.Abp.PermissionManagement.TestBase + Volo.Abp.PermissionManagement.TestBase + true + false + false + false + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/AbpPermissionManagementTestBaseModule.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/AbpPermissionManagementTestBaseModule.cs new file mode 100644 index 0000000000..d0aa4f4307 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/AbpPermissionManagementTestBaseModule.cs @@ -0,0 +1,40 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Autofac; +using Volo.Abp.Modularity; + +namespace Volo.Abp.PermissionManagement +{ + [DependsOn( + typeof(AbpPermissionManagementDomainModule), + typeof(AbpAutofacModule), + typeof(AbpTestBaseModule) + )] + public class AbpPermissionManagementTestBaseModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.Configure(options => + { + options.DefinitionProviders.Add(); + }); + + services.AddAssemblyOf(); + } + + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + SeedTestData(context); + } + + private static void SeedTestData(ApplicationInitializationContext context) + { + using (var scope = context.ServiceProvider.CreateScope()) + { + scope.ServiceProvider + .GetRequiredService() + .Build(); + } + } + } +} diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/PermissionGrantRepository_Tests.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/PermissionGrantRepository_Tests.cs new file mode 100644 index 0000000000..e27be7e426 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/PermissionGrantRepository_Tests.cs @@ -0,0 +1,36 @@ +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Modularity; +using Xunit; + +namespace Volo.Abp.PermissionManagement +{ + public abstract class PermissionGrantRepository_Tests : PermissionManagementTestBase + where TStartupModule : IAbpModule + { + protected IPermissionGrantRepository PermissionGrantRepository { get; } + + protected PermissionGrantRepository_Tests() + { + PermissionGrantRepository = GetRequiredService(); + } + + [Fact] + public async Task FindAsync() + { + (await PermissionGrantRepository.FindAsync("MyPermission1", UserPermissionValueProvider.ProviderName, PermissionTestDataBuilder.User1Id.ToString())).ShouldNotBeNull(); + + (await PermissionGrantRepository.FindAsync("Undefined-Permission", UserPermissionValueProvider.ProviderName, PermissionTestDataBuilder.User1Id.ToString())).ShouldBeNull(); + (await PermissionGrantRepository.FindAsync("MyPermission1", "Undefined-Provider", "Unknown-Id")).ShouldBeNull(); + } + + [Fact] + public async Task GetListAsync() + { + var permissionGrants = await PermissionGrantRepository.GetListAsync(UserPermissionValueProvider.ProviderName, PermissionTestDataBuilder.User1Id.ToString()); + + permissionGrants.ShouldContain(p => p.Name == "MyPermission1"); + } + } +} diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/PermissionManagementTestBase.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/PermissionManagementTestBase.cs new file mode 100644 index 0000000000..c90537d159 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/PermissionManagementTestBase.cs @@ -0,0 +1,13 @@ +using Volo.Abp.Modularity; + +namespace Volo.Abp.PermissionManagement +{ + public abstract class PermissionManagementTestBase : AbpIntegratedTest + where TStartupModule : IAbpModule + { + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + } +} diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/PermissionTestDataBuilder.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/PermissionTestDataBuilder.cs new file mode 100644 index 0000000000..81fb98b2be --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/PermissionTestDataBuilder.cs @@ -0,0 +1,34 @@ +using System; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Guids; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionTestDataBuilder : ITransientDependency + { + public static Guid User1Id { get; } = Guid.NewGuid(); + public static Guid User2Id { get; } = Guid.NewGuid(); + + private readonly IPermissionGrantRepository _permissionGrantRepository; + private readonly IGuidGenerator _guidGenerator; + + public PermissionTestDataBuilder(IGuidGenerator guidGenerator, IPermissionGrantRepository permissionGrantRepository) + { + _guidGenerator = guidGenerator; + _permissionGrantRepository = permissionGrantRepository; + } + + public void Build() + { + _permissionGrantRepository.Insert( + new PermissionGrant( + _guidGenerator.Create(), + "MyPermission1", + UserPermissionValueProvider.ProviderName, + User1Id.ToString() + ) + ); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/TestPermissionDefinitionProvider.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/TestPermissionDefinitionProvider.cs new file mode 100644 index 0000000000..bd997f76d4 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.TestBase/Volo/Abp/PermissionManagement/TestPermissionDefinitionProvider.cs @@ -0,0 +1,17 @@ +using Volo.Abp.Authorization.Permissions; + +namespace Volo.Abp.PermissionManagement +{ + public class TestPermissionDefinitionProvider : PermissionDefinitionProvider + { + public override void Define(IPermissionDefinitionContext context) + { + var testGroup = context.AddGroup("TestGroup"); + + testGroup.AddPermission("MyPermission1"); + + var myPermission2 = testGroup.AddPermission("MyPermission2"); + myPermission2.AddChild("MyPermission2.ChildPermission1"); + } + } +} \ No newline at end of file diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo.Abp.PermissionManagement.Tests.csproj b/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo.Abp.PermissionManagement.Tests.csproj new file mode 100644 index 0000000000..3ca7e38642 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo.Abp.PermissionManagement.Tests.csproj @@ -0,0 +1,24 @@ + + + + netcoreapp2.0 + Volo.Abp.PermissionManagement.Tests + Volo.Abp.PermissionManagement.Tests + true + false + false + false + + + + + + + + + + + + + + \ No newline at end of file diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/AbpPermissionManagementTestModule.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/AbpPermissionManagementTestModule.cs new file mode 100644 index 0000000000..dfe18255a4 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/AbpPermissionManagementTestModule.cs @@ -0,0 +1,38 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Modularity; +using Volo.Abp.PermissionManagement.EntityFrameworkCore; +using Volo.Abp.Uow; + +namespace Volo.Abp.PermissionManagement +{ + [DependsOn( + typeof(AbpPermissionManagementEntityFrameworkCoreModule), + typeof(AbpPermissionManagementTestBaseModule))] + public class AbpPermissionManagementTestModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddEntityFrameworkInMemoryDatabase(); + + var databaseName = Guid.NewGuid().ToString(); + + services.Configure(options => + { + options.Configure(context => + { + context.DbContextOptions.UseInMemoryDatabase(databaseName); + }); + }); + + services.Configure(options => + { + options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; //EF in-memory database does not support transactions + }); + + services.AddAssemblyOf(); + } + } +} diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/PermissionChecker_User_Tests.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/PermissionChecker_User_Tests.cs new file mode 100644 index 0000000000..280cc0d638 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/PermissionChecker_User_Tests.cs @@ -0,0 +1,59 @@ +using System; +using System.Security.Claims; +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.Security.Claims; +using Xunit; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionChecker_User_Tests : PermissionTestBase + { + private readonly IPermissionChecker _permissionChecker; + + public PermissionChecker_User_Tests() + { + _permissionChecker = GetRequiredService(); + } + + [Fact] + public async Task Should_Return_True_For_Granted_Current_User() + { + (await _permissionChecker.IsGrantedAsync( + CreatePrincipal(PermissionTestDataBuilder.User1Id), + "MyPermission1" + )).ShouldBeTrue(); + } + + [Fact] + public async Task Should_Return_False_For_Non_Granted_Current_User() + { + (await _permissionChecker.IsGrantedAsync( + CreatePrincipal(PermissionTestDataBuilder.User2Id), + "MyPermission1" + )).ShouldBeFalse(); + } + + [Fact] + public async Task Should_Return_False_For_Current_User_If_Anonymous() + { + (await _permissionChecker.IsGrantedAsync( + CreatePrincipal(null), + "MyPermission1" + )).ShouldBeFalse(); + } + + private static ClaimsPrincipal CreatePrincipal(Guid? userId) + { + var claimsIdentity = new ClaimsIdentity(); + + if (userId != null) + { + claimsIdentity.AddClaim(new Claim(AbpClaimTypes.UserId, userId.ToString())); + } + + return new ClaimsPrincipal(claimsIdentity); + } + } +} diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/PermissionManager_Basic_Tests.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/PermissionManager_Basic_Tests.cs new file mode 100644 index 0000000000..1fb52c13a1 --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/PermissionManager_Basic_Tests.cs @@ -0,0 +1,31 @@ +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Authorization.Permissions; +using Xunit; + +namespace Volo.Abp.PermissionManagement +{ + public class PermissionChecker_Basic_Tests : PermissionTestBase + { + private readonly IPermissionChecker _permissionChecker; + + public PermissionChecker_Basic_Tests() + { + _permissionChecker = GetRequiredService(); + } + + [Fact] + public async Task Should_Throw_Exception_If_Permission_Is_Not_Defined() + { + await Assert.ThrowsAsync(async () => + await _permissionChecker.IsGrantedAsync("UndefinedPermissionName") + ); + } + + [Fact] + public async Task Should_Return_False_As_Default_For_Any_Permission() + { + (await _permissionChecker.IsGrantedAsync("MyPermission1")).ShouldBeFalse(); + } + } +} diff --git a/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/PermissionTestBase.cs b/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/PermissionTestBase.cs new file mode 100644 index 0000000000..5e611642aa --- /dev/null +++ b/modules/permission-management/test/Volo.Abp.PermissionManagement.Tests/Volo/Abp/PermissionManagement/PermissionTestBase.cs @@ -0,0 +1,24 @@ +using System; +using Volo.Abp.PermissionManagement.EntityFrameworkCore; + +namespace Volo.Abp.PermissionManagement +{ + public abstract class PermissionTestBase : PermissionManagementTestBase + { + protected virtual void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } + + protected virtual T UsingDbContext(Func action) + { + using (var dbContext = GetRequiredService()) + { + return action.Invoke(dbContext); + } + } + } +}