vendor/symfony/security-core/Encoder/UserPasswordEncoder.php line 19

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Core\Encoder;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
  12. use Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface;
  13. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. trigger_deprecation('symfony/security-core', '5.3', 'The "%s" class is deprecated, use "%s" instead.', UserPasswordEncoder::class, UserPasswordHasher::class);
  16. /**
  17. * A generic password encoder.
  18. *
  19. * @author Ariel Ferrandini <arielferrandini@gmail.com>
  20. *
  21. * @deprecated since Symfony 5.3, use {@link UserPasswordHasher} instead
  22. */
  23. class UserPasswordEncoder implements UserPasswordEncoderInterface
  24. {
  25. private $encoderFactory;
  26. public function __construct(EncoderFactoryInterface $encoderFactory)
  27. {
  28. $this->encoderFactory = $encoderFactory;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function encodePassword(UserInterface $user, string $plainPassword)
  34. {
  35. $encoder = $this->encoderFactory->getEncoder($user);
  36. if (!$user instanceof PasswordAuthenticatedUserInterface) {
  37. trigger_deprecation('symfony/password-hasher', '5.3', 'Not implementing the "%s" interface while using "%s" is deprecated, the "%s" class should implement it.', PasswordAuthenticatedUserInterface::class, __CLASS__, get_debug_type($user));
  38. }
  39. $salt = $user->getSalt();
  40. if ($salt && !$user instanceof LegacyPasswordAuthenticatedUserInterface) {
  41. trigger_deprecation('symfony/password-hasher', '5.3', 'Returning a string from "getSalt()" without implementing the "%s" interface is deprecated, the "%s" class should implement it.', LegacyPasswordAuthenticatedUserInterface::class, get_debug_type($user));
  42. }
  43. return $encoder->encodePassword($plainPassword, $user->getSalt());
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function isPasswordValid(UserInterface $user, string $raw)
  49. {
  50. if (null === $user->getPassword()) {
  51. return false;
  52. }
  53. $encoder = $this->encoderFactory->getEncoder($user);
  54. return $encoder->isPasswordValid($user->getPassword(), $raw, $user->getSalt());
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function needsRehash(UserInterface $user): bool
  60. {
  61. if (null === $user->getPassword()) {
  62. return false;
  63. }
  64. $encoder = $this->encoderFactory->getEncoder($user);
  65. return $encoder->needsRehash($user->getPassword());
  66. }
  67. }