public function execute(ChangePasswordAction $action) { $user = $action->getUser(); $newPassword = $action->getNewPassword(); $encoder = $this->encoderFactory->getEncoder($user); $salt = md5(uniqid(null, true)); $password = $encoder->encodePassword($newPassword, $salt); $newCredentials = $this->credentialsRepository->createNew($user, $password, $salt); $oldCredentials = $this->credentialsRepository->findOneByUser($user); if (null === $oldCredentials) { throw new \RuntimeException(sprintf('Unable to retrieve old credentials for user %s', $user->getUsername())); } $this->credentialsRepository->remove($oldCredentials); $this->credentialsRepository->save($newCredentials); }
function it_should_change_user_password(CredentialsRepository $credentialsRepository, EncoderFactoryInterface $encoderFactory, EventDispatcherInterface $eventDispatcher, PasswordEncoderInterface $passwordEncoder, User $user, Credentials $oldCredentials, Credentials $newCredentials, ChangePasswordAction $action) { // Mocks $action->getUser()->willReturn($user); $action->getOldPassword()->willReturn('old_p4ssw0rd'); $action->getNewPassword()->willReturn('new_p4ssw0rd'); $encoderFactory->getEncoder($user)->willReturn($passwordEncoder); $passwordEncoder->encodePassword('new_p4ssw0rd', Argument::type('string'))->shouldBeCalled()->willReturn('encoded_password=='); // Old credentials should be remove $credentialsRepository->findOneByUser($user)->willReturn($oldCredentials); $credentialsRepository->remove($oldCredentials)->shouldBeCalled(); // New encoded password should be saved $credentialsRepository->createNew($user, 'encoded_password==', Argument::type('string'))->shouldBeCalled()->willReturn($newCredentials); $credentialsRepository->save($newCredentials)->shouldBeCalled(); $this->execute($action); }