private void SetRolesRule()
        {
            if (HttpMethod == HttpMethod.Post || RequestJsonDictionary.ContainsKey("role_ids"))
            {
                IList <CustomerRole> customerRoles = null;

                // async validation: https://docs.fluentvalidation.net/en/latest/async.html

                RuleFor(x => x.RoleIds)
                .NotNull()
                .Must(roles => roles.Count > 0)
                .WithMessage("role_ids required")
                .DependentRules(() => RuleFor(dto => dto.RoleIds)
                                .MustAsync(async(roleIds, cancellation) =>
                {
                    if (customerRoles == null)
                    {
                        customerRoles = await _customerRolesHelper.GetValidCustomerRolesAsync(roleIds);
                    }

                    var isInGuestAndRegisterRoles = _customerRolesHelper.IsInGuestsRole(customerRoles) &&
                                                    _customerRolesHelper.IsInRegisteredRole(customerRoles);

                    // Customer can not be in guest and register roles simultaneously
                    return(!isInGuestAndRegisterRoles);
                })
                                .WithMessage("must not be in guest and register roles simultaneously")
                                .DependentRules(() => RuleFor(dto => dto.RoleIds)
                                                .MustAsync(async(roleIds, cancellation) =>
                {
                    if (customerRoles == null)
                    {
                        customerRoles = await _customerRolesHelper.GetValidCustomerRolesAsync(roleIds);
                    }

                    var isInGuestOrRegisterRoles = _customerRolesHelper.IsInGuestsRole(customerRoles) ||
                                                   _customerRolesHelper.IsInRegisteredRole(customerRoles);

                    // Customer must be in either guest or register role.
                    return(isInGuestOrRegisterRoles);
                })
                                                .WithMessage("must be in guest or register role")
                                                )
                                );
            }
        }
示例#2
0
        private void SetRuleForRoles()
        {
            IList <CustomerRole> customerRoles = null;

            RuleFor <List <int> >(x => x.RoleIds)
            .NotNull()
            .Must(roles => roles.Count > 0)
            .WithMessage("role_ids required")
            .DependentRules(dependentRules => dependentRules.RuleFor(dto => dto.RoleIds)
                            .Must(roleIds =>
            {
                if (customerRoles == null)
                {
                    customerRoles = _customerRolesHelper.GetValidCustomerRoles(roleIds);
                }

                bool isInGuestAndRegisterRoles = _customerRolesHelper.IsInGuestsRole(customerRoles) &&
                                                 _customerRolesHelper.IsInRegisteredRole(customerRoles);

                // Customer can not be in guest and register roles simultaneously
                return(!isInGuestAndRegisterRoles);
            })
                            .WithMessage("must not be in guest and register roles simultaneously")
                            .DependentRules(dependentRule => dependentRules.RuleFor(dto => dto.RoleIds)
                                            .Must(roleIds =>
            {
                if (customerRoles == null)
                {
                    customerRoles = _customerRolesHelper.GetValidCustomerRoles(roleIds);
                }

                bool isInGuestOrRegisterRoles = _customerRolesHelper.IsInGuestsRole(customerRoles) ||
                                                _customerRolesHelper.IsInRegisteredRole(customerRoles);

                // Customer must be in either guest or register role.
                return(isInGuestOrRegisterRoles);
            })
                                            .WithMessage("must be in guest or register role")
                                            )
                            );
        }