Skip to content

Commit

Permalink
[Maintenance] PasswordUpdater supports both Symfony 5.4 & Symfony 6
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafikooo authored and Zales0123 committed Oct 5, 2022
1 parent 0de67b7 commit c4e08e4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
25 changes: 22 additions & 3 deletions src/Sylius/Component/User/Security/PasswordUpdater.php
Expand Up @@ -17,15 +17,34 @@

final class PasswordUpdater implements PasswordUpdaterInterface
{
public function __construct(private UserPasswordEncoderInterface $userPasswordEncoder)
/** @psalm-suppress DeprecatedClass */
public function __construct(private UserPasswordEncoderInterface|UserPasswordHasherInterface $userPasswordEncoderOrHasher)
{
if ($this->userPasswordEncoderOrHasher instanceof UserPasswordEncoderInterface) {
trigger_deprecation(
'sylius/user',
'1.12',
'The "%s" class is deprecated, use "%s" instead.',
UserPasswordEncoderInterface::class,
UserPasswordHasherInterface::class
);
}
}

public function updatePassword(CredentialsHolderInterface $user): void
{
if (!in_array($user->getPlainPassword(), ['', null], true)) {
$user->setPassword($this->userPasswordEncoder->encode($user));
if (in_array($user->getPlainPassword(), ['', null], true)) {
return;
}

if ($this->userPasswordEncoderOrHasher instanceof UserPasswordEncoderInterface) {
$user->setPassword($this->userPasswordEncoderOrHasher->encode($user));
$user->eraseCredentials();

return;
}

$user->setPassword($this->userPasswordEncoderOrHasher->hash($user));
$user->eraseCredentials();
}
}
47 changes: 34 additions & 13 deletions src/Sylius/Component/User/spec/Security/PasswordUpdaterSpec.php
Expand Up @@ -18,21 +18,36 @@
use Sylius\Component\User\Model\UserInterface;
use Sylius\Component\User\Security\PasswordUpdaterInterface;
use Sylius\Component\User\Security\UserPasswordEncoderInterface;
use Sylius\Component\User\Security\UserPasswordHasherInterface;

final class PasswordUpdaterSpec extends ObjectBehavior
{
function let(UserPasswordEncoderInterface $userPasswordEncoder): void
function it_implements_password_updater_interface(UserPasswordHasherInterface $userPasswordHasher): void
{
$this->beConstructedWith($userPasswordEncoder);
$this->beConstructedWith($userPasswordHasher);
$this->shouldImplement(PasswordUpdaterInterface::class);
}

function it_implements_password_updater_interface(): void
{
$this->shouldImplement(PasswordUpdaterInterface::class);
function it_updates_user_profile_with_hashed_password_if_using_symfony_6(
UserPasswordHasherInterface $userPasswordHasher,
UserInterface $user
): void {
$this->beConstructedWith($userPasswordHasher);
$user->getPlainPassword()->willReturn('topSecretPlainPassword');

$userPasswordHasher->hash($user)->willReturn('topSecretHashedPassword');

$user->eraseCredentials()->shouldBeCalled();
$user->setPassword('topSecretHashedPassword')->shouldBeCalled();

$this->updatePassword($user);
}

function it_updates_user_profile_with_encoded_password(UserPasswordEncoderInterface $userPasswordEncoder, UserInterface $user): void
{
function it_updates_user_profile_with_encoded_password_if_using_symfony_5_4(
UserPasswordEncoderInterface $userPasswordEncoder,
UserInterface $user
): void {
$this->beConstructedWith($userPasswordEncoder);
$user->getPlainPassword()->willReturn('topSecretPlainPassword');

$userPasswordEncoder->encode($user)->willReturn('topSecretEncodedPassword');
Expand All @@ -43,23 +58,29 @@ function it_updates_user_profile_with_encoded_password(UserPasswordEncoderInterf
$this->updatePassword($user);
}

function it_does_nothing_if_plain_password_is_empty(UserPasswordEncoderInterface $userPasswordEncoder, UserInterface $user): void
{
function it_does_nothing_if_plain_password_is_empty(
UserPasswordHasherInterface $userPasswordHasher,
UserInterface $user
): void {
$this->beConstructedWith($userPasswordHasher);
$user->getPlainPassword()->willReturn('');

$userPasswordEncoder->encode($user)->willReturn('topSecretEncodedPassword');
$userPasswordHasher->hash($user)->shouldNotBeCalled();

$user->setPassword(Argument::any())->shouldNotBeCalled();
$user->eraseCredentials()->shouldNotBeCalled();

$this->updatePassword($user);
}

function it_does_nothing_if_plain_password_is_null(UserPasswordEncoderInterface $userPasswordEncoder, UserInterface $user): void
{
function it_does_nothing_if_plain_password_is_null(
UserPasswordHasherInterface $userPasswordHasher,
UserInterface $user
): void {
$this->beConstructedWith($userPasswordHasher);
$user->getPlainPassword()->willReturn(null);

$userPasswordEncoder->encode($user)->willReturn('topSecretEncodedPassword');
$userPasswordHasher->hash($user)->shouldNotBeCalled();

$user->setPassword(Argument::any())->shouldNotBeCalled();
$user->eraseCredentials()->shouldNotBeCalled();
Expand Down

0 comments on commit c4e08e4

Please sign in to comment.