vendor/symfony/doctrine-bridge/Security/User/EntityUserProvider.php line 50

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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 Doctrine\Persistence\Proxy;
  16. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  17. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  18. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  19. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  20. use Symfony\Component\Security\Core\User\UserInterface;
  21. use Symfony\Component\Security\Core\User\UserProviderInterface;
  22. /**
  23.  * Wrapper around a Doctrine ObjectManager.
  24.  *
  25.  * Provides provisioning for Doctrine entity users.
  26.  *
  27.  * @author Fabien Potencier <[email protected]>
  28.  * @author Johannes M. Schmitt <[email protected]>
  29.  */
  30. class EntityUserProvider implements UserProviderInterfacePasswordUpgraderInterface
  31. {
  32.     private ManagerRegistry $registry;
  33.     private ?string $managerName;
  34.     private string $classOrAlias;
  35.     private string $class;
  36.     private ?string $property;
  37.     public function __construct(ManagerRegistry $registrystring $classOrAliasstring $property nullstring $managerName null)
  38.     {
  39.         $this->registry $registry;
  40.         $this->managerName $managerName;
  41.         $this->classOrAlias $classOrAlias;
  42.         $this->property $property;
  43.     }
  44.     public function loadUserByIdentifier(string $identifier): UserInterface
  45.     {
  46.         $repository $this->getRepository();
  47.         if (null !== $this->property) {
  48.             $user $repository->findOneBy([$this->property => $identifier]);
  49.         } else {
  50.             if (!$repository instanceof UserLoaderInterface) {
  51.                 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->classOrAliasget_debug_type($repository)));
  52.             }
  53.             $user $repository->loadUserByIdentifier($identifier);
  54.         }
  55.         if (null === $user) {
  56.             $e = new UserNotFoundException(sprintf('User "%s" not found.'$identifier));
  57.             $e->setUserIdentifier($identifier);
  58.             throw $e;
  59.         }
  60.         return $user;
  61.     }
  62.     public function refreshUser(UserInterface $user): UserInterface
  63.     {
  64.         $class $this->getClass();
  65.         if (!$user instanceof $class) {
  66.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'get_debug_type($user)));
  67.         }
  68.         $repository $this->getRepository();
  69.         if ($repository instanceof UserProviderInterface) {
  70.             $refreshedUser $repository->refreshUser($user);
  71.         } else {
  72.             // The user must be reloaded via the primary key as all other data
  73.             // might have changed without proper persistence in the database.
  74.             // That's the case when the user has been changed by a form with
  75.             // validation errors.
  76.             if (!$id $this->getClassMetadata()->getIdentifierValues($user)) {
  77.                 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.');
  78.             }
  79.             $refreshedUser $repository->find($id);
  80.             if (null === $refreshedUser) {
  81.                 $e = new UserNotFoundException('User with id '.json_encode($id).' not found.');
  82.                 $e->setUserIdentifier(json_encode($id));
  83.                 throw $e;
  84.             }
  85.         }
  86.         if ($refreshedUser instanceof Proxy && !$refreshedUser->__isInitialized()) {
  87.             $refreshedUser->__load();
  88.         }
  89.         return $refreshedUser;
  90.     }
  91.     public function supportsClass(string $class): bool
  92.     {
  93.         return $class === $this->getClass() || is_subclass_of($class$this->getClass());
  94.     }
  95.     /**
  96.      * @final
  97.      */
  98.     public function upgradePassword(PasswordAuthenticatedUserInterface $userstring $newHashedPassword): void
  99.     {
  100.         $class $this->getClass();
  101.         if (!$user instanceof $class) {
  102.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'get_debug_type($user)));
  103.         }
  104.         $repository $this->getRepository();
  105.         if ($repository instanceof PasswordUpgraderInterface) {
  106.             $repository->upgradePassword($user$newHashedPassword);
  107.         }
  108.     }
  109.     private function getObjectManager(): ObjectManager
  110.     {
  111.         return $this->registry->getManager($this->managerName);
  112.     }
  113.     private function getRepository(): ObjectRepository
  114.     {
  115.         return $this->getObjectManager()->getRepository($this->classOrAlias);
  116.     }
  117.     private function getClass(): string
  118.     {
  119.         if (!isset($this->class)) {
  120.             $class $this->classOrAlias;
  121.             if (str_contains($class':')) {
  122.                 $class $this->getClassMetadata()->getName();
  123.             }
  124.             $this->class $class;
  125.         }
  126.         return $this->class;
  127.     }
  128.     private function getClassMetadata(): ClassMetadata
  129.     {
  130.         return $this->getObjectManager()->getClassMetadata($this->classOrAlias);
  131.     }
  132. }