Example #1
0
        /// <summary>
        /// Validates that the email is not already registered with another user.
        /// </summary>
        protected virtual async Task <ValidationError> ValidateUniqueAsync(IEmailAddressValidationContext context)
        {
            var userArea = _userAreaDefinitionRepository.GetRequiredByCode(context.UserAreaCode);
            var options  = _userAreaDefinitionRepository.GetOptionsByCode(context.UserAreaCode).EmailAddress;

            if (!options.RequireUnique && !userArea.UseEmailAsUsername)
            {
                return(null);
            }

            var query = new IsUserEmailAddressUniqueQuery()
            {
                Email        = context.Email.UniqueEmailAddress,
                UserAreaCode = context.UserAreaCode
            };

            if (context.UserId.HasValue)
            {
                query.UserId = context.UserId.Value;
            }

            var isUnique = await _domainRepository
                           .WithContext(context.ExecutionContext)
                           .ExecuteQueryAsync(query);

            if (isUnique)
            {
                return(null);
            }

            return(UserValidationErrors
                   .EmailAddress
                   .NotUnique
                   .Create(context.PropertyName));
        }
Example #2
0
        /// <summary>
        /// Validates that the <see cref="IEmailAddressValidationContext.Email"/>
        /// property is not <see langword="null"/>, which indicates that the formatting
        /// operation did not return a value. This can happen if the normalizer was supplied
        /// a value but could not parse it successfully.
        /// </summary>
        protected virtual ValidationError ValidateEmailFormattingResult(IEmailAddressValidationContext context)
        {
            if (context.Email != null)
            {
                return(null);
            }

            return(UserValidationErrors
                   .EmailAddress
                   .InvalidFormat
                   .Create(context.PropertyName));
        }
Example #3
0
        /// <summary>
        /// Validates that the email is not shorter then the limit defined in the
        /// <see cref="EmailAddressOptions"/> configuration settings.
        /// </summary>
        protected virtual ValidationError ValidateMinLength(IEmailAddressValidationContext context)
        {
            var options = _userAreaDefinitionRepository.GetOptionsByCode(context.UserAreaCode).EmailAddress;

            if (context.Email.NormalizedEmailAddress.Length >= options.MinLength)
            {
                return(null);
            }

            return(UserValidationErrors
                   .EmailAddress
                   .MinLengthNotMet
                   .Customize()
                   .WithMessageFormatParameters(options.MinLength)
                   .WithProperties(context.PropertyName)
                   .Create());
        }
Example #4
0
        /// <summary>
        /// Validates that the email contains only the characters permitted by the
        /// <see cref="EmailAddressOptions"/> configuration settings.
        /// </summary>
        protected virtual ValidationError ValidateAllowedCharacters(IEmailAddressValidationContext context)
        {
            var options           = _userAreaDefinitionRepository.GetOptionsByCode(context.UserAreaCode).EmailAddress;
            var invalidCharacters = EmailAddressCharacterValidator.GetInvalidCharacters(context.Email.NormalizedEmailAddress, options);

            if (!invalidCharacters.Any())
            {
                return(null);
            }

            // Be careful here, because we're handling user input. Any message should be escaped when
            // rendered, but to be safe we'll only include a single invalid character
            return(UserValidationErrors
                   .EmailAddress
                   .InvalidCharacters
                   .Customize()
                   .WithMessageFormatParameters(invalidCharacters.First().ToString())
                   .WithProperties(context.PropertyName)
                   .Create());
        }
Example #5
0
        public virtual async Task <ICollection <ValidationError> > GetErrorsAsync(IEmailAddressValidationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var validators = new List <Func <IEmailAddressValidationContext, ValidationError> >()
            {
                ValidateEmailFormattingResult,
                ValidateMinLength,
                ValidateMaxLength,
                ValidateAllowedCharacters,
            };

            var asyncValidators = new List <Func <IEmailAddressValidationContext, Task <ValidationError> > >()
            {
                ValidateUniqueAsync
            };

            foreach (var validator in validators)
            {
                var error = validator(context);
                if (error != null)
                {
                    return(WrapError(error));
                }
            }

            foreach (var asyncValidator in asyncValidators)
            {
                var error = await asyncValidator(context);

                if (error != null)
                {
                    return(WrapError(error));
                }
            }

            return(new List <ValidationError>());
        }
        /// <summary>
        /// Validates a user email address, throwing a <see cref="ValidationErrorException"/>
        /// if any errors are found. By default the validator checks that the format contains only
        /// the characters permitted by the <see cref="EmailAddressOptions"/> configuration settings,
        /// as well as checking for uniquness.
        /// </summary>
        public static async Task ValidateAsync(this IEmailAddressValidator emailAddressValidator, IEmailAddressValidationContext context)
        {
            var result = await emailAddressValidator.GetErrorsAsync(context);

            if (result.Any())
            {
                throw new ValidationErrorException(result.First());
            }
        }