示例#1
0
        public ActionResult ChangePassword([FromBody] UserChangePasswordDTO userDTO)
        {
            if (ModelState.IsValid)
            {
                var user = _userRepository.Get(userDTO.Email);

                if (user == null)
                {
                    return(NotFound("Usuário não encontrado."));
                }

                if (!_userRepository.CheckPassword(user, userDTO.PasswordCurrent))
                {
                    return(NotFound("Senha atual inválida."));
                }

                user.PasswordHash = userDTO.PasswordNew;

                var result = _userRepository.ChangePassword(user, userDTO.PasswordNew);

                if (!result.Succeeded)
                {
                    List <string> errors = new List <string>();

                    foreach (var error in result.Errors)
                    {
                        errors.Add(error.Description);
                    }

                    return(UnprocessableEntity(errors));
                }
                else
                {
                    return(Ok("Senha alterada com sucesso."));
                }
            }
            else
            {
                return(UnprocessableEntity(ModelState));
            }
        }