mirror of https://github.com/abpframework/abp.git
Browse Source
Improve performance for application-configuration with large number of permissionspull/25232/head
committed by
GitHub
9 changed files with 260 additions and 19 deletions
@ -0,0 +1,51 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization; |
||||
|
|
||||
|
public class PermissionChecker_BulkWithStateChecker_Tests : AuthorizationTestBase |
||||
|
{ |
||||
|
private readonly IPermissionChecker _permissionChecker; |
||||
|
|
||||
|
public PermissionChecker_BulkWithStateChecker_Tests() |
||||
|
{ |
||||
|
_permissionChecker = GetRequiredService<IPermissionChecker>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task IsGrantedAsync_With_Empty_Names_Should_Return_Empty_Result() |
||||
|
{ |
||||
|
var result = await _permissionChecker.IsGrantedAsync(Array.Empty<string>()); |
||||
|
result.Result.ShouldBeEmpty(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task IsGrantedAsync_StateChecker_Permission_Is_Undefined_When_StateChecker_Fails() |
||||
|
{ |
||||
|
// MyPermission1 has TestRequireEditionPermissionSimpleStateChecker.
|
||||
|
// Current user (Douglas) has no EditionId claim → StateChecker returns false.
|
||||
|
// The permission must stay Undefined (never reaches the value-provider pipeline).
|
||||
|
var result = await _permissionChecker.IsGrantedAsync(new[] { "MyPermission1", "MyPermission3" }); |
||||
|
|
||||
|
result.Result["MyPermission1"].ShouldBe(PermissionGrantResult.Undefined); |
||||
|
result.Result["MyPermission3"].ShouldBe(PermissionGrantResult.Granted); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task IsGrantedAsync_Mix_Of_Defined_And_Undefined_Permissions() |
||||
|
{ |
||||
|
var result = await _permissionChecker.IsGrantedAsync(new[] |
||||
|
{ |
||||
|
"MyPermission3", |
||||
|
"NonExistentPermission", |
||||
|
"MyPermission5" |
||||
|
}); |
||||
|
|
||||
|
result.Result["MyPermission3"].ShouldBe(PermissionGrantResult.Granted); |
||||
|
result.Result["NonExistentPermission"].ShouldBe(PermissionGrantResult.Prohibited); |
||||
|
result.Result["MyPermission5"].ShouldBe(PermissionGrantResult.Granted); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,57 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Authorization.Permissions; |
||||
|
using Volo.Abp.Authorization.Permissions.Resources; |
||||
|
using Volo.Abp.Authorization.TestServices.Resources; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization; |
||||
|
|
||||
|
public class ResourcePermissionChecker_BulkWithStateChecker_Tests : AuthorizationTestBase |
||||
|
{ |
||||
|
private readonly IResourcePermissionChecker _resourcePermissionChecker; |
||||
|
|
||||
|
public ResourcePermissionChecker_BulkWithStateChecker_Tests() |
||||
|
{ |
||||
|
_resourcePermissionChecker = GetRequiredService<IResourcePermissionChecker>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task IsGrantedAsync_With_Empty_Names_Should_Return_Empty_Result() |
||||
|
{ |
||||
|
var result = await _resourcePermissionChecker.IsGrantedAsync( |
||||
|
Array.Empty<string>(), TestEntityResource.ResourceName, TestEntityResource.ResourceKey5); |
||||
|
|
||||
|
result.Result.ShouldBeEmpty(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task IsGrantedAsync_StateChecker_Permission_Is_Undefined_When_StateChecker_Fails() |
||||
|
{ |
||||
|
// MyResourcePermission1 has TestRequireEditionPermissionSimpleStateChecker.
|
||||
|
// Current user (Douglas) has no EditionId claim → StateChecker returns false.
|
||||
|
// The permission must stay Undefined (never reaches the value-provider pipeline).
|
||||
|
var result = await _resourcePermissionChecker.IsGrantedAsync( |
||||
|
new[] { "MyResourcePermission1", "MyResourcePermission3" }, |
||||
|
TestEntityResource.ResourceName, |
||||
|
TestEntityResource.ResourceKey3); |
||||
|
|
||||
|
result.Result["MyResourcePermission1"].ShouldBe(PermissionGrantResult.Undefined); |
||||
|
result.Result["MyResourcePermission3"].ShouldBe(PermissionGrantResult.Granted); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task IsGrantedAsync_Mix_Of_Defined_And_Undefined_Permissions() |
||||
|
{ |
||||
|
var result = await _resourcePermissionChecker.IsGrantedAsync( |
||||
|
new[] { "MyResourcePermission3", "NonExistentPermission", "MyResourcePermission5" }, |
||||
|
TestEntityResource.ResourceName, |
||||
|
TestEntityResource.ResourceKey5); |
||||
|
|
||||
|
result.Result["MyResourcePermission3"].ShouldBe(PermissionGrantResult.Granted); |
||||
|
result.Result["NonExistentPermission"].ShouldBe(PermissionGrantResult.Prohibited); |
||||
|
result.Result["MyResourcePermission5"].ShouldBe(PermissionGrantResult.Granted); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,66 @@ |
|||||
|
using System.Globalization; |
||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
public class SimpleStateChecker_NoCheckers_Test : SimpleStateCheckerTestBase |
||||
|
{ |
||||
|
// No GlobalStateCheckers registered — verifies the fast path when both
|
||||
|
// state.StateCheckers and Options.GlobalStateCheckers are empty.
|
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Entity_With_No_State_Checkers_Should_Be_Enabled() |
||||
|
{ |
||||
|
var entity = new MyStateEntity(); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(entity)).ShouldBeTrue(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Entity_With_No_State_Checkers_Should_Not_Increment_Check_Counts() |
||||
|
{ |
||||
|
var entity = new MyStateEntity(); |
||||
|
|
||||
|
await SimpleStateCheckerManager.IsEnabledAsync(entity); |
||||
|
|
||||
|
entity.CheckCount.ShouldBe(0); |
||||
|
entity.GlobalCheckCount.ShouldBe(0); |
||||
|
entity.MultipleCheckCount.ShouldBe(0); |
||||
|
entity.MultipleGlobalCheckCount.ShouldBe(0); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Multiple_Entities_With_No_State_Checkers_Should_All_Be_Enabled() |
||||
|
{ |
||||
|
var entities = new[] |
||||
|
{ |
||||
|
new MyStateEntity { CreationTime = DateTime.Parse("2022-01-01", CultureInfo.InvariantCulture) }, |
||||
|
new MyStateEntity { CreationTime = DateTime.Parse("2019-01-01", CultureInfo.InvariantCulture) } |
||||
|
}; |
||||
|
|
||||
|
var result = await SimpleStateCheckerManager.IsEnabledAsync(entities); |
||||
|
|
||||
|
result.Values.ShouldAllBe(v => v); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Entity_With_Individual_Checker_Should_Still_Be_Checked() |
||||
|
{ |
||||
|
var entity = new MyStateEntity |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2021-01-01", CultureInfo.InvariantCulture) |
||||
|
}; |
||||
|
entity.AddSimpleStateChecker(new MySimpleStateChecker()); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(entity)).ShouldBeTrue(); |
||||
|
entity.CheckCount.ShouldBe(1); |
||||
|
|
||||
|
entity.CreationTime = DateTime.Parse("2001-01-01", CultureInfo.InvariantCulture); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(entity)).ShouldBeFalse(); |
||||
|
entity.CheckCount.ShouldBe(2); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue