/// <summary>
        ///     Controller para cambiar la clave del usuario
        /// </summary>
        /// <param name="loginDTO">Objeto login a cambiar su clave</param>
        /// <returns>Objeto tipo Entity con los datos del usuario luego de cambiar su clave</returns>
        /// <exception cref="PasswordRecoveryException">El objeto a retornar es nulo</exception>
        /// <exception cref="DatabaseException">Algun error con la base de datos</exception>

        //POST : /api/Email
        public ActionResult <LoginDTO> Recovery([FromBody] LoginDTO loginDTO)
        {
            try{
                LoginMapper             LoginMapper = MapperFactory.createLoginMapper();
                Entity                  entity      = LoginMapper.CreateEntity(loginDTO);
                RecoveryPasswordCommand command     = CommandFactory.RecoveryPasswordCommand((Login)entity);
                command.Execute();

                Login objUser = command.GetResult();

                if (objUser != null)
                {
                    Console.WriteLine("Correo del usuario que modifico: ");
                    Console.WriteLine(objUser.email);
                    Console.WriteLine("Clave del usuario modificada: ");
                    Console.WriteLine(objUser.password);

                    //logica correo
                    var message = new MimeMessage();
                    //From Address
                    message.From.Add(new MailboxAddress("Vacanze Administracion", "*****@*****.**"));
                    //To Address
                    message.To.Add(new MailboxAddress("Usuario", address: objUser.email));
                    //Subject
                    message.Subject = "Recuperacion De Contraseña : ";

                    message.Body = new TextPart("plain")
                    {
                        Text = "Su contraseña nueva: " + objUser.password
                    };


                    using (var client = new MailKit.Net.Smtp.SmtpClient()){
                        client.CheckCertificateRevocation = false;
                        client.Connect("smtp.gmail.com", 587);
                        client.Authenticate("*****@*****.**", "_Gx123456");
                        client.Send(message);
                        client.Disconnect(true);
                        client.Dispose();
                    }
                    LoginDTO ldto = LoginMapper.CreateDTO(objUser);
                    return(Ok(ldto));
                }
                else
                {
                    return(BadRequest(new { message = "Correo invalido." }));
                }
            }
            catch (DatabaseException ex) {
                _logger?.LogError(ex, "Database exception cuando se intenta mandar el correo con la nueva clave al cliente");
                return(StatusCode(500, ex.Message));
            }
            catch (PasswordRecoveryException) {
                return(BadRequest(new { message = "Correo invalido." }));
            }
        }
        public async Task Post_email_no_existe_BadRequest()
        {
            // Arrange
            var data           = new RecoveryPasswordCommand("*****@*****.**");
            var requestContent = SerializerUtils.GetRequestContent(data);

            // Act
            var response = await Client.PostAsync(BaseUrl, requestContent);

            // Assert
            response.StatusCode.ShouldBe(HttpStatusCode.NotFound);
        }
        public async Task Post_email_invalido_BadRequest(string email)
        {
            // Arrange
            var data           = new RecoveryPasswordCommand(email);
            var requestContent = SerializerUtils.GetRequestContent(data);

            // Act
            var response = await Client.PostAsync(BaseUrl, requestContent);

            // Assert
            response.StatusCode.ShouldBe(HttpStatusCode.BadRequest);
        }
        public async Task Post_recuperar_contrasena_valida_Created()
        {
            // Arrange
            var data           = new RecoveryPasswordCommand("*****@*****.**");
            var requestContent = SerializerUtils.GetRequestContent(data);

            // Act
            var response = await Client.PostAsync(BaseUrl, requestContent);

            // Assert
            response.EnsureSuccessStatusCode();
        }
        public ActionResult RecoveryPassword(RecoveryPasswordCommand command)
        {
            string message; User user;

            if ((user = _repository.GetUserByEmail(command.user_email)) != null)
            {
                user.RecoveryCode = _profileCondition.random.Next(100000, 999999);
                _repository.UpdateUser(user);
                _mailer.SendEmail(user.Email, "Recovery password", "Recovery code =" + user.RecoveryCode);
                _logger.LogInformation($"Recovery password, id -> {user.Id}.");
                return(Ok(new MessageResponse(true, $"Recovery password. Send message with code to email -> {user.Email}.")));
            }
            else
            {
                message = $"User with email -> {command.user_email} doesn't exist.";
            }
            _logger.LogWarning(message);
            var response = new MessageResponse(false, message);

            return(StatusCode(500, response));
        }
Beispiel #6
0
        public async Task <ActionResult> RecoveryPassword(RecoveryPasswordCommand recoveryPasswordCommand)
        {
            await Mediator.Send(recoveryPasswordCommand);

            return(StatusCode(StatusCodes.Status201Created));
        }