Exemplo n.º 1
0
        public CommandResult UpdatePassword(UserUpdatePasswordCommand command, string currentUser)
        {
            var user = _repository.GetByNameOrEmail(command.Username);

            if (user == null)
            {
                return(new CommandResult(false, "Login inválido! "));
            }

            var salt_tabela = user.Salt;

            byte[] salt         = Convert.FromBase64String(salt_tabela);
            var    hashPassword = HashGenerator.Generate(command.Password, salt);

            if (user.Password != hashPassword)
            {
                return(new CommandResult(false, "Senha antiga não confere. "));
            }

            // add new hash and salt
            hashPassword = HashGenerator.Generate(command.NewPassword, salt);

            user.UpdatePassword(command.NewPassword);

            if (user.Invalid)
            {
                return(new CommandResult(false, string.Join(". ", Notifications)));
            }

            _repository.UpdatePassword(user.Username, hashPassword);

            user.HidePassword();

            return(new CommandResult(true, "Senha alterada com sucesso! ", user));
        }
Exemplo n.º 2
0
        public IActionResult UpdatePassword(UserUpdatePasswordCommand command)
        {
            var update = _userService.UpdatePassword(command);

            if (update.IsError)
            {
                return(BadRequest(update));
            }
            return(Ok(update));
        }
        public async Task UpdatePasswordAsync(UserUpdatePasswordCommand command)
        {
            await ValidatorFactory.GetValidator <UserUpdatePasswordCommand>().ValidateAndThrowAsync(command);

            var user = await _readRepository.GetSingleAsync(_userFiltersProvider.ById(command.UserId));

            user.PasswordHash = command.NewPasswordHash;

            await WriteRepository.UpdateAsync(user);

            await UnitOfWork.CommitAsync();
        }
        public void UserAuthHandler_UpdatePassword_Valid()
        {
            var repository    = new FakeUserAuthRepository();
            var logRepository = new FakeAccessLogRepository();
            var handler       = new UserAuthHandler(repository, logRepository);
            var command       = new UserUpdatePasswordCommand();

            command.Username    = repository.GetAll().FirstOrDefault().Username;
            command.Password    = "******";
            command.NewPassword = "******";
            var result = handler.UpdatePassword(command, "userIdentity");

            Assert.IsTrue(result.Success);
        }
        public CommandResult UpdatePassword(UserUpdatePasswordCommand command, string userIdentity)
        {
            // verificação da validade do usuario e senha
            var user = _repository.GetSalt(command.Username);

            if (user == null)
            {
                return(new CommandResult(false, "Login inválido. ", command));
            }

            var salt_tabela = user.Salt;

            byte[] salt         = Convert.FromBase64String(salt_tabela);
            var    hashPassword = Hash.Create(command.Password, salt);

            if (user.Password != hashPassword)
            {
                return(new CommandResult(false, "Senha antiga não confere. ", command));
            }

            // criacao de novo hash para a nova senha
            hashPassword = Hash.Create(command.NewPassword, salt);

            user.UpdatePassword(command.NewPassword);

            AddNotifications(user);
            if (Invalid)
            {
                return(new CommandResult(false, GroupNotifications.Group(Notifications), command));
            }

            _repository.UpdatePassword(user.Id, hashPassword);

            var log = new AccessLog(
                "UpdatePassword",
                DateTime.Now,
                userIdentity,
                "UserAuth",
                $"Usuário alterado: {command.Username}");

            _log.Register(log);

            user.HidePassword();

            return(new CommandResultToken(true, "Senha alterada com sucesso! ", user));
        }
Exemplo n.º 6
0
 public ICommandResult UpdatePassword(UserUpdatePasswordCommand command)
 {
     try
     {
         if (command.Password == command.ConfirmPassword)
         {
             var user = _userRepository.Read(command.Id);
             user.Password = Encrypt.Password(command.Password);
             _userRepository.Update(user);
             var commandResult = new CommandResult("Senha atualizada com sucesso!", user, false);
             return(commandResult);
         }
         else
         {
             var commandResult = new CommandResult($"Senha e confirmação de senha não são iguais !", null, false);
             return(commandResult);
         }
     }
     catch (Exception ex)
     {
         var commandResult = new CommandResult($"{ex.InnerException.Message}", null, true);
         return(commandResult);
     }
 }
Exemplo n.º 7
0
 public CommandResult UpdatePassword([FromServices] IUserCommandHandler handler,
                                     [FromBody] UserUpdatePasswordCommand command)
 {
     return(handler.UpdatePassword(command, User.Identity.Name));
 }
Exemplo n.º 8
0
        public CommandResult UpdatePassword(UserUpdatePasswordCommand command)
        {
            CommandResult result = _handler.UpdatePassword(command, "admin");

            return(result);
        }