コード例 #1
0
    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);
    }
コード例 #2
0
    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.StateCheckers)
                                     .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.StateCheckers.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.GlobalStateCheckers
                     .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);
        }
    }
コード例 #3
0
 public abstract Task <SimpleStateCheckerResult <TState> > IsEnabledAsync(SimpleBatchStateCheckerContext <TState> context);
コード例 #4
0
        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));
        }
コード例 #5
0
        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));
        }