mirror of https://github.com/abpframework/abp
parent
4115819755
commit
a4d57d63fe
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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";
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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";
|
||||
}
|
Loading…
Reference in new issue