diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Toolbars/ToolbarManager.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Toolbars/ToolbarManager.cs index ecd73ac5ab..53679c5b22 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Toolbars/ToolbarManager.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Toolbars/ToolbarManager.cs @@ -36,16 +36,17 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars using (var scope = ServiceProvider.CreateScope()) { - RequirePermissionsSimpleBatchStateChecker.Instance.ClearCheckModels(); + using (RequirePermissionsSimpleBatchStateChecker.Use(new RequirePermissionsSimpleBatchStateChecker())) + { + var context = new ToolbarConfigurationContext(ThemeManager.CurrentTheme, toolbar, scope.ServiceProvider); - var context = new ToolbarConfigurationContext(ThemeManager.CurrentTheme, toolbar, scope.ServiceProvider); + foreach (var contributor in Options.Contributors) + { + await contributor.ConfigureToolbarAsync(context); + } - foreach (var contributor in Options.Contributors) - { - await contributor.ConfigureToolbarAsync(context); + await CheckPermissionsAsync(scope.ServiceProvider, toolbar); } - - await CheckPermissionsAsync(scope.ServiceProvider, toolbar); } return toolbar; diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionSimpleStateCheckerExtensions.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionSimpleStateCheckerExtensions.cs index 5fc22cb93c..c8c4d315fd 100644 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionSimpleStateCheckerExtensions.cs +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionSimpleStateCheckerExtensions.cs @@ -43,8 +43,8 @@ namespace Volo.Abp.Authorization.Permissions if (batchCheck) { - RequirePermissionsSimpleBatchStateChecker.Instance.AddCheckModels(new RequirePermissionsSimpleBatchStateCheckerModel(state, permissions, requiresAll)); - state.StateCheckers.Add(RequirePermissionsSimpleBatchStateChecker.Instance); + RequirePermissionsSimpleBatchStateChecker.Current.AddCheckModels(new RequirePermissionsSimpleBatchStateCheckerModel(state, permissions, requiresAll)); + state.StateCheckers.Add(RequirePermissionsSimpleBatchStateChecker.Current); } else { diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/RequirePermissionsSimpleBatchStateChecker.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/RequirePermissionsSimpleBatchStateChecker.cs index ebd91bf14f..e03b2b87e3 100644 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/RequirePermissionsSimpleBatchStateChecker.cs +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/RequirePermissionsSimpleBatchStateChecker.cs @@ -1,5 +1,7 @@ +using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.SimpleStateChecking; @@ -9,10 +11,16 @@ namespace Volo.Abp.Authorization.Permissions public class RequirePermissionsSimpleBatchStateChecker : SimpleBatchStateCheckerBase where TState : IHasSimpleStateCheckers { - public static readonly RequirePermissionsSimpleBatchStateChecker Instance = new RequirePermissionsSimpleBatchStateChecker(); + public static RequirePermissionsSimpleBatchStateChecker Current => _current.Value; + private static readonly AsyncLocal> _current = new AsyncLocal>(); private readonly List> _models; + static RequirePermissionsSimpleBatchStateChecker() + { + _current.Value = new RequirePermissionsSimpleBatchStateChecker(); + } + public RequirePermissionsSimpleBatchStateChecker() { _models = new List>(); @@ -26,9 +34,11 @@ namespace Volo.Abp.Authorization.Permissions return this; } - public void ClearCheckModels() + public static IDisposable Use(RequirePermissionsSimpleBatchStateChecker checker) { - _models.Clear(); + var previousValue = Current; + _current.Value = checker; + return new DisposeAction(() => _current.Value = previousValue); } public override async Task> IsEnabledAsync(SimpleBatchStateCheckerContext context) diff --git a/framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuManager.cs b/framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuManager.cs index d58c88b6e6..0b52aff7b8 100644 --- a/framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuManager.cs +++ b/framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuManager.cs @@ -14,7 +14,6 @@ namespace Volo.Abp.UI.Navigation protected AbpNavigationOptions Options { get; } protected IHybridServiceScopeFactory ServiceScopeFactory { get; } protected ISimpleStateCheckerManager SimpleStateCheckerManager { get; } - public MenuManager( IOptions options, IHybridServiceScopeFactory serviceScopeFactory, @@ -31,16 +30,17 @@ namespace Volo.Abp.UI.Navigation using (var scope = ServiceScopeFactory.CreateScope()) { - RequirePermissionsSimpleBatchStateChecker.Instance.ClearCheckModels(); + using (RequirePermissionsSimpleBatchStateChecker.Use(new RequirePermissionsSimpleBatchStateChecker())) + { + var context = new MenuConfigurationContext(menu, scope.ServiceProvider); - var context = new MenuConfigurationContext(menu, scope.ServiceProvider); + foreach (var contributor in Options.MenuContributors) + { + await contributor.ConfigureMenuAsync(context); + } - foreach (var contributor in Options.MenuContributors) - { - await contributor.ConfigureMenuAsync(context); + await CheckPermissionsAsync(scope.ServiceProvider, menu); } - - await CheckPermissionsAsync(scope.ServiceProvider, menu); } NormalizeMenu(menu); diff --git a/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/RequirePermissionsSimpleBatchStateChecker_Tests.cs b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/RequirePermissionsSimpleBatchStateChecker_Tests.cs index 6d75a80f1c..a3b7a96eea 100644 --- a/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/RequirePermissionsSimpleBatchStateChecker_Tests.cs +++ b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/RequirePermissionsSimpleBatchStateChecker_Tests.cs @@ -16,6 +16,25 @@ namespace Volo.Abp.Authorization _simpleStateCheckerManager = GetRequiredService>(); } + [Fact] + public void Switch_Current_Checker_Test() + { + var checker = RequirePermissionsSimpleBatchStateChecker.Current; + checker.ShouldNotBeNull(); + + RequirePermissionsSimpleBatchStateChecker checker2 = null; + + using (RequirePermissionsSimpleBatchStateChecker.Use(new RequirePermissionsSimpleBatchStateChecker())) + { + checker2 = RequirePermissionsSimpleBatchStateChecker.Current; + checker2.ShouldNotBeNull(); + checker2.ShouldNotBe(checker); + } + + checker2.ShouldNotBeNull(); + checker2.ShouldNotBe(checker); + } + [Fact] public async Task RequirePermissionsSimpleBatchStateChecker_Test() { @@ -46,5 +65,15 @@ namespace Volo.Abp.Authorization StateCheckers = new List>(); } } + + class MyStateEntity2 : IHasSimpleStateCheckers + { + public List> StateCheckers { get; } + + public MyStateEntity2() + { + StateCheckers = new List>(); + } + } } }