mirror of https://github.com/abpframework/abp.git
54 changed files with 1052 additions and 291 deletions
@ -1,9 +0,0 @@ |
|||||
using System.Threading.Tasks; |
|
||||
|
|
||||
namespace Volo.Abp.Authorization.Permissions |
|
||||
{ |
|
||||
public interface IPermissionStateManager |
|
||||
{ |
|
||||
Task<bool> IsEnabledAsync(PermissionDefinition permission); |
|
||||
} |
|
||||
} |
|
||||
@ -1,9 +0,0 @@ |
|||||
using System.Threading.Tasks; |
|
||||
|
|
||||
namespace Volo.Abp.Authorization.Permissions |
|
||||
{ |
|
||||
public interface IPermissionStateProvider |
|
||||
{ |
|
||||
Task<bool> IsEnabledAsync(PermissionStateContext context); |
|
||||
} |
|
||||
} |
|
||||
@ -1,19 +0,0 @@ |
|||||
using JetBrains.Annotations; |
|
||||
|
|
||||
namespace Volo.Abp.Authorization.Permissions |
|
||||
{ |
|
||||
public static class PermissionDefinitionExtensions |
|
||||
{ |
|
||||
public static PermissionDefinition AddStateProviders( |
|
||||
[NotNull] this PermissionDefinition permissionDefinition, |
|
||||
[NotNull] params IPermissionStateProvider[] permissionStateProviders) |
|
||||
{ |
|
||||
Check.NotNull(permissionDefinition, nameof(permissionDefinition)); |
|
||||
Check.NotNull(permissionStateProviders, nameof(permissionStateProviders)); |
|
||||
|
|
||||
permissionDefinition.StateProviders.AddRange(permissionStateProviders); |
|
||||
|
|
||||
return permissionDefinition; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,53 @@ |
|||||
|
using JetBrains.Annotations; |
||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.Permissions |
||||
|
{ |
||||
|
public static class PermissionSimpleStateCheckerExtensions |
||||
|
{ |
||||
|
public static TState RequirePermissions<TState>( |
||||
|
[NotNull] this TState state, |
||||
|
params string[] permissions) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
state.RequirePermissions(requiresAll: true, batchCheck: true, permissions); |
||||
|
return state; |
||||
|
} |
||||
|
|
||||
|
public static TState RequirePermissions<TState>( |
||||
|
[NotNull] this TState state, |
||||
|
bool requiresAll, |
||||
|
params string[] permissions) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
state.RequirePermissions(requiresAll: requiresAll, batchCheck: true, permissions); |
||||
|
return state; |
||||
|
} |
||||
|
|
||||
|
public static TState RequirePermissions<TState>( |
||||
|
[NotNull] this TState state, |
||||
|
bool requiresAll, |
||||
|
bool batchCheck = true, |
||||
|
params string[] permissions) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
Check.NotNull(state, nameof(state)); |
||||
|
Check.NotNullOrEmpty(permissions, nameof(permissions)); |
||||
|
|
||||
|
if (batchCheck) |
||||
|
{ |
||||
|
lock (state) |
||||
|
{ |
||||
|
RequirePermissionsSimpleBatchStateChecker<TState>.Instance.AddCheckModels(new RequirePermissionsSimpleBatchStateCheckerModel<TState>(state, permissions, requiresAll)); |
||||
|
state.SimpleStateCheckers.Add(RequirePermissionsSimpleBatchStateChecker<TState>.Instance); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
state.SimpleStateCheckers.Add(new RequirePermissionsSimpleStateChecker<TState>(new RequirePermissionsSimpleBatchStateCheckerModel<TState>(state, permissions, requiresAll))); |
||||
|
} |
||||
|
|
||||
|
return state; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,57 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.Permissions |
||||
|
{ |
||||
|
public class RequirePermissionsSimpleBatchStateChecker<TState> : SimpleBatchStateCheckerBase<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
public static readonly RequirePermissionsSimpleBatchStateChecker<TState> Instance = new RequirePermissionsSimpleBatchStateChecker<TState>(); |
||||
|
|
||||
|
private readonly List<RequirePermissionsSimpleBatchStateCheckerModel<TState>> _models; |
||||
|
|
||||
|
public RequirePermissionsSimpleBatchStateChecker() |
||||
|
{ |
||||
|
_models = new List<RequirePermissionsSimpleBatchStateCheckerModel<TState>>(); |
||||
|
} |
||||
|
|
||||
|
public RequirePermissionsSimpleBatchStateChecker<TState> AddCheckModels(params RequirePermissionsSimpleBatchStateCheckerModel<TState>[] models) |
||||
|
{ |
||||
|
Check.NotNullOrEmpty(models, nameof(models)); |
||||
|
|
||||
|
_models.AddRange(models); |
||||
|
return this; |
||||
|
} |
||||
|
|
||||
|
public override async Task<SimpleStateCheckerResult<TState>> IsEnabledAsync(SimpleBatchStateCheckerContext<TState> context) |
||||
|
{ |
||||
|
var permissionChecker = context.ServiceProvider.GetRequiredService<IPermissionChecker>(); |
||||
|
|
||||
|
var result = new SimpleStateCheckerResult<TState>(context.States); |
||||
|
|
||||
|
var permissions = _models.Where(x => context.States.Any(s => s.Equals(x.State))).SelectMany(x => x.Permissions).Distinct().ToArray(); |
||||
|
var grantResult = await permissionChecker.IsGrantedAsync(permissions); |
||||
|
|
||||
|
foreach (var state in context.States) |
||||
|
{ |
||||
|
var model = _models.FirstOrDefault(x => x.State.Equals(state)); |
||||
|
if (model != null) |
||||
|
{ |
||||
|
if (model.RequiresAll) |
||||
|
{ |
||||
|
result[model.State] = model.Permissions.All(x => grantResult.Result.Any(y => y.Key == x && y.Value == PermissionGrantResult.Granted)); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
result[model.State] = grantResult.Result.Any(x => model.Permissions.Contains(x.Key) && x.Value == PermissionGrantResult.Granted); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.Permissions |
||||
|
{ |
||||
|
public class RequirePermissionsSimpleBatchStateCheckerModel<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
public TState State { get; } |
||||
|
|
||||
|
public string[] Permissions { get; } |
||||
|
|
||||
|
public bool RequiresAll { get; } |
||||
|
|
||||
|
public RequirePermissionsSimpleBatchStateCheckerModel(TState state, string[] permissions, bool requiresAll = true) |
||||
|
{ |
||||
|
Check.NotNull(state, nameof(state)); |
||||
|
Check.NotNullOrEmpty(permissions, nameof(permissions)); |
||||
|
|
||||
|
State = state; |
||||
|
Permissions = permissions; |
||||
|
RequiresAll = requiresAll; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
namespace Volo.Abp.Authorization.Permissions |
||||
|
{ |
||||
|
public class RequirePermissionsSimpleStateChecker<TState> : ISimpleStateChecker<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
private readonly RequirePermissionsSimpleBatchStateCheckerModel<TState> _model; |
||||
|
|
||||
|
public RequirePermissionsSimpleStateChecker(RequirePermissionsSimpleBatchStateCheckerModel<TState> model) |
||||
|
{ |
||||
|
_model = model; |
||||
|
} |
||||
|
|
||||
|
public async Task<bool> IsEnabledAsync(SimpleStateCheckerContext<TState> context) |
||||
|
{ |
||||
|
var permissionChecker = context.ServiceProvider.GetRequiredService<IPermissionChecker>(); |
||||
|
|
||||
|
if (_model.Permissions.Length == 1) |
||||
|
{ |
||||
|
return await permissionChecker.IsGrantedAsync(_model.Permissions.First()); |
||||
|
} |
||||
|
|
||||
|
var grantResult = await permissionChecker.IsGrantedAsync(_model.Permissions); |
||||
|
|
||||
|
return _model.RequiresAll |
||||
|
? grantResult.AllGranted |
||||
|
: grantResult.Result.Any(x => _model.Permissions.Contains(x.Key) && x.Value == PermissionGrantResult.Granted); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,51 +0,0 @@ |
|||||
using System; |
|
||||
using System.Linq; |
|
||||
using System.Threading.Tasks; |
|
||||
using Microsoft.Extensions.DependencyInjection; |
|
||||
using Microsoft.Extensions.Options; |
|
||||
using Volo.Abp.DependencyInjection; |
|
||||
|
|
||||
namespace Volo.Abp.Authorization.Permissions |
|
||||
{ |
|
||||
public class PermissionStateManager : IPermissionStateManager, ITransientDependency |
|
||||
{ |
|
||||
protected IServiceProvider ServiceProvider { get; } |
|
||||
protected AbpPermissionOptions Options { get; } |
|
||||
|
|
||||
public PermissionStateManager(IServiceProvider serviceProvider, IOptions<AbpPermissionOptions> options) |
|
||||
{ |
|
||||
ServiceProvider = serviceProvider; |
|
||||
Options = options.Value; |
|
||||
} |
|
||||
|
|
||||
public async Task<bool> IsEnabledAsync(PermissionDefinition permission) |
|
||||
{ |
|
||||
using (var scope = ServiceProvider.CreateScope()) |
|
||||
{ |
|
||||
var context = new PermissionStateContext |
|
||||
{ |
|
||||
Permission = permission, |
|
||||
ServiceProvider = scope.ServiceProvider.GetRequiredService<ICachedServiceProvider>() |
|
||||
}; |
|
||||
|
|
||||
foreach (var provider in permission.StateProviders) |
|
||||
{ |
|
||||
if (!await provider.IsEnabledAsync(context)) |
|
||||
{ |
|
||||
return false; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
foreach (IPermissionStateProvider provider in Options.GlobalStateProviders.Select(x => ServiceProvider.GetRequiredService(x))) |
|
||||
{ |
|
||||
if (!await provider.IsEnabledAsync(context)) |
|
||||
{ |
|
||||
return false; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return true; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,15 @@ |
|||||
|
using Volo.Abp.Collections; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public class AbpSimpleStateCheckerOptions<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
public ITypeList<ISimpleStateChecker<TState>> GlobalSimpleStateCheckers { get; } |
||||
|
|
||||
|
public AbpSimpleStateCheckerOptions() |
||||
|
{ |
||||
|
GlobalSimpleStateCheckers = new TypeList<ISimpleStateChecker<TState>>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public interface IHasSimpleStateCheckers<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
List<ISimpleStateChecker<TState>> SimpleStateCheckers { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public interface ISimpleBatchStateChecker<TState> : ISimpleStateChecker<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
Task<SimpleStateCheckerResult<TState>> IsEnabledAsync(SimpleBatchStateCheckerContext<TState> context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public interface ISimpleStateChecker<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
Task<bool> IsEnabledAsync(SimpleStateCheckerContext<TState> context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public interface ISimpleStateCheckerManager<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
Task<bool> IsEnabledAsync(TState state); |
||||
|
|
||||
|
Task<SimpleStateCheckerResult<TState>> IsEnabledAsync(TState[] states); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public abstract class SimpleBatchStateCheckerBase<TState>: ISimpleBatchStateChecker<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
public async Task<bool> IsEnabledAsync(SimpleStateCheckerContext<TState> context) |
||||
|
{ |
||||
|
return (await IsEnabledAsync(new SimpleBatchStateCheckerContext<TState>(context.ServiceProvider, new[] {context.State}))).Values.All(x => x); |
||||
|
} |
||||
|
|
||||
|
public abstract Task<SimpleStateCheckerResult<TState>> IsEnabledAsync(SimpleBatchStateCheckerContext<TState> context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public class SimpleBatchStateCheckerContext<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
public IServiceProvider ServiceProvider { get; } |
||||
|
|
||||
|
public TState[] States { get; } |
||||
|
|
||||
|
public SimpleBatchStateCheckerContext(IServiceProvider serviceProvider, TState[] states) |
||||
|
{ |
||||
|
ServiceProvider = serviceProvider; |
||||
|
States = states; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public class SimpleStateCheckerContext<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
public IServiceProvider ServiceProvider { get; } |
||||
|
|
||||
|
public TState State { get; } |
||||
|
|
||||
|
public SimpleStateCheckerContext(IServiceProvider serviceProvider, TState state) |
||||
|
{ |
||||
|
ServiceProvider = serviceProvider; |
||||
|
State = state; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,112 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public class SimpleStateCheckerManager<TState> : ISimpleStateCheckerManager<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
protected IServiceProvider ServiceProvider { get; } |
||||
|
|
||||
|
protected AbpSimpleStateCheckerOptions<TState> Options { get; } |
||||
|
|
||||
|
public SimpleStateCheckerManager(IServiceProvider serviceProvider, IOptions<AbpSimpleStateCheckerOptions<TState>> options) |
||||
|
{ |
||||
|
ServiceProvider = serviceProvider; |
||||
|
Options = options.Value; |
||||
|
} |
||||
|
|
||||
|
public virtual async Task<bool> IsEnabledAsync(TState state) |
||||
|
{ |
||||
|
return await InternalIsEnabledAsync(state, true); |
||||
|
} |
||||
|
|
||||
|
public virtual async Task<SimpleStateCheckerResult<TState>> IsEnabledAsync(TState[] states) |
||||
|
{ |
||||
|
var result = new SimpleStateCheckerResult<TState>(states); |
||||
|
|
||||
|
using (var scope = ServiceProvider.CreateScope()) |
||||
|
{ |
||||
|
var batchStateCheckers = states.SelectMany(x => x.SimpleStateCheckers) |
||||
|
.Where(x => x is ISimpleBatchStateChecker<TState>) |
||||
|
.Cast<ISimpleBatchStateChecker<TState>>() |
||||
|
.GroupBy(x => x) |
||||
|
.Select(x => x.Key); |
||||
|
|
||||
|
foreach (var stateChecker in batchStateCheckers) |
||||
|
{ |
||||
|
var context = new SimpleBatchStateCheckerContext<TState>( |
||||
|
scope.ServiceProvider.GetRequiredService<ICachedServiceProvider>(), |
||||
|
states.Where(x => x.SimpleStateCheckers.Contains(stateChecker)).ToArray()); |
||||
|
|
||||
|
foreach (var x in await stateChecker.IsEnabledAsync(context)) |
||||
|
{ |
||||
|
result[x.Key] = x.Value; |
||||
|
} |
||||
|
|
||||
|
if (result.Values.All(x => !x)) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
foreach (ISimpleBatchStateChecker<TState> globalStateChecker in Options.GlobalSimpleStateCheckers |
||||
|
.Where(x => typeof(ISimpleBatchStateChecker<TState>).IsAssignableFrom(x)) |
||||
|
.Select(x => ServiceProvider.GetRequiredService(x))) |
||||
|
{ |
||||
|
var context = new SimpleBatchStateCheckerContext<TState>( |
||||
|
scope.ServiceProvider.GetRequiredService<ICachedServiceProvider>(), |
||||
|
states.Where(x => result.Any(y => y.Key.Equals(x) && y.Value)).ToArray()); |
||||
|
|
||||
|
foreach (var x in await globalStateChecker.IsEnabledAsync(context)) |
||||
|
{ |
||||
|
result[x.Key] = x.Value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
foreach (var state in states) |
||||
|
{ |
||||
|
if (result[state]) |
||||
|
{ |
||||
|
result[state] = await InternalIsEnabledAsync(state, false); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected virtual async Task<bool> InternalIsEnabledAsync(TState state, bool useBatchChecker) |
||||
|
{ |
||||
|
using (var scope = ServiceProvider.CreateScope()) |
||||
|
{ |
||||
|
var context = new SimpleStateCheckerContext<TState>(scope.ServiceProvider.GetRequiredService<ICachedServiceProvider>(), state); |
||||
|
|
||||
|
foreach (var provider in state.SimpleStateCheckers.WhereIf(!useBatchChecker, x => x is not ISimpleBatchStateChecker<TState>)) |
||||
|
{ |
||||
|
if (!await provider.IsEnabledAsync(context)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
foreach (ISimpleStateChecker<TState> provider in Options.GlobalSimpleStateCheckers |
||||
|
.WhereIf(!useBatchChecker, x => !typeof(ISimpleBatchStateChecker<TState>).IsAssignableFrom(x)) |
||||
|
.Select(x => ServiceProvider.GetRequiredService(x))) |
||||
|
{ |
||||
|
if (!await provider.IsEnabledAsync(context)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public class SimpleStateCheckerResult<TState> : Dictionary<TState, bool> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
public SimpleStateCheckerResult() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public SimpleStateCheckerResult(IEnumerable<TState> states, bool initValue = true) |
||||
|
{ |
||||
|
foreach (var state in states) |
||||
|
{ |
||||
|
Add(state, initValue); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
using JetBrains.Annotations; |
||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
namespace Volo.Abp.Features |
||||
|
{ |
||||
|
public static class FeatureSimpleStateCheckerExtensions |
||||
|
{ |
||||
|
public static TState RequireFeatures<TState>( |
||||
|
[NotNull] this TState state, |
||||
|
params string[] features) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
state.RequireFeatures(true, features); |
||||
|
return state; |
||||
|
} |
||||
|
|
||||
|
public static TState RequireFeatures<TState>( |
||||
|
[NotNull] this TState state, |
||||
|
bool requiresAll, |
||||
|
params string[] features) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
Check.NotNull(state, nameof(state)); |
||||
|
Check.NotNullOrEmpty(features, nameof(features)); |
||||
|
|
||||
|
state.SimpleStateCheckers.Add(new RequireFeaturesSimpleStateChecker<TState>(requiresAll, features)); |
||||
|
return state; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,28 +0,0 @@ |
|||||
using JetBrains.Annotations; |
|
||||
using Volo.Abp.Authorization.Permissions; |
|
||||
|
|
||||
namespace Volo.Abp.Features |
|
||||
{ |
|
||||
public static class FeatureDefinitionExtensions |
|
||||
{ |
|
||||
public static PermissionDefinition RequireFeatures( |
|
||||
[NotNull] this PermissionDefinition permissionDefinition, |
|
||||
params string[] features) |
|
||||
{ |
|
||||
return permissionDefinition.RequireFeatures(true, features); |
|
||||
} |
|
||||
|
|
||||
public static PermissionDefinition RequireFeatures( |
|
||||
[NotNull] this PermissionDefinition permissionDefinition, |
|
||||
bool requiresAll, |
|
||||
params string[] features) |
|
||||
{ |
|
||||
Check.NotNull(permissionDefinition, nameof(permissionDefinition)); |
|
||||
Check.NotNullOrEmpty(features, nameof(features)); |
|
||||
|
|
||||
return permissionDefinition.AddStateProviders( |
|
||||
new RequireFeaturesPermissionStateProvider(requiresAll, features) |
|
||||
); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,32 +0,0 @@ |
|||||
using System.Threading.Tasks; |
|
||||
using Microsoft.Extensions.DependencyInjection; |
|
||||
using Volo.Abp.Authorization.Permissions; |
|
||||
|
|
||||
namespace Volo.Abp.Features |
|
||||
{ |
|
||||
public class RequireFeaturesPermissionStateProvider : IPermissionStateProvider |
|
||||
{ |
|
||||
private readonly string[] _featureNames; |
|
||||
private readonly bool _requiresAll; |
|
||||
|
|
||||
public RequireFeaturesPermissionStateProvider(params string[] featureNames) |
|
||||
: this(true, featureNames) |
|
||||
{ |
|
||||
|
|
||||
} |
|
||||
|
|
||||
public RequireFeaturesPermissionStateProvider(bool requiresAll, params string[] featureNames) |
|
||||
{ |
|
||||
Check.NotNullOrEmpty(featureNames, nameof(featureNames)); |
|
||||
|
|
||||
_requiresAll = requiresAll; |
|
||||
_featureNames = featureNames; |
|
||||
} |
|
||||
|
|
||||
public async Task<bool> IsEnabledAsync(PermissionStateContext context) |
|
||||
{ |
|
||||
var feature = context.ServiceProvider.GetRequiredService<IFeatureChecker>(); |
|
||||
return await feature.IsEnabledAsync(_requiresAll, _featureNames); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,32 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
namespace Volo.Abp.Features |
||||
|
{ |
||||
|
public class RequireFeaturesSimpleStateChecker<TState> : ISimpleStateChecker<TState> |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
private readonly string[] _featureNames; |
||||
|
private readonly bool _requiresAll; |
||||
|
|
||||
|
public RequireFeaturesSimpleStateChecker(params string[] featureNames) |
||||
|
: this(true, featureNames) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public RequireFeaturesSimpleStateChecker(bool requiresAll, params string[] featureNames) |
||||
|
{ |
||||
|
Check.NotNullOrEmpty(featureNames, nameof(featureNames)); |
||||
|
|
||||
|
_requiresAll = requiresAll; |
||||
|
_featureNames = featureNames; |
||||
|
} |
||||
|
|
||||
|
public async Task<bool> IsEnabledAsync(SimpleStateCheckerContext<TState> context) |
||||
|
{ |
||||
|
var featureChecker = context.ServiceProvider.GetRequiredService<IFeatureChecker>(); |
||||
|
return await featureChecker.IsEnabledAsync(_requiresAll, _featureNames); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,50 +0,0 @@ |
|||||
using System; |
|
||||
using JetBrains.Annotations; |
|
||||
using Volo.Abp.Authorization.Permissions; |
|
||||
|
|
||||
namespace Volo.Abp.GlobalFeatures |
|
||||
{ |
|
||||
public static class GlobalFeatureDefinitionExtensions |
|
||||
{ |
|
||||
public static PermissionDefinition RequireGlobalFeatures( |
|
||||
this PermissionDefinition permissionDefinition, |
|
||||
params string[] globalFeatures) |
|
||||
{ |
|
||||
return permissionDefinition.RequireGlobalFeatures(true, globalFeatures); |
|
||||
} |
|
||||
|
|
||||
public static PermissionDefinition RequireGlobalFeatures( |
|
||||
[NotNull] this PermissionDefinition permissionDefinition, |
|
||||
bool requiresAll, |
|
||||
params string[] globalFeatures) |
|
||||
{ |
|
||||
Check.NotNull(permissionDefinition, nameof(permissionDefinition)); |
|
||||
Check.NotNullOrEmpty(globalFeatures, nameof(globalFeatures)); |
|
||||
|
|
||||
return permissionDefinition.AddStateProviders( |
|
||||
new RequireGlobalFeaturesPermissionStateProvider(requiresAll, globalFeatures) |
|
||||
); |
|
||||
} |
|
||||
|
|
||||
public static PermissionDefinition RequireGlobalFeatures( |
|
||||
this PermissionDefinition permissionDefinition, |
|
||||
params Type[] globalFeatures) |
|
||||
{ |
|
||||
return permissionDefinition.RequireGlobalFeatures(true, globalFeatures); |
|
||||
} |
|
||||
|
|
||||
public static PermissionDefinition RequireGlobalFeatures( |
|
||||
[NotNull] this PermissionDefinition permissionDefinition, |
|
||||
bool requiresAll, |
|
||||
params Type[] globalFeatures) |
|
||||
{ |
|
||||
Check.NotNull(permissionDefinition, nameof(permissionDefinition)); |
|
||||
Check.NotNullOrEmpty(globalFeatures, nameof(globalFeatures)); |
|
||||
|
|
||||
return permissionDefinition.AddStateProviders( |
|
||||
new RequireGlobalFeaturesPermissionStateProvider(requiresAll, globalFeatures) |
|
||||
); |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,51 @@ |
|||||
|
using System; |
||||
|
using JetBrains.Annotations; |
||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
namespace Volo.Abp.GlobalFeatures |
||||
|
{ |
||||
|
public static class GlobalFeatureSimpleStateCheckerExtensions |
||||
|
{ |
||||
|
public static TState RequireGlobalFeatures<TState>( |
||||
|
[NotNull] this TState state, |
||||
|
params string[] globalFeatures) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
return state.RequireGlobalFeatures(true, globalFeatures); |
||||
|
} |
||||
|
|
||||
|
public static TState RequireGlobalFeatures<TState>( |
||||
|
[NotNull] this TState state, |
||||
|
bool requiresAll, |
||||
|
params string[] globalFeatures) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
Check.NotNull(state, nameof(state)); |
||||
|
Check.NotNullOrEmpty(globalFeatures, nameof(globalFeatures)); |
||||
|
|
||||
|
state.SimpleStateCheckers.Add(new RequireGlobalFeaturesSimpleStateChecker<TState>(requiresAll, globalFeatures)); |
||||
|
return state; |
||||
|
} |
||||
|
|
||||
|
public static TState RequireGlobalFeatures<TState>( |
||||
|
[NotNull] this TState state, |
||||
|
params Type[] globalFeatures) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
return state.RequireGlobalFeatures(true, globalFeatures); |
||||
|
} |
||||
|
|
||||
|
public static TState RequireGlobalFeatures<TState>( |
||||
|
[NotNull] this TState state, |
||||
|
bool requiresAll, |
||||
|
params Type[] globalFeatures) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
Check.NotNull(state, nameof(state)); |
||||
|
Check.NotNullOrEmpty(globalFeatures, nameof(globalFeatures)); |
||||
|
|
||||
|
state.SimpleStateCheckers.Add(new RequireGlobalFeaturesSimpleStateChecker<TState>(requiresAll, globalFeatures)); |
||||
|
return state; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp |
||||
|
{ |
||||
|
public class AbpTestModule : AbpModule |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,105 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Globalization; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Testing; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public abstract class SimpleStateCheckerTestBase : AbpIntegratedTest<AbpTestModule> |
||||
|
{ |
||||
|
protected readonly ISimpleStateCheckerManager<MyStateEntity> SimpleStateCheckerManager; |
||||
|
|
||||
|
public SimpleStateCheckerTestBase() |
||||
|
{ |
||||
|
SimpleStateCheckerManager = GetRequiredService<ISimpleStateCheckerManager<MyStateEntity>>(); |
||||
|
} |
||||
|
|
||||
|
public class MyStateEntity : IHasSimpleStateCheckers<MyStateEntity> |
||||
|
{ |
||||
|
public int CheckCount { get; set; } |
||||
|
|
||||
|
public int GlobalCheckCount { get; set; } |
||||
|
|
||||
|
public int MultipleCheckCount { get; set; } |
||||
|
|
||||
|
public int MultipleGlobalCheckCount { get; set; } |
||||
|
|
||||
|
public DateTime CreationTime { get; set; } |
||||
|
|
||||
|
public DateTime? LastModificationTime { get; set; } |
||||
|
|
||||
|
public List<ISimpleStateChecker<MyStateEntity>> SimpleStateCheckers { get; } |
||||
|
|
||||
|
public MyStateEntity() |
||||
|
{ |
||||
|
SimpleStateCheckers = new List<ISimpleStateChecker<MyStateEntity>>(); |
||||
|
} |
||||
|
|
||||
|
public MyStateEntity AddSimpleStateChecker(ISimpleStateChecker<MyStateEntity> checker) |
||||
|
{ |
||||
|
SimpleStateCheckers.Add(checker); |
||||
|
return this; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MySimpleStateChecker : ISimpleStateChecker<SimpleStateCheckerTestBase.MyStateEntity> |
||||
|
{ |
||||
|
public Task<bool> IsEnabledAsync(SimpleStateCheckerContext<SimpleStateCheckerTestBase.MyStateEntity> context) |
||||
|
{ |
||||
|
context.State.CheckCount += 1; |
||||
|
return Task.FromResult(context.State.CreationTime > DateTime.Parse("2020-01-01", CultureInfo.InvariantCulture)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MyGlobalSimpleStateChecker : ISimpleStateChecker<SimpleStateCheckerTestBase.MyStateEntity>, ITransientDependency |
||||
|
{ |
||||
|
public Task<bool> IsEnabledAsync(SimpleStateCheckerContext<SimpleStateCheckerTestBase.MyStateEntity> context) |
||||
|
{ |
||||
|
context.State.GlobalCheckCount += 1; |
||||
|
return Task.FromResult(context.State.LastModificationTime.HasValue); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MySimpleBatchStateChecker : SimpleBatchStateCheckerBase<SimpleStateCheckerTestBase.MyStateEntity> |
||||
|
{ |
||||
|
public override Task<SimpleStateCheckerResult<SimpleStateCheckerTestBase.MyStateEntity>> IsEnabledAsync(SimpleBatchStateCheckerContext<SimpleStateCheckerTestBase.MyStateEntity> context) |
||||
|
{ |
||||
|
foreach (var state in context.States) |
||||
|
{ |
||||
|
state.MultipleCheckCount += 1; |
||||
|
} |
||||
|
|
||||
|
var result = new SimpleStateCheckerResult<SimpleStateCheckerTestBase.MyStateEntity>(context.States); |
||||
|
|
||||
|
foreach (var x in result) |
||||
|
{ |
||||
|
result[x.Key] = x.Key.CreationTime > DateTime.Parse("2020-01-01", CultureInfo.InvariantCulture); |
||||
|
} |
||||
|
|
||||
|
return Task.FromResult(result); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MyGlobalSimpleBatchStateChecker : SimpleBatchStateCheckerBase<SimpleStateCheckerTestBase.MyStateEntity>, ITransientDependency |
||||
|
{ |
||||
|
public override Task<SimpleStateCheckerResult<SimpleStateCheckerTestBase.MyStateEntity>> IsEnabledAsync(SimpleBatchStateCheckerContext<SimpleStateCheckerTestBase.MyStateEntity> context) |
||||
|
{ |
||||
|
foreach (var state in context.States) |
||||
|
{ |
||||
|
state.MultipleGlobalCheckCount += 1; |
||||
|
} |
||||
|
|
||||
|
var result = new SimpleStateCheckerResult<SimpleStateCheckerTestBase.MyStateEntity>(context.States); |
||||
|
|
||||
|
foreach (var x in result) |
||||
|
{ |
||||
|
result[x.Key] = x.Key.LastModificationTime.HasValue; |
||||
|
} |
||||
|
|
||||
|
return Task.FromResult(result); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,81 @@ |
|||||
|
using System; |
||||
|
using System.Globalization; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public class SimpleStateChecker_CheckCount_Test : SimpleStateCheckerTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public async Task Simple_State_Check_Should_Be_Prevent_Multiple_Checks() |
||||
|
{ |
||||
|
var myStateEntities = new SimpleStateCheckerTestBase.MyStateEntity[] |
||||
|
{ |
||||
|
new SimpleStateCheckerTestBase.MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2022-01-01", CultureInfo.InvariantCulture) |
||||
|
}, |
||||
|
|
||||
|
new SimpleStateCheckerTestBase.MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2020-01-01", CultureInfo.InvariantCulture), |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
myStateEntities[0].AddSimpleStateChecker(new MySimpleBatchStateChecker()); |
||||
|
myStateEntities[1].AddSimpleStateChecker(new MySimpleStateChecker()); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntities[0])).ShouldBeTrue(); |
||||
|
|
||||
|
myStateEntities[0].CheckCount.ShouldBe(0); |
||||
|
myStateEntities[0].GlobalCheckCount.ShouldBe(0); |
||||
|
myStateEntities[0].MultipleCheckCount.ShouldBe(1); |
||||
|
myStateEntities[0].MultipleGlobalCheckCount.ShouldBe(0); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntities[1])).ShouldBeFalse(); |
||||
|
|
||||
|
myStateEntities[1].CheckCount.ShouldBe(1); |
||||
|
myStateEntities[1].GlobalCheckCount.ShouldBe(0); |
||||
|
myStateEntities[1].MultipleCheckCount.ShouldBe(0); |
||||
|
myStateEntities[1].MultipleGlobalCheckCount.ShouldBe(0); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Multiple_And_Simple_State_Check_Should_Be_Prevent_Multiple_Checks() |
||||
|
{ |
||||
|
var myStateEntities = new SimpleStateCheckerTestBase.MyStateEntity[] |
||||
|
{ |
||||
|
new SimpleStateCheckerTestBase.MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2022-01-01", CultureInfo.InvariantCulture) |
||||
|
}, |
||||
|
|
||||
|
new SimpleStateCheckerTestBase.MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2020-01-01", CultureInfo.InvariantCulture), |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
myStateEntities[0].AddSimpleStateChecker(new MySimpleBatchStateChecker()); |
||||
|
myStateEntities[1].AddSimpleStateChecker(new MySimpleStateChecker()); |
||||
|
|
||||
|
var result = await SimpleStateCheckerManager.IsEnabledAsync(myStateEntities); |
||||
|
result.Count.ShouldBe(2); |
||||
|
|
||||
|
result[myStateEntities[0]].ShouldBeTrue(); |
||||
|
result[myStateEntities[1]].ShouldBeFalse(); |
||||
|
|
||||
|
myStateEntities[0].CheckCount.ShouldBe(0); |
||||
|
myStateEntities[0].GlobalCheckCount.ShouldBe(0); |
||||
|
myStateEntities[0].MultipleCheckCount.ShouldBe(1); |
||||
|
myStateEntities[0].MultipleGlobalCheckCount.ShouldBe(0); |
||||
|
|
||||
|
myStateEntities[1].CheckCount.ShouldBe(1); |
||||
|
myStateEntities[1].GlobalCheckCount.ShouldBe(0); |
||||
|
myStateEntities[1].MultipleCheckCount.ShouldBe(0); |
||||
|
myStateEntities[1].MultipleGlobalCheckCount.ShouldBe(0); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,95 @@ |
|||||
|
using System; |
||||
|
using System.Globalization; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public class SimpleStateChecker_GlobalCheckCount_Test : SimpleStateCheckerTestBase |
||||
|
{ |
||||
|
protected override void AfterAddApplication(IServiceCollection services) |
||||
|
{ |
||||
|
services.Configure<AbpSimpleStateCheckerOptions<MyStateEntity>>(options => |
||||
|
{ |
||||
|
options.GlobalSimpleStateCheckers.Add<MyGlobalSimpleStateChecker>(); |
||||
|
options.GlobalSimpleStateCheckers.Add<MyGlobalSimpleBatchStateChecker>(); |
||||
|
}); |
||||
|
|
||||
|
base.AfterAddApplication(services); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Simple_State_Check_Should_Be_Prevent_Multiple_Checks() |
||||
|
{ |
||||
|
var myStateEntities = new SimpleStateCheckerTestBase.MyStateEntity[] |
||||
|
{ |
||||
|
new SimpleStateCheckerTestBase.MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2022-01-01", CultureInfo.InvariantCulture), |
||||
|
LastModificationTime = DateTime.Parse("2022-01-01", CultureInfo.InvariantCulture) |
||||
|
}, |
||||
|
|
||||
|
new SimpleStateCheckerTestBase.MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2020-01-01", CultureInfo.InvariantCulture) |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
myStateEntities[0].AddSimpleStateChecker(new MySimpleBatchStateChecker()); |
||||
|
myStateEntities[1].AddSimpleStateChecker(new MySimpleStateChecker()); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntities[0])).ShouldBeTrue(); |
||||
|
|
||||
|
myStateEntities[0].CheckCount.ShouldBe(0); |
||||
|
myStateEntities[0].GlobalCheckCount.ShouldBe(1); |
||||
|
myStateEntities[0].MultipleCheckCount.ShouldBe(1); |
||||
|
myStateEntities[0].MultipleGlobalCheckCount.ShouldBe(1); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntities[1])).ShouldBeFalse(); |
||||
|
|
||||
|
myStateEntities[1].CheckCount.ShouldBe(1); |
||||
|
myStateEntities[1].GlobalCheckCount.ShouldBe(0); |
||||
|
myStateEntities[1].MultipleCheckCount.ShouldBe(0); |
||||
|
myStateEntities[1].MultipleGlobalCheckCount.ShouldBe(0); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Multiple_And_Simple_State_Check_Should_Be_Prevent_Multiple_Checks() |
||||
|
{ |
||||
|
var myStateEntities = new SimpleStateCheckerTestBase.MyStateEntity[] |
||||
|
{ |
||||
|
new SimpleStateCheckerTestBase.MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2022-01-01", CultureInfo.InvariantCulture), |
||||
|
LastModificationTime = DateTime.Parse("2022-01-01", CultureInfo.InvariantCulture) |
||||
|
}, |
||||
|
|
||||
|
new SimpleStateCheckerTestBase.MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2020-01-01", CultureInfo.InvariantCulture) |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
myStateEntities[0].AddSimpleStateChecker(new MySimpleBatchStateChecker()); |
||||
|
myStateEntities[1].AddSimpleStateChecker(new MySimpleStateChecker()); |
||||
|
|
||||
|
var result = await SimpleStateCheckerManager.IsEnabledAsync(myStateEntities); |
||||
|
result.Count.ShouldBe(2); |
||||
|
|
||||
|
result[myStateEntities[0]].ShouldBeTrue(); |
||||
|
result[myStateEntities[1]].ShouldBeFalse(); |
||||
|
|
||||
|
myStateEntities[0].CheckCount.ShouldBe(0); |
||||
|
myStateEntities[0].GlobalCheckCount.ShouldBe(1); |
||||
|
myStateEntities[0].MultipleCheckCount.ShouldBe(1); |
||||
|
myStateEntities[0].MultipleGlobalCheckCount.ShouldBe(1); |
||||
|
|
||||
|
myStateEntities[1].CheckCount.ShouldBe(0); |
||||
|
myStateEntities[1].GlobalCheckCount.ShouldBe(0); |
||||
|
myStateEntities[1].MultipleCheckCount.ShouldBe(0); |
||||
|
myStateEntities[1].MultipleGlobalCheckCount.ShouldBe(1); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,105 @@ |
|||||
|
using System; |
||||
|
using System.Globalization; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking |
||||
|
{ |
||||
|
public class SimpleStateChecker_Tests : SimpleStateCheckerTestBase |
||||
|
{ |
||||
|
protected override void AfterAddApplication(IServiceCollection services) |
||||
|
{ |
||||
|
services.Configure<AbpSimpleStateCheckerOptions<MyStateEntity>>(options => |
||||
|
{ |
||||
|
options.GlobalSimpleStateCheckers.Add<MyGlobalSimpleStateChecker>(); |
||||
|
options.GlobalSimpleStateCheckers.Add<MyGlobalSimpleBatchStateChecker>(); |
||||
|
}); |
||||
|
|
||||
|
base.AfterAddApplication(services); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task State_Check_Should_Be_Works() |
||||
|
{ |
||||
|
var myStateEntity = new MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2021-01-01", CultureInfo.InvariantCulture), |
||||
|
LastModificationTime = DateTime.Parse("2021-01-01", CultureInfo.InvariantCulture) |
||||
|
}; |
||||
|
myStateEntity.AddSimpleStateChecker(new MySimpleStateChecker()); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntity)).ShouldBeTrue(); |
||||
|
|
||||
|
myStateEntity.CreationTime = DateTime.Parse("2001-01-01", CultureInfo.InvariantCulture); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntity)).ShouldBeFalse(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Global_State_Check_Should_Be_Works() |
||||
|
{ |
||||
|
var myStateEntity = new MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2021-01-01", CultureInfo.InvariantCulture) |
||||
|
}; |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntity)).ShouldBeFalse(); |
||||
|
|
||||
|
myStateEntity.LastModificationTime = DateTime.Parse("2001-01-01", CultureInfo.InvariantCulture); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntity)).ShouldBeTrue(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Multiple_State_Check_Should_Be_Works() |
||||
|
{ |
||||
|
var checker = new MySimpleBatchStateChecker(); |
||||
|
|
||||
|
var myStateEntities = new MyStateEntity[] |
||||
|
{ |
||||
|
new MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2021-01-01", CultureInfo.InvariantCulture), |
||||
|
LastModificationTime = DateTime.Parse("2021-01-01", CultureInfo.InvariantCulture) |
||||
|
}, |
||||
|
|
||||
|
new MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2021-01-01", CultureInfo.InvariantCulture), |
||||
|
LastModificationTime = DateTime.Parse("2021-01-01", CultureInfo.InvariantCulture) |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
foreach (var myStateEntity in myStateEntities) |
||||
|
{ |
||||
|
myStateEntity.AddSimpleStateChecker(checker); |
||||
|
} |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntities)).ShouldAllBe(x => x.Value); |
||||
|
|
||||
|
foreach (var myStateEntity in myStateEntities) |
||||
|
{ |
||||
|
myStateEntity.CreationTime = DateTime.Parse("2001-01-01", CultureInfo.InvariantCulture); |
||||
|
} |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntities)).ShouldAllBe(x => !x.Value); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Multiple_Global_State_Check_Should_Be_Works() |
||||
|
{ |
||||
|
var myStateEntity = new MyStateEntity() |
||||
|
{ |
||||
|
CreationTime = DateTime.Parse("2021-01-01", CultureInfo.InvariantCulture) |
||||
|
}; |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntity)).ShouldBeFalse(); |
||||
|
|
||||
|
myStateEntity.LastModificationTime = DateTime.Parse("2001-01-01", CultureInfo.InvariantCulture); |
||||
|
|
||||
|
(await SimpleStateCheckerManager.IsEnabledAsync(myStateEntity)).ShouldBeTrue(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue