UserPasswordEncoder.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Security\Core\User\UserInterface;
  12. /**
  13. * A generic password encoder.
  14. *
  15. * @author Ariel Ferrandini <arielferrandini@gmail.com>
  16. */
  17. class UserPasswordEncoder implements UserPasswordEncoderInterface
  18. {
  19. private $encoderFactory;
  20. public function __construct(EncoderFactoryInterface $encoderFactory)
  21. {
  22. $this->encoderFactory = $encoderFactory;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function encodePassword(UserInterface $user, string $plainPassword)
  28. {
  29. $encoder = $this->encoderFactory->getEncoder($user);
  30. return $encoder->encodePassword($plainPassword, $user->getSalt());
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function isPasswordValid(UserInterface $user, string $raw)
  36. {
  37. if (null === $user->getPassword()) {
  38. return false;
  39. }
  40. $encoder = $this->encoderFactory->getEncoder($user);
  41. return $encoder->isPasswordValid($user->getPassword(), $raw, $user->getSalt());
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function needsRehash(UserInterface $user): bool
  47. {
  48. if (null === $user->getPassword()) {
  49. return false;
  50. }
  51. $encoder = $this->encoderFactory->getEncoder($user);
  52. return $encoder->needsRehash($user->getPassword());
  53. }
  54. }