EntityUserProvider.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Bridge\Doctrine\Security\User;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Doctrine\Persistence\Mapping\ClassMetadata;
  13. use Doctrine\Persistence\ObjectManager;
  14. use Doctrine\Persistence\ObjectRepository;
  15. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  16. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  17. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  18. use Symfony\Component\Security\Core\User\UserInterface;
  19. use Symfony\Component\Security\Core\User\UserProviderInterface;
  20. /**
  21. * Wrapper around a Doctrine ObjectManager.
  22. *
  23. * Provides provisioning for Doctrine entity users.
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  27. */
  28. class EntityUserProvider implements UserProviderInterface, PasswordUpgraderInterface
  29. {
  30. private $registry;
  31. private $managerName;
  32. private $classOrAlias;
  33. private $class;
  34. private $property;
  35. public function __construct(ManagerRegistry $registry, string $classOrAlias, string $property = null, string $managerName = null)
  36. {
  37. $this->registry = $registry;
  38. $this->managerName = $managerName;
  39. $this->classOrAlias = $classOrAlias;
  40. $this->property = $property;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function loadUserByUsername(string $username)
  46. {
  47. $repository = $this->getRepository();
  48. if (null !== $this->property) {
  49. $user = $repository->findOneBy([$this->property => $username]);
  50. } else {
  51. if (!$repository instanceof UserLoaderInterface) {
  52. throw new \InvalidArgumentException(sprintf('You must either make the "%s" entity Doctrine Repository ("%s") implement "Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface" or set the "property" option in the corresponding entity provider configuration.', $this->classOrAlias, get_debug_type($repository)));
  53. }
  54. $user = $repository->loadUserByUsername($username);
  55. }
  56. if (null === $user) {
  57. $e = new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
  58. $e->setUsername($username);
  59. throw $e;
  60. }
  61. return $user;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function refreshUser(UserInterface $user)
  67. {
  68. $class = $this->getClass();
  69. if (!$user instanceof $class) {
  70. throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_debug_type($user)));
  71. }
  72. $repository = $this->getRepository();
  73. if ($repository instanceof UserProviderInterface) {
  74. $refreshedUser = $repository->refreshUser($user);
  75. } else {
  76. // The user must be reloaded via the primary key as all other data
  77. // might have changed without proper persistence in the database.
  78. // That's the case when the user has been changed by a form with
  79. // validation errors.
  80. if (!$id = $this->getClassMetadata()->getIdentifierValues($user)) {
  81. throw new \InvalidArgumentException('You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine.');
  82. }
  83. $refreshedUser = $repository->find($id);
  84. if (null === $refreshedUser) {
  85. $e = new UsernameNotFoundException('User with id '.json_encode($id).' not found.');
  86. $e->setUsername(json_encode($id));
  87. throw $e;
  88. }
  89. }
  90. return $refreshedUser;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function supportsClass(string $class)
  96. {
  97. return $class === $this->getClass() || is_subclass_of($class, $this->getClass());
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
  103. {
  104. $class = $this->getClass();
  105. if (!$user instanceof $class) {
  106. throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_debug_type($user)));
  107. }
  108. $repository = $this->getRepository();
  109. if ($repository instanceof PasswordUpgraderInterface) {
  110. $repository->upgradePassword($user, $newEncodedPassword);
  111. }
  112. }
  113. private function getObjectManager(): ObjectManager
  114. {
  115. return $this->registry->getManager($this->managerName);
  116. }
  117. private function getRepository(): ObjectRepository
  118. {
  119. return $this->getObjectManager()->getRepository($this->classOrAlias);
  120. }
  121. private function getClass(): string
  122. {
  123. if (null === $this->class) {
  124. $class = $this->classOrAlias;
  125. if (false !== strpos($class, ':')) {
  126. $class = $this->getClassMetadata()->getName();
  127. }
  128. $this->class = $class;
  129. }
  130. return $this->class;
  131. }
  132. private function getClassMetadata(): ClassMetadata
  133. {
  134. return $this->getObjectManager()->getClassMetadata($this->classOrAlias);
  135. }
  136. }