Skip to content

Commit

Permalink
Add SetForClientAsync extension method to IPermissionManager .
Browse files Browse the repository at this point in the history
Resolve #17796
  • Loading branch information
maliming committed Oct 4, 2023
1 parent 4115819 commit a4d57d6
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.Authorization.Permissions;

namespace Volo.Abp.PermissionManagement;

public static class ClientPermissionManagerExtensions
{
public static Task<PermissionWithGrantedProviders> GetForClientAsync([NotNull] this IPermissionManager permissionManager, string clientId, string permissionName)
{
Check.NotNull(permissionManager, nameof(permissionManager));

return permissionManager.GetAsync(permissionName, ClientPermissionValueProvider.ProviderName, clientId);
}

public static Task<List<PermissionWithGrantedProviders>> GetAllForClientAsync([NotNull] this IPermissionManager permissionManager, string clientId)
{
Check.NotNull(permissionManager, nameof(permissionManager));

return permissionManager.GetAllAsync(ClientPermissionValueProvider.ProviderName, clientId);
}

public static Task SetForClientAsync([NotNull] this IPermissionManager permissionManager, string clientId, [NotNull] string permissionName, bool isGranted)
{
Check.NotNull(permissionManager, nameof(permissionManager));

return permissionManager.SetAsync(permissionName, ClientPermissionValueProvider.ProviderName, clientId, isGranted);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Volo.Abp.PermissionManagement.Domain.IdentityServer\Volo.Abp.PermissionManagement.Domain.IdentityServer.csproj" />
<ProjectReference Include="..\Volo.Abp.IdentityServer.EntityFrameworkCore.Tests\Volo.Abp.IdentityServer.EntityFrameworkCore.Tests.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement.IdentityServer;

namespace Volo.Abp.IdentityServer;

[DependsOn(typeof(AbpIdentityServerTestEntityFrameworkCoreModule))]
[DependsOn(
typeof(AbpIdentityServerTestEntityFrameworkCoreModule),
typeof(AbpPermissionManagementDomainIdentityServerModule)
)]
public class AbpIdentityServerDomainTestModule : AbpModule
{

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.IdentityServer;
using Volo.Abp.PermissionManagement;
using Xunit;

namespace Volo.Abp.OpenIddict;

public class PermissionManager_Tests : AbpIdentityServerDomainTestBase
{
private readonly IPermissionManager _permissionManager;
private readonly IPermissionStore _permissionStore;
private readonly AbpIdentityServerTestData _testData;

public PermissionManager_Tests()
{
_permissionManager = GetRequiredService<IPermissionManager>();
_permissionStore = GetRequiredService<IPermissionStore>();
_testData = GetRequiredService<AbpIdentityServerTestData>();
}

[Fact]
public async Task Should_Grant_Permission_To_Client()
{
(await _permissionManager.GetForClientAsync(_testData.Client1Name, TestPermissionNames.MyPermission1)).IsGranted.ShouldBeFalse();
(await _permissionStore.IsGrantedAsync(TestPermissionNames.MyPermission2, ClientPermissionValueProvider.ProviderName, _testData.Client1Name)).ShouldBeFalse();

await _permissionManager.SetForClientAsync(_testData.Client1Name, TestPermissionNames.MyPermission2, true);

(await _permissionManager.GetForClientAsync(_testData.Client1Name, TestPermissionNames.MyPermission2)).IsGranted.ShouldBeTrue();
(await _permissionStore.IsGrantedAsync(TestPermissionNames.MyPermission2, ClientPermissionValueProvider.ProviderName, _testData.Client1Name)).ShouldBeTrue();
}

[Fact]
public async Task Should_Revoke_Permission_From_Client()
{
await _permissionManager.SetForClientAsync(_testData.Client1Name, TestPermissionNames.MyPermission1, true);
(await _permissionManager.GetForClientAsync(_testData.Client1Name, TestPermissionNames.MyPermission1)).IsGranted.ShouldBeTrue();

await _permissionManager.SetForClientAsync(_testData.Client1Name, TestPermissionNames.MyPermission1, false);
(await _permissionManager.GetForClientAsync(_testData.Client1Name, TestPermissionNames.MyPermission1)).IsGranted.ShouldBeFalse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Volo.Abp.IdentityServer;

public static class TestPermissionNames
{
public static class Groups
{
public const string TestGroup = "TestGroup";
}

public const string MyPermission1 = "MyPermission1";

public const string MyPermission2 = "MyPermission2";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Volo.Abp.Authorization.Permissions;

namespace Volo.Abp.IdentityServer;

public class TestTestPermissionDefinitionProvider : PermissionDefinitionProvider
{
public override void Define(IPermissionDefinitionContext context)
{
var testGroup = context.AddGroup(TestPermissionNames.Groups.TestGroup);
testGroup.AddPermission(TestPermissionNames.MyPermission1);
testGroup.AddPermission(TestPermissionNames.MyPermission2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ProjectReference Include="..\..\..\..\modules\identity\src\Volo.Abp.Identity.EntityFrameworkCore\Volo.Abp.Identity.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\Volo.Abp.IdentityServer.TestBase\Volo.Abp.IdentityServer.TestBase.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.EntityFrameworkCore.Sqlite\Volo.Abp.EntityFrameworkCore.Sqlite.csproj" />
<ProjectReference Include="..\..\..\..\modules\permission-management\src\Volo.Abp.PermissionManagement.EntityFrameworkCore\Volo.Abp.PermissionManagement.EntityFrameworkCore.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.IdentityServer.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.Threading;

namespace Volo.Abp.IdentityServer;
Expand All @@ -16,7 +17,8 @@ namespace Volo.Abp.IdentityServer;
typeof(AbpIdentityEntityFrameworkCoreModule),
typeof(AbpIdentityServerEntityFrameworkCoreModule),
typeof(AbpIdentityServerTestBaseModule),
typeof(AbpEntityFrameworkCoreSqliteModule)
typeof(AbpEntityFrameworkCoreSqliteModule),
typeof(AbpPermissionManagementEntityFrameworkCoreModule)
)]
public class AbpIdentityServerTestEntityFrameworkCoreModule : AbpModule
{
Expand Down Expand Up @@ -46,6 +48,10 @@ private static SqliteConnection CreateDatabaseAndGetConnection()
new DbContextOptionsBuilder<IdentityServerDbContext>().UseSqlite(connection).Options
).GetService<IRelationalDatabaseCreator>().CreateTables();

new PermissionManagementDbContext(
new DbContextOptionsBuilder<PermissionManagementDbContext>().UseSqlite(connection).Options
).GetService<IRelationalDatabaseCreator>().CreateTables();

return connection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class AbpIdentityServerTestData : ISingletonDependency
{
public Guid Client1Id { get; } = Guid.NewGuid();

public string Client1Name { get; } = "ClientId1";

public Guid ApiResource1Id { get; } = Guid.NewGuid();

public Guid IdentityResource1Id { get; } = Guid.NewGuid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private async Task AddIdentityResources()

private async Task AddClients()
{
var client = new Client(_testData.Client1Id, "ClientId1")
var client = new Client(_testData.Client1Id, _testData.Client1Name)
{
Description = nameof(Client.Description),
ClientName = nameof(Client.ClientName),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.Authorization.Permissions;

namespace Volo.Abp.PermissionManagement;

public static class ClientPermissionManagerExtensions
{
public static Task<PermissionWithGrantedProviders> GetForClientAsync([NotNull] this IPermissionManager permissionManager, string clientId, string permissionName)
{
Check.NotNull(permissionManager, nameof(permissionManager));

return permissionManager.GetAsync(permissionName, ClientPermissionValueProvider.ProviderName, clientId);
}

public static Task<List<PermissionWithGrantedProviders>> GetAllForClientAsync([NotNull] this IPermissionManager permissionManager, string clientId)
{
Check.NotNull(permissionManager, nameof(permissionManager));

return permissionManager.GetAllAsync(ClientPermissionValueProvider.ProviderName, clientId);
}

public static Task SetForClientAsync([NotNull] this IPermissionManager permissionManager, string clientId, [NotNull] string permissionName, bool isGranted)
{
Check.NotNull(permissionManager, nameof(permissionManager));

return permissionManager.SetAsync(permissionName, ClientPermissionValueProvider.ProviderName, clientId, isGranted);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Volo.Abp.PermissionManagement.Domain.OpenIddict\Volo.Abp.PermissionManagement.Domain.OpenIddict.csproj" />
<ProjectReference Include="..\Volo.Abp.OpenIddict.EntityFrameworkCore.Tests\Volo.Abp.OpenIddict.EntityFrameworkCore.Tests.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement.OpenIddict;

namespace Volo.Abp.OpenIddict;

Expand All @@ -8,9 +9,10 @@ namespace Volo.Abp.OpenIddict;
* database independent anyway.
*/
[DependsOn(
typeof(OpenIddictEntityFrameworkCoreTestModule)
typeof(OpenIddictEntityFrameworkCoreTestModule),
typeof(AbpPermissionManagementDomainOpenIddictModule)
)]
public class OpenIddictDomainTestModule : AbpModule
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.PermissionManagement;
using Xunit;

namespace Volo.Abp.OpenIddict;

public class PermissionManager_Tests : OpenIddictDomainTestBase
{
private readonly IPermissionManager _permissionManager;
private readonly IPermissionStore _permissionStore;
private readonly AbpOpenIddictTestData _testData;

public PermissionManager_Tests()
{
_permissionManager = GetRequiredService<IPermissionManager>();
_permissionStore = GetRequiredService<IPermissionStore>();
_testData = GetRequiredService<AbpOpenIddictTestData>();
}

[Fact]
public async Task Should_Grant_Permission_To_Client()
{
(await _permissionManager.GetForClientAsync(_testData.App1ClientId, TestPermissionNames.MyPermission1)).IsGranted.ShouldBeFalse();
(await _permissionStore.IsGrantedAsync(TestPermissionNames.MyPermission2, ClientPermissionValueProvider.ProviderName, _testData.App1ClientId)).ShouldBeFalse();

await _permissionManager.SetForClientAsync(_testData.App1ClientId, TestPermissionNames.MyPermission2, true);

(await _permissionManager.GetForClientAsync(_testData.App1ClientId, TestPermissionNames.MyPermission2)).IsGranted.ShouldBeTrue();
(await _permissionStore.IsGrantedAsync(TestPermissionNames.MyPermission2, ClientPermissionValueProvider.ProviderName, _testData.App1ClientId)).ShouldBeTrue();
}

[Fact]
public async Task Should_Revoke_Permission_From_Client()
{
await _permissionManager.SetForClientAsync(_testData.App1ClientId, TestPermissionNames.MyPermission1, true);
(await _permissionManager.GetForClientAsync(_testData.App1ClientId, TestPermissionNames.MyPermission1)).IsGranted.ShouldBeTrue();

await _permissionManager.SetForClientAsync(_testData.App1ClientId, TestPermissionNames.MyPermission1, false);
(await _permissionManager.GetForClientAsync(_testData.App1ClientId, TestPermissionNames.MyPermission1)).IsGranted.ShouldBeFalse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Volo.Abp.Authorization.Permissions;

namespace Volo.Abp.OpenIddict;

public class TestPermissionDefinitionProvider : PermissionDefinitionProvider
{
public override void Define(IPermissionDefinitionContext context)
{
var testGroup = context.AddGroup(TestPermissionNames.Groups.TestGroup);
testGroup.AddPermission(TestPermissionNames.MyPermission1);
testGroup.AddPermission(TestPermissionNames.MyPermission2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Volo.Abp.OpenIddict;

public static class TestPermissionNames
{
public static class Groups
{
public const string TestGroup = "TestGroup";
}

public const string MyPermission1 = "MyPermission1";

public const string MyPermission2 = "MyPermission2";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ProjectReference Include="..\Volo.Abp.OpenIddict.TestBase\Volo.Abp.OpenIddict.TestBase.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.EntityFrameworkCore.Sqlite\Volo.Abp.EntityFrameworkCore.Sqlite.csproj" />
<ProjectReference Include="..\..\..\..\modules\identity\src\Volo.Abp.Identity.EntityFrameworkCore\Volo.Abp.Identity.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\..\..\modules\permission-management\src\Volo.Abp.PermissionManagement.EntityFrameworkCore\Volo.Abp.PermissionManagement.EntityFrameworkCore.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
using Volo.Abp.EntityFrameworkCore.Sqlite;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;

namespace Volo.Abp.OpenIddict.EntityFrameworkCore;

[DependsOn(
typeof(OpenIddictTestBaseModule),
typeof(AbpOpenIddictEntityFrameworkCoreModule),
typeof(AbpIdentityEntityFrameworkCoreModule),
typeof(AbpEntityFrameworkCoreSqliteModule)
typeof(AbpEntityFrameworkCoreSqliteModule),
typeof(AbpPermissionManagementEntityFrameworkCoreModule)
)]
public class OpenIddictEntityFrameworkCoreTestModule : AbpModule
{
Expand All @@ -38,11 +40,15 @@ private static SqliteConnection CreateDatabaseAndGetConnection()
new IdentityDbContext(
new DbContextOptionsBuilder<IdentityDbContext>().UseSqlite(connection).Options
).GetService<IRelationalDatabaseCreator>().CreateTables();

new OpenIddictDbContext(
new DbContextOptionsBuilder<OpenIddictDbContext>().UseSqlite(connection).Options
).GetService<IRelationalDatabaseCreator>().CreateTables();

new PermissionManagementDbContext(
new DbContextOptionsBuilder<PermissionManagementDbContext>().UseSqlite(connection).Options
).GetService<IRelationalDatabaseCreator>().CreateTables();

return connection;
}
}

0 comments on commit a4d57d6

Please sign in to comment.