コード例 #1
0
        public async Task Any(ModifyValidationRules request)
        {
            var appHost = HostContext.AssertAppHost();
            var feature = appHost.AssertPlugin <ValidationFeature>();
            await RequestUtils.AssertAccessRoleAsync(base.Request, accessRole : feature.AccessRole, authSecret : request.AuthSecret);

            var utcNow   = DateTime.UtcNow;
            var userName = (await base.GetSessionAsync()).GetUserAuthName();
            var rules    = request.SaveRules;

            if (!rules.IsEmpty())
            {
                foreach (var rule in rules)
                {
                    if (rule.Type == null)
                    {
                        throw new ArgumentNullException(nameof(rule.Type));
                    }

                    var existingType = appHost.Metadata.FindDtoType(rule.Type);
                    if (existingType == null)
                    {
                        throw new ArgumentException(@$ "{rule.Type} does not exist", nameof(rule.Type));
                    }

                    if (rule.Validator == "")
                    {
                        rule.Validator = null;
                    }
                    if (rule.Condition == "")
                    {
                        rule.Condition = null;
                    }
                    if (rule.Field == "")
                    {
                        rule.Field = null;
                    }
                    if (rule.ErrorCode == "")
                    {
                        rule.ErrorCode = null;
                    }
                    if (rule.Message == "")
                    {
                        rule.Message = null;
                    }
                    if (rule.Notes == "")
                    {
                        rule.Notes = null;
                    }

                    if (rule.Field != null && TypeProperties.Get(existingType).GetAccessor(rule.Field) == null)
                    {
                        throw new ArgumentException(@$ "{rule.Field} does not exist on {rule.Type}", nameof(rule.Field));
                    }

                    if (rule.Validator != null)
                    {
                        object validator;
                        try
                        {
                            validator = appHost.EvalExpression(rule.Validator);
                            if (validator == null)
                            {
                                throw new ArgumentException(@$ "Validator does not exist", nameof(rule.Validator));
                            }
                        }
                        catch (Exception e)
                        {
                            throw new ArgumentException(@$ "Invalid Validator: " + e.Message, nameof(rule.Validator));
                        }

                        var validators     = (validator as List <object>) ?? TypeConstants.EmptyObjectList;
                        var firstValidator = validator is IPropertyValidator pv
                            ? pv
                            : validator is ITypeValidator tv
                            ? tv
                            : validators?.FirstOrDefault() ?? validator;

                        if (rule.Field != null && !(firstValidator is IPropertyValidator && validators.All(v => v is IPropertyValidator)))
                        {
                            throw new ArgumentException(@$ "{nameof(IPropertyValidator)} is expected but was {(validators?.FirstOrDefault(v => !(v is IPropertyValidator)) ?? firstValidator).GetType().Name}", nameof(rule.Validator));
                        }

                        if (rule.Field == null && !(firstValidator is ITypeValidator && validators.All(v => v is ITypeValidator)))
                        {
                            throw new ArgumentException(@$ "{nameof(ITypeValidator)} is expected but was {(validators?.FirstOrDefault(v => !(v is IPropertyValidator)) ?? firstValidator).GetType().Name}", nameof(rule.Validator));
                        }

                        if (rule.Condition != null)
                        {
                            throw new ArgumentException(@$ "Only {nameof(rule.Validator)} or {nameof(rule.Condition)} can be specified, not both", nameof(rule.Condition));
                        }
                    }
                    else
                    {
                        if (rule.Condition == null)
                        {
                            throw new ArgumentNullException(nameof(rule.Validator), @$ "{nameof(rule.Validator)} or {nameof(rule.Condition)} is required");
                        }

                        try
                        {
                            var ast = Validators.ParseCondition(appHost.ScriptContext, rule.Condition);
                            await ast.Init().ConfigAwait();
                        }
                        catch (Exception e)
                        {
                            var useEx = e is ScriptException se ? se.InnerException ?? e : e;
                            throw new ArgumentException(useEx.Message, nameof(rule.Condition));
                        }
                    }

                    if (rule.CreatedBy == null)
                    {
                        rule.CreatedBy   = userName;
                        rule.CreatedDate = utcNow;
                    }
                    rule.ModifiedBy   = userName;
                    rule.ModifiedDate = utcNow;
                }

                await ValidationSource.SaveValidationRulesAsync(rules).ConfigAwait();
            }

            if (!request.SuspendRuleIds.IsEmpty())
            {
                var suspendRules = await ValidationSource.GetValidateRulesByIdsAsync(request.SuspendRuleIds).ConfigAwait();

                foreach (var suspendRule in suspendRules)
                {
                    suspendRule.SuspendedBy   = userName;
                    suspendRule.SuspendedDate = utcNow;
                }

                await ValidationSource.SaveValidationRulesAsync(suspendRules).ConfigAwait();
            }

            if (!request.UnsuspendRuleIds.IsEmpty())
            {
                var unsuspendRules = await ValidationSource.GetValidateRulesByIdsAsync(request.UnsuspendRuleIds).ConfigAwait();

                foreach (var unsuspendRule in unsuspendRules)
                {
                    unsuspendRule.SuspendedBy   = null;
                    unsuspendRule.SuspendedDate = null;
                }

                await ValidationSource.SaveValidationRulesAsync(unsuspendRules).ConfigAwait();
            }

            if (!request.DeleteRuleIds.IsEmpty())
            {
                await ValidationSource.DeleteValidationRulesAsync(request.DeleteRuleIds.ToArray()).ConfigAwait();
            }

            if (request.ClearCache.GetValueOrDefault())
            {
                await ValidationSource.ClearCacheAsync().ConfigAwait();
            }
        }
コード例 #2
0
        public async Task Any(ModifyValidationRules request)
        {
            var appHost   = HostContext.AssertAppHost();
            var container = appHost.GetContainer();
            var feature   = appHost.AssertPlugin <ValidationFeature>();

            RequestUtils.AssertAccessRole(base.Request, accessRole: feature.AccessRole, authSecret: request.AuthSecret);

            var utcNow   = DateTime.UtcNow;
            var userName = base.GetSession().GetUserAuthName();
            var rules    = request.SaveRules;

            if (!rules.IsEmpty())
            {
                foreach (var rule in rules)
                {
                    if (rule.Type == null)
                    {
                        throw new ArgumentNullException(nameof(rule.Type));
                    }

                    if (rule.CreatedBy == null)
                    {
                        rule.CreatedBy   = userName;
                        rule.CreatedDate = utcNow;
                    }
                    rule.ModifiedBy   = userName;
                    rule.ModifiedDate = utcNow;
                }

                await ValidationSource.SaveValidationRulesAsync(rules);
            }

            if (!request.SuspendRuleIds.IsEmpty())
            {
                var suspendRules = await ValidationSource.GetValidateRulesByIdsAsync(request.SuspendRuleIds);

                foreach (var suspendRule in suspendRules)
                {
                    suspendRule.SuspendedBy   = userName;
                    suspendRule.SuspendedDate = utcNow;
                }

                await ValidationSource.SaveValidationRulesAsync(suspendRules);
            }

            if (!request.UnsuspendRuleIds.IsEmpty())
            {
                var unsuspendRules = await ValidationSource.GetValidateRulesByIdsAsync(request.UnsuspendRuleIds);

                foreach (var unsuspendRule in unsuspendRules)
                {
                    unsuspendRule.SuspendedBy   = null;
                    unsuspendRule.SuspendedDate = null;
                }

                await ValidationSource.SaveValidationRulesAsync(unsuspendRules);
            }

            if (!request.DeleteRuleIds.IsEmpty())
            {
                await ValidationSource.DeleteValidationRulesAsync(request.DeleteRuleIds.ToArray());
            }
        }