/// <summary>
 /// Envia un correo para confirmar que se cambiara la conttraseña
 /// </summary>
 /// <param name="recoverDto"></param>
 public void RecoverPassword(RecoverPassDto recoverDto)
 {
     try
     {
         if (recoverDto.Email is null || recoverDto.Username is null)
         {
             throw new Exception("El objeto esta vacio");
         }
         Users user = this.userRepository.FindUserByUserNameEmail(recoverDto);
         if (user is null)
         {
             throw new BussinessException(HttpStatusCode.Forbidden, "Error al ingresar las credenciales");
         }
         if (!user.IsConfirmed)
         {
             throw new BussinessException(HttpStatusCode.Forbidden, "El email no ha sido confirmado");
         }
         string[] apiAuth = new string[]
         {
             configuration["ApiAuth:Issuer"],
             configuration["ApiAuth:Audience"],
             configuration["ApiAuth:SecretKey"]
         };
         UserDto userDto = mapper.Map <UserDto>(user);
         user.TokenChangePassword = new JwtSecurityTokenHandler()
                                    .WriteToken(Security.GenerateToken(userDto, apiAuth, TokenExpiresEnum.confirmation));
         int userUpdated = this.userRepository.UpdateUser(user);
         if (userUpdated != 0)
         {
             this.SendSecuriyEmail(user, EmailBodyEnum.recoverPassEmail, ActionProcessUserEnum.recoverPass);
         }
     }
     catch (BussinessException) { throw; }
     catch (Exception) { throw; }
 }
        public async Task <ResultObject> ConfirmRecoverPass(RecoverPassDto recoverPassDto)
        {
            ResultObject result = new ResultObject();
            var          user   = await _userService.FindUserByNationalCode(recoverPassDto.NationalCode);

            if (user == null)
            {
                result.ServerErrors.Add(new ServerErr()
                {
                    Hint = "کد ملی وارد شده در سامانه موجود نمی باشد.", Type = ConstErrorTypes.BussinessError
                });
                return(result);
            }

            if (user.ConfirmCode != recoverPassDto.ConfirmCode)
            {
                result.ServerErrors.Add(new ServerErr()
                {
                    Hint = "کد فعالسازی معتبر نمی باشد.", Type = ConstErrorTypes.BussinessError
                });
                return(result);
            }
            user.Password = GetHassPassCode(recoverPassDto.Password);
            await _userService.UpdateUserAsync(user);

            result.Result = _transactionManager.SaveAllAsync();
            return(result);
        }
        /// <summary>
        /// Cambia las contraseña de un usuario previamente el email haya sido confirmado
        /// </summary>
        /// <param name="changePasswordDto"></param>
        public void ChangePassword(ChangePasswordDto changePasswordDto)
        {
            try
            {
                if (changePasswordDto is null)
                {
                    throw new Exception("El objeto esta vacio");
                }

                RecoverPassDto dto  = mapper.Map <RecoverPassDto>(changePasswordDto);
                Users          user = this.userRepository.FindUserByUserNameEmail(dto);
                if (user is null)
                {
                    throw new BussinessException(HttpStatusCode.Forbidden, "El usuario no existe");
                }

                string[] apiAuth = new string[]
                {
                    configuration["ApiAuth:Issuer"],
                    configuration["ApiAuth:Audience"],
                    configuration["ApiAuth:SecretKey"]
                };
                if (!user.IsConfirmed || !user.IsConfirmedChange)
                {
                    throw new BussinessException(HttpStatusCode.Forbidden, "Tiene que confirmar la cuenta primero");
                }

                if (!Security.ValidateCurrentToken(user.TokenChangePassword, apiAuth))
                {
                    throw new BussinessException(HttpStatusCode.Forbidden, "El token expiró");
                }

                if (!changePasswordDto.ConfirmPassword.Equals(changePasswordDto.Password))
                {
                    throw new BussinessException("Las contraseñas no coinciden, favor verificarlas");
                }

                if (this.ValidatePassword(changePasswordDto.Password))
                {
                    throw new BussinessException("La contraseña no es lo suficientemente fuerte");
                }

                user.IsConfirmedChange   = false;
                user.Password            = Security.Encrypt(changePasswordDto.Password, configuration["ApiAuth:ClaveIV"]);
                user.TokenChangePassword = null;
                user.UpdateDate          = DateTime.Now;
                int userUpdated = this.userRepository.UpdateUser(user);
                if (userUpdated == 0)
                {
                    throw new BussinessException($"Error al confirmar el email");
                }
            }
            catch (BussinessException) { throw; }
            catch (Exception) { throw; }
        }
 public IActionResult RecoverPassword(RecoverPassDto recoverDto)
 {
     try
     {
         userConfiguration.RecoverPassword(recoverDto);
         return(Ok(new
         {
             message = "Se ha enviado un mensaje de verificacion de identidad"
         }));
     }
     catch (BussinessException e)
     {
         return(Problem(e.ReasonPhrase ?? e.Message, null, (int?)e.StatusCode, null, null));
     }
     catch (Exception) { throw; }
 }
        public async Task <ResultObject> ReCoverPassAsync(RecoverPassDto recoverPassDto)
        {
            ResultObject result = new ResultObject();
            var          user   = await _userService.FindUserByNationalCode(recoverPassDto.NationalCode);

            if (user == null)
            {
                result.ServerErrors.Add(new ServerErr()
                {
                    Hint = "کد ملی وارد شده در سامانه موجود نمی باشد.", Type = ConstErrorTypes.BussinessError
                });
                return(result);
            }

            var code = GenerateRandomConfirmCode();

            user.ConfirmCode = code;
            await _userService.UpdateUserAsync(user);

            result.Result = _transactionManager.SaveAllAsync();
            return(result);
        }
 public async Task <ResultObject> ConfirmRecoverPass([FromBody] RecoverPassDto recoverPassDto)
 {
     return(await userAppService.ConfirmRecoverPass(recoverPassDto));
 }
 public async Task <ResultObject> ReCoverPassAsync([FromBody] RecoverPassDto recoverPassDto)
 {
     return(await userAppService.ReCoverPassAsync(recoverPassDto));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Encuentra el usuario por email o el nombre de usuario
 /// </summary>
 /// <param name="recoverDto"></param>
 /// <returns></returns>
 public Users FindUserByUserNameEmail(RecoverPassDto recoverDto)
 {
     return(this.repository.GetOneData <Users>(
                g => g.Email.Equals(recoverDto.Email) ||
                g.UserName.Equals(recoverDto.Username)));
 }