Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Volo.Abp.AspNetCore.Mvc.UI.Packages;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.Widgets;
using Volo.Abp.Features;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;

Expand All @@ -12,7 +13,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
[DependsOn(
typeof(AbpAspNetCoreMvcUiBootstrapModule),
typeof(AbpAspNetCoreMvcUiPackagesModule),
typeof(AbpAspNetCoreMvcUiWidgetsModule)
typeof(AbpAspNetCoreMvcUiWidgetsModule),
typeof(AbpFeaturesModule)
)]
public class AbpAspNetCoreMvcUiThemeSharedModule : AbpModule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Volo.Abp.AspNetCore.Mvc.UI.Theming;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Features;
using Volo.Abp.SimpleStateChecking;

namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars;
Expand Down Expand Up @@ -37,6 +38,7 @@ public async Task<Toolbar> GetAsync(string name)
using (var scope = ServiceProvider.CreateScope())
{
using (RequirePermissionsSimpleBatchStateChecker<ToolbarItem>.Use(new RequirePermissionsSimpleBatchStateChecker<ToolbarItem>()))
using (RequireFeaturesSimpleBatchStateChecker<ToolbarItem>.Use(new RequireFeaturesSimpleBatchStateChecker<ToolbarItem>()))
{
var context = new ToolbarConfigurationContext(ThemeManager.CurrentTheme, toolbar, scope.ServiceProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

<ItemGroup>
<ProjectReference Include="..\Volo.Abp.AspNetCore.Mvc.UI.Bootstrap\Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.csproj" />
<ProjectReference Include="..\Volo.Abp.Features\Volo.Abp.Features.csproj" />
<ProjectReference Include="..\Volo.Abp.AspNetCore.Mvc.UI.Packages\Volo.Abp.AspNetCore.Mvc.UI.Packages.csproj" />
<ProjectReference Include="..\Volo.Abp.AspNetCore.Mvc.UI.Widgets\Volo.Abp.AspNetCore.Mvc.UI.Widgets.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -57,7 +57,7 @@ public virtual async Task<SimpleStateCheckerResult<TState>> IsEnabledAsync(TStat

foreach (ISimpleBatchStateChecker<TState> globalStateChecker in Options.GlobalStateCheckers
.Where(x => typeof(ISimpleBatchStateChecker<TState>).IsAssignableFrom(x))
.Select(x => ServiceProvider.GetRequiredService(x)))
.Select(x => scope.ServiceProvider.GetRequiredService(x)))
{
var context = new SimpleBatchStateCheckerContext<TState>(
scope.ServiceProvider.GetRequiredService<ICachedServiceProvider>(),
Expand All @@ -69,11 +69,15 @@ public virtual async Task<SimpleStateCheckerResult<TState>> IsEnabledAsync(TStat
}
}

var hasNonBatchGlobalCheckers = Options.GlobalStateCheckers
.Any(x => !typeof(ISimpleBatchStateChecker<TState>).IsAssignableFrom(x));

foreach (var state in states)
{
if (result[state])
if (result[state] &&
(hasNonBatchGlobalCheckers || state.StateCheckers.Any(x => x is not ISimpleBatchStateChecker<TState>)))
{
result[state] = await InternalIsEnabledAsync(state, false);
result[state] = await EvaluateCheckersAsync(state, false, scope);
}
Comment thread
maliming marked this conversation as resolved.
}
Comment thread
maliming marked this conversation as resolved.

Expand All @@ -96,27 +100,36 @@ protected virtual async Task<bool> InternalIsEnabledAsync(TState state, bool use

using (var scope = ServiceProvider.CreateScope())
{
var context = new SimpleStateCheckerContext<TState>(scope.ServiceProvider.GetRequiredService<ICachedServiceProvider>(), state);
return await EvaluateCheckersAsync(state, useBatchChecker, scope);
}
}

protected virtual async Task<bool> EvaluateCheckersAsync(TState state, bool useBatchChecker, IServiceScope scope)
{
var context = new SimpleStateCheckerContext<TState>(
!useBatchChecker
? scope.ServiceProvider.GetRequiredService<ITransientCachedServiceProvider>()
: scope.ServiceProvider.GetRequiredService<ICachedServiceProvider>(),
state);

foreach (var provider in state.StateCheckers.WhereIf(!useBatchChecker, x => x is not ISimpleBatchStateChecker<TState>))
foreach (var provider in state.StateCheckers.WhereIf(!useBatchChecker, x => x is not ISimpleBatchStateChecker<TState>))
{
if (!await provider.IsEnabledAsync(context))
{
if (!await provider.IsEnabledAsync(context))
{
return false;
}
return false;
}
}

foreach (ISimpleStateChecker<TState> provider in Options.GlobalStateCheckers
.WhereIf(!useBatchChecker, x => !typeof(ISimpleBatchStateChecker<TState>).IsAssignableFrom(x))
.Select(x => ServiceProvider.GetRequiredService(x)))
foreach (ISimpleStateChecker<TState> provider in Options.GlobalStateCheckers
.WhereIf(!useBatchChecker, x => !typeof(ISimpleBatchStateChecker<TState>).IsAssignableFrom(x))
.Select(x => scope.ServiceProvider.GetRequiredService(x)))
{
Comment thread
maliming marked this conversation as resolved.
if (!await provider.IsEnabledAsync(context))
{
if (!await provider.IsEnabledAsync(context))
{
return false;
}
return false;
}

return true;
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

Expand Down Expand Up @@ -28,4 +29,16 @@ public virtual async Task<bool> IsEnabledAsync(string name)
);
}
}

public virtual async Task<Dictionary<string, bool>> IsEnabledAsync(string[] names)
{
var result = new Dictionary<string, bool>();

foreach (var name in names)
{
result[name] = await IsEnabledAsync(name);
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JetBrains.Annotations;
using JetBrains.Annotations;
using Volo.Abp.SimpleStateChecking;

namespace Volo.Abp.Features;
Expand All @@ -10,7 +10,7 @@ public static TState RequireFeatures<TState>(
params string[] features)
where TState : IHasSimpleStateCheckers<TState>
{
state.RequireFeatures(true, features);
state.RequireFeatures(requiresAll: true, batchCheck: true, features);
return state;
}

Expand All @@ -19,11 +19,32 @@ public static TState RequireFeatures<TState>(
bool requiresAll,
params string[] features)
where TState : IHasSimpleStateCheckers<TState>
{
state.RequireFeatures(requiresAll: requiresAll, batchCheck: true, features);
return state;
}

public static TState RequireFeatures<TState>(
[NotNull] this TState state,
bool requiresAll,
bool batchCheck,
params string[] features)
where TState : IHasSimpleStateCheckers<TState>
{
Check.NotNull(state, nameof(state));
Check.NotNullOrEmpty(features, nameof(features));

state.StateCheckers.Add(new RequireFeaturesSimpleStateChecker<TState>(requiresAll, features));
if (batchCheck)
{
RequireFeaturesSimpleBatchStateChecker<TState>.Current.AddCheckModels(
new RequireFeaturesSimpleBatchStateCheckerModel<TState>(state, features, requiresAll));
state.StateCheckers.Add(RequireFeaturesSimpleBatchStateChecker<TState>.Current);
}
else
{
state.StateCheckers.Add(new RequireFeaturesSimpleStateChecker<TState>(requiresAll, features));
}

return state;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Text.Json.Nodes;
using Volo.Abp.DependencyInjection;
using Volo.Abp.SimpleStateChecking;
Expand All @@ -10,7 +10,7 @@ public class FeaturesSimpleStateCheckerSerializerContributor :
ISingletonDependency
{
public const string CheckerShortName = "F";

public string? SerializeToJson<TState>(ISimpleStateChecker<TState> checker)
where TState : IHasSimpleStateCheckers<TState>
{
Expand Down Expand Up @@ -53,4 +53,4 @@ public class FeaturesSimpleStateCheckerSerializerContributor :
nameArray.Select(x => x!.ToString()).ToArray()
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System.Collections.Generic;
using JetBrains.Annotations;
using System.Threading.Tasks;

namespace Volo.Abp.Features;
Expand All @@ -8,4 +9,6 @@ public interface IFeatureChecker
Task<string?> GetOrNullAsync([NotNull] string name);

Task<bool> IsEnabledAsync(string name);

Comment thread
maliming marked this conversation as resolved.
Task<Dictionary<string, bool>> IsEnabledAsync([NotNull] string[] names);
}
Comment thread
maliming marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.SimpleStateChecking;

namespace Volo.Abp.Features;

public class RequireFeaturesSimpleBatchStateChecker<TState> : SimpleBatchStateCheckerBase<TState>
where TState : IHasSimpleStateCheckers<TState>
{
public static RequireFeaturesSimpleBatchStateChecker<TState> Current => _current.Value!;
private static readonly AsyncLocal<RequireFeaturesSimpleBatchStateChecker<TState>> _current = new();

private readonly List<RequireFeaturesSimpleBatchStateCheckerModel<TState>> _models;

static RequireFeaturesSimpleBatchStateChecker()
{
_current.Value = new RequireFeaturesSimpleBatchStateChecker<TState>();
}

public RequireFeaturesSimpleBatchStateChecker()
{
_models = new List<RequireFeaturesSimpleBatchStateCheckerModel<TState>>();
}

public RequireFeaturesSimpleBatchStateChecker<TState> AddCheckModels(
params RequireFeaturesSimpleBatchStateCheckerModel<TState>[] models)
{
Check.NotNullOrEmpty(models, nameof(models));

_models.AddRange(models);
return this;
}

public static IDisposable Use(RequireFeaturesSimpleBatchStateChecker<TState> checker)
{
var previousValue = Current;
_current.Value = checker;
return new DisposeAction(() => _current.Value = previousValue);
}

public override async Task<SimpleStateCheckerResult<TState>> IsEnabledAsync(
SimpleBatchStateCheckerContext<TState> context)
{
var featureChecker = context.ServiceProvider.GetRequiredService<IFeatureChecker>();

var result = new SimpleStateCheckerResult<TState>(context.States);

var relevantModels = _models
.Where(x => context.States.Any(s => s.Equals(x.State)))
.ToList();

var features = relevantModels.SelectMany(x => x.FeatureNames).Distinct().ToArray();
var featureValues = await featureChecker.IsEnabledAsync(features);

foreach (var state in context.States)
{
var model = relevantModels.FirstOrDefault(x => x.State.Equals(state));
if (model != null)
Comment thread
maliming marked this conversation as resolved.
Outdated
{
if (model.RequiresAll)
{
result[state] = model.FeatureNames.All(x => featureValues.TryGetValue(x, out var v) && v);
}
else
{
result[state] = model.FeatureNames.Any(x => featureValues.TryGetValue(x, out var v) && v);
}
}
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Volo.Abp.SimpleStateChecking;

namespace Volo.Abp.Features;

public class RequireFeaturesSimpleBatchStateCheckerModel<TState>
where TState : IHasSimpleStateCheckers<TState>
{
public TState State { get; }

public string[] FeatureNames { get; }

public bool RequiresAll { get; }

public RequireFeaturesSimpleBatchStateCheckerModel(TState state, string[] featureNames, bool requiresAll = true)
{
Check.NotNull(state, nameof(state));
Check.NotNullOrEmpty(featureNames, nameof(featureNames));

State = state;
FeatureNames = featureNames;
RequiresAll = requiresAll;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Authorization\Volo.Abp.Authorization.csproj" />
<ProjectReference Include="..\Volo.Abp.Features\Volo.Abp.Features.csproj" />
<ProjectReference Include="..\Volo.Abp.UI\Volo.Abp.UI.csproj" />
<ProjectReference Include="..\Volo.Abp.MultiTenancy\Volo.Abp.MultiTenancy.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Volo.Abp.Authorization;
using Volo.Abp.Features;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
Expand All @@ -7,7 +8,7 @@

namespace Volo.Abp.UI.Navigation;

[DependsOn(typeof(AbpUiModule), typeof(AbpAuthorizationModule), typeof(AbpMultiTenancyModule))]
[DependsOn(typeof(AbpUiModule), typeof(AbpAuthorizationModule), typeof(AbpFeaturesModule), typeof(AbpMultiTenancyModule))]
public class AbpUiNavigationModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Options;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Features;
using Volo.Abp.SimpleStateChecking;

namespace Volo.Abp.UI.Navigation;
Expand Down Expand Up @@ -82,6 +83,7 @@ protected virtual async Task<ApplicationMenu> GetInternalAsync(string name)
using (var scope = ServiceScopeFactory.CreateScope())
{
using (RequirePermissionsSimpleBatchStateChecker<ApplicationMenuItem>.Use(new RequirePermissionsSimpleBatchStateChecker<ApplicationMenuItem>()))
using (RequireFeaturesSimpleBatchStateChecker<ApplicationMenuItem>.Use(new RequireFeaturesSimpleBatchStateChecker<ApplicationMenuItem>()))
{
var context = new MenuConfigurationContext(menu, scope.ServiceProvider);

Expand Down
Loading
Loading