Пример #1
0
        public async Task <IActionResult> UpdateAsync(UserViewModel <TUser> userModel)
        {
            var userSpecification = new UserSpecification()
            {
                ID    = userModel.User.ID,
                Email = userModel.User.Email
            };

            var savedUser = await _userRepository.ReadAsync(userSpecification);

            var roles = await _userRoleRepository.ListAsync();

            foreach (var userRole in userModel.User.Roles)
            {
                var role = roles.Where(r => r.ID == userRole.ID).FirstOrDefault();
                userRole.Name         = role.Name;
                userRole.DisplayTitle = role.DisplayTitle;
            }

            var email = userModel.User.Email;

            if (email.HasValue())
            {
                if (!_emailValidatorService.IsValidEmail(email))
                {
                    ModelState.AddModelError("InvalidEmail", _localizer[UserErrorMessages.InvalidEmail]);
                }

                if (email != savedUser.Email)
                {
                    var duplicateUser = await _userRepository.ReadAsync(userSpecification);

                    if (duplicateUser != null)
                    {
                        ModelState.AddModelError("DuplicateUser", _localizer[UserErrorMessages.DuplicateUser]);
                    }
                }
            }

            if (userModel.HasIdentity(UserID) && userModel.Password.HasValue() ||
                userModel.HasIdentity(UserID) && userModel.ConfirmationPassword.HasValue())
            {
                if (userModel.Password.IsNullOrWhiteSpace() || userModel.ConfirmationPassword.IsNullOrWhiteSpace())
                {
                    ModelState.AddModelError("Invalid", _localizer[UserErrorMessages.TwoPasswordsRequired]);
                    return(PartialView("_Editor", userModel));
                }

                if (userModel.Password != userModel.ConfirmationPassword)
                {
                    ModelState.AddModelError("Invalid", _localizer[UserErrorMessages.PasswordsDoNotMatch]);
                    return(PartialView("_Editor", userModel));
                }

                var passwordValidationErrors = _passwordValidatorService.ValidatePassword(userModel.Password);

                if (passwordValidationErrors.Count > 0)
                {
                    ModelState.AddModelError(
                        key: "PasswordValidationErrors",
                        errorMessage: _localizer[_passwordValidatorService.GetPasswordValidationErrorMessage()]
                        );
                    return(PartialView("_Editor", userModel));
                }

                var passHash = _passwordHasherService.HashPassword(userModel.User, userModel.Password);
                userModel.User.PassHash = passHash;
            }
            else
            {
                userModel.User.PassHash = savedUser.PassHash;
            }

            if (!ModelState.IsValid)
            {
                return(PartialView("_Editor", userModel));
            }

            var update = await _userRepository.UpdateAsync(userModel.User, UserID);

            if (update <= 0)
            {
                return(BadRequest());
            }

            var updatedUser = await _userRepository.ReadAsync(userSpecification);

            var userDTO          = new UserDTO(updatedUser);
            var userUpdatedEvent = new UserUpdated(userDTO);
            await _mediator.Publish(userUpdatedEvent);

            var result = new UserViewModel <TUser>()
            {
                ID   = userModel.ID,
                User = updatedUser
            };

            return(PartialView("_Editor", result));
        }
Пример #2
0
        public async Task <IActionResult> SetPassword(SetPasswordViewModel data)
        {
            ViewData["Email"] = data.Email;

            if (!ModelState.IsValid)
            {
                return(PartialView(data));
            }

            if (!_emailValidatorService.IsValidEmail(data.Email))
            {
                ModelState.AddModelError("InvalidEmail", _localizer[AuthenticationErrorMessages.InvalidEmail]);
                return(PartialView(data));
            }

            if (data.NewPassword.HasValue() && data.ConfirmNewPassword.HasValue() &&
                data.NewPassword != data.ConfirmNewPassword)
            {
                ModelState.AddModelError("PasswordsDoNotMatch", _localizer[AuthenticationErrorMessages.PasswordsDoNotMatch]);
                return(PartialView(data));
            }

            var tokenResult = await _userPasswordRepository.ValidatePasswordTokenAsync(data.Email, data.Token);

            if (tokenResult == TokenVerificationResult.Invalid)
            {
                ModelState.AddModelError("InvalidToken", _localizer[AuthenticationErrorMessages.InvalidToken]);
                return(PartialView(data));
            }

            if (tokenResult == TokenVerificationResult.Expired)
            {
                ModelState.AddModelError("ExpiredToken", _localizer[AuthenticationErrorMessages.ExpiredToken]);
                return(PartialView(data));
            }

            var userSpecification = new UserSpecification()
            {
                Email = data.Email
            };

            AuthenticationUser = await _userRepository.ReadAsync(userSpecification);

            if (AuthenticationUser is null)
            {
                ModelState.AddModelError("UserDoesNotExist", _localizer[AuthenticationErrorMessages.UserDoesNotExist]);
                return(PartialView(data));
            }

            var passwordValidationErrors = _passwordValidatorService.ValidatePassword(data.NewPassword);

            if (passwordValidationErrors.Count > 0)
            {
                ModelState.AddModelError(
                    key: "PasswordValidationErrors",
                    errorMessage: _localizer[_passwordValidatorService.GetPasswordValidationErrorMessage()]
                    );
                return(PartialView(data));
            }

            string hashedPassword = _passwordHasherService.HashPassword(AuthenticationUser, data.NewPassword);
            var    userDTO        = new UserDTO(AuthenticationUser);
            var    setPassword    = await _userPasswordRepository.SetPasswordAsync(userDTO, hashedPassword);

            if (!setPassword)
            {
                ModelState.AddModelError("PasswordSetFailed", _localizer[AuthenticationErrorMessages.PasswordSetFailed]);
                return(PartialView());
            }

            data.SetPasswordSuccess = true;

            return(PartialView(data));
        }