Browse Source

Merge pull request #9046 from abpframework/maliming/patch-blazor

Use AsyncLocal to store static checker.
pull/9072/head
liangshiwei 5 years ago
committed by GitHub
parent
commit
caf1de5090
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Toolbars/ToolbarManager.cs
  2. 4
      framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionSimpleStateCheckerExtensions.cs
  3. 16
      framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/RequirePermissionsSimpleBatchStateChecker.cs
  4. 16
      framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuManager.cs
  5. 29
      framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/RequirePermissionsSimpleBatchStateChecker_Tests.cs

15
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()) using (var scope = ServiceProvider.CreateScope())
{ {
RequirePermissionsSimpleBatchStateChecker<ToolbarItem>.Instance.ClearCheckModels(); using (RequirePermissionsSimpleBatchStateChecker<ToolbarItem>.Use(new RequirePermissionsSimpleBatchStateChecker<ToolbarItem>()))
{
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 CheckPermissionsAsync(scope.ServiceProvider, toolbar);
{
await contributor.ConfigureToolbarAsync(context);
} }
await CheckPermissionsAsync(scope.ServiceProvider, toolbar);
} }
return toolbar; return toolbar;

4
framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionSimpleStateCheckerExtensions.cs

@ -43,8 +43,8 @@ namespace Volo.Abp.Authorization.Permissions
if (batchCheck) if (batchCheck)
{ {
RequirePermissionsSimpleBatchStateChecker<TState>.Instance.AddCheckModels(new RequirePermissionsSimpleBatchStateCheckerModel<TState>(state, permissions, requiresAll)); RequirePermissionsSimpleBatchStateChecker<TState>.Current.AddCheckModels(new RequirePermissionsSimpleBatchStateCheckerModel<TState>(state, permissions, requiresAll));
state.StateCheckers.Add(RequirePermissionsSimpleBatchStateChecker<TState>.Instance); state.StateCheckers.Add(RequirePermissionsSimpleBatchStateChecker<TState>.Current);
} }
else else
{ {

16
framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/RequirePermissionsSimpleBatchStateChecker.cs

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.SimpleStateChecking; using Volo.Abp.SimpleStateChecking;
@ -9,10 +11,16 @@ namespace Volo.Abp.Authorization.Permissions
public class RequirePermissionsSimpleBatchStateChecker<TState> : SimpleBatchStateCheckerBase<TState> public class RequirePermissionsSimpleBatchStateChecker<TState> : SimpleBatchStateCheckerBase<TState>
where TState : IHasSimpleStateCheckers<TState> where TState : IHasSimpleStateCheckers<TState>
{ {
public static readonly RequirePermissionsSimpleBatchStateChecker<TState> Instance = new RequirePermissionsSimpleBatchStateChecker<TState>(); public static RequirePermissionsSimpleBatchStateChecker<TState> Current => _current.Value;
private static readonly AsyncLocal<RequirePermissionsSimpleBatchStateChecker<TState>> _current = new AsyncLocal<RequirePermissionsSimpleBatchStateChecker<TState>>();
private readonly List<RequirePermissionsSimpleBatchStateCheckerModel<TState>> _models; private readonly List<RequirePermissionsSimpleBatchStateCheckerModel<TState>> _models;
static RequirePermissionsSimpleBatchStateChecker()
{
_current.Value = new RequirePermissionsSimpleBatchStateChecker<TState>();
}
public RequirePermissionsSimpleBatchStateChecker() public RequirePermissionsSimpleBatchStateChecker()
{ {
_models = new List<RequirePermissionsSimpleBatchStateCheckerModel<TState>>(); _models = new List<RequirePermissionsSimpleBatchStateCheckerModel<TState>>();
@ -26,9 +34,11 @@ namespace Volo.Abp.Authorization.Permissions
return this; return this;
} }
public void ClearCheckModels() public static IDisposable Use(RequirePermissionsSimpleBatchStateChecker<TState> checker)
{ {
_models.Clear(); var previousValue = Current;
_current.Value = checker;
return new DisposeAction(() => _current.Value = previousValue);
} }
public override async Task<SimpleStateCheckerResult<TState>> IsEnabledAsync(SimpleBatchStateCheckerContext<TState> context) public override async Task<SimpleStateCheckerResult<TState>> IsEnabledAsync(SimpleBatchStateCheckerContext<TState> context)

16
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 AbpNavigationOptions Options { get; }
protected IHybridServiceScopeFactory ServiceScopeFactory { get; } protected IHybridServiceScopeFactory ServiceScopeFactory { get; }
protected ISimpleStateCheckerManager<ApplicationMenuItem> SimpleStateCheckerManager { get; } protected ISimpleStateCheckerManager<ApplicationMenuItem> SimpleStateCheckerManager { get; }
public MenuManager( public MenuManager(
IOptions<AbpNavigationOptions> options, IOptions<AbpNavigationOptions> options,
IHybridServiceScopeFactory serviceScopeFactory, IHybridServiceScopeFactory serviceScopeFactory,
@ -31,16 +30,17 @@ namespace Volo.Abp.UI.Navigation
using (var scope = ServiceScopeFactory.CreateScope()) using (var scope = ServiceScopeFactory.CreateScope())
{ {
RequirePermissionsSimpleBatchStateChecker<ApplicationMenuItem>.Instance.ClearCheckModels(); using (RequirePermissionsSimpleBatchStateChecker<ApplicationMenuItem>.Use(new RequirePermissionsSimpleBatchStateChecker<ApplicationMenuItem>()))
{
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 CheckPermissionsAsync(scope.ServiceProvider, menu);
{
await contributor.ConfigureMenuAsync(context);
} }
await CheckPermissionsAsync(scope.ServiceProvider, menu);
} }
NormalizeMenu(menu); NormalizeMenu(menu);

29
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/RequirePermissionsSimpleBatchStateChecker_Tests.cs

@ -16,6 +16,25 @@ namespace Volo.Abp.Authorization
_simpleStateCheckerManager = GetRequiredService<ISimpleStateCheckerManager<MyStateEntity>>(); _simpleStateCheckerManager = GetRequiredService<ISimpleStateCheckerManager<MyStateEntity>>();
} }
[Fact]
public void Switch_Current_Checker_Test()
{
var checker = RequirePermissionsSimpleBatchStateChecker<MyStateEntity2>.Current;
checker.ShouldNotBeNull();
RequirePermissionsSimpleBatchStateChecker<MyStateEntity2> checker2 = null;
using (RequirePermissionsSimpleBatchStateChecker<MyStateEntity2>.Use(new RequirePermissionsSimpleBatchStateChecker<MyStateEntity2>()))
{
checker2 = RequirePermissionsSimpleBatchStateChecker<MyStateEntity2>.Current;
checker2.ShouldNotBeNull();
checker2.ShouldNotBe(checker);
}
checker2.ShouldNotBeNull();
checker2.ShouldNotBe(checker);
}
[Fact] [Fact]
public async Task RequirePermissionsSimpleBatchStateChecker_Test() public async Task RequirePermissionsSimpleBatchStateChecker_Test()
{ {
@ -46,5 +65,15 @@ namespace Volo.Abp.Authorization
StateCheckers = new List<ISimpleStateChecker<MyStateEntity>>(); StateCheckers = new List<ISimpleStateChecker<MyStateEntity>>();
} }
} }
class MyStateEntity2 : IHasSimpleStateCheckers<MyStateEntity2>
{
public List<ISimpleStateChecker<MyStateEntity2>> StateCheckers { get; }
public MyStateEntity2()
{
StateCheckers = new List<ISimpleStateChecker<MyStateEntity2>>();
}
}
} }
} }

Loading…
Cancel
Save