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

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\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\UserNotFoundException;
  17. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  18. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. use Symfony\Component\Security\Core\User\UserProviderInterface;
  21. /**
  22.  * Wrapper around a Doctrine ObjectManager.
  23.  *
  24.  * Provides provisioning for Doctrine entity users.
  25.  *
  26.  * @author Fabien Potencier <fabien@symfony.com>
  27.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  28.  */
  29. class EntityUserProvider implements UserProviderInterfacePasswordUpgraderInterface
  30. {
  31.     private $registry;
  32.     private $managerName;
  33.     private $classOrAlias;
  34.     private $class;
  35.     private $property;
  36.     public function __construct(ManagerRegistry $registrystring $classOrAliasstring $property nullstring $managerName null)
  37.     {
  38.         $this->registry $registry;
  39.         $this->managerName $managerName;
  40.         $this->classOrAlias $classOrAlias;
  41.         $this->property $property;
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function loadUserByUsername(string $username)
  47.     {
  48.         trigger_deprecation('symfony/doctrine-bridge''5.3''Method "%s()" is deprecated, use loadUserByIdentifier() instead.'__METHOD__);
  49.         return $this->loadUserByIdentifier($username);
  50.     }
  51.     public function loadUserByIdentifier(string $identifier): UserInterface
  52.     {
  53.         $repository $this->getRepository();
  54.         if (null !== $this->property) {
  55.             $user $repository->findOneBy([$this->property => $identifier]);
  56.         } else {
  57.             if (!$repository instanceof UserLoaderInterface) {
  58.                 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)));
  59.             }
  60.             // @deprecated since Symfony 5.3, change to $repository->loadUserByIdentifier() in 6.0
  61.             if (method_exists($repository'loadUserByIdentifier')) {
  62.                 $user $repository->loadUserByIdentifier($identifier);
  63.             } else {
  64.                 trigger_deprecation('symfony/doctrine-bridge''5.3''Not implementing method "loadUserByIdentifier()" in user loader "%s" is deprecated. This method will replace "loadUserByUsername()" in Symfony 6.0.'get_debug_type($repository));
  65.                 $user $repository->loadUserByUsername($identifier);
  66.             }
  67.         }
  68.         if (null === $user) {
  69.             $e = new UserNotFoundException(sprintf('User "%s" not found.'$identifier));
  70.             $e->setUserIdentifier($identifier);
  71.             throw $e;
  72.         }
  73.         return $user;
  74.     }
  75.     /**
  76.      * {@inheritdoc}
  77.      */
  78.     public function refreshUser(UserInterface $user)
  79.     {
  80.         $class $this->getClass();
  81.         if (!$user instanceof $class) {
  82.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'get_debug_type($user)));
  83.         }
  84.         $repository $this->getRepository();
  85.         if ($repository instanceof UserProviderInterface) {
  86.             $refreshedUser $repository->refreshUser($user);
  87.         } else {
  88.             // The user must be reloaded via the primary key as all other data
  89.             // might have changed without proper persistence in the database.
  90.             // That's the case when the user has been changed by a form with
  91.             // validation errors.
  92.             if (!$id $this->getClassMetadata()->getIdentifierValues($user)) {
  93.                 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.');
  94.             }
  95.             $refreshedUser $repository->find($id);
  96.             if (null === $refreshedUser) {
  97.                 $e = new UserNotFoundException('User with id '.json_encode($id).' not found.');
  98.                 $e->setUserIdentifier(json_encode($id));
  99.                 throw $e;
  100.             }
  101.         }
  102.         return $refreshedUser;
  103.     }
  104.     /**
  105.      * {@inheritdoc}
  106.      */
  107.     public function supportsClass(string $class)
  108.     {
  109.         return $class === $this->getClass() || is_subclass_of($class$this->getClass());
  110.     }
  111.     /**
  112.      * {@inheritdoc}
  113.      *
  114.      * @final
  115.      */
  116.     public function upgradePassword($userstring $newHashedPassword): void
  117.     {
  118.         if (!$user instanceof PasswordAuthenticatedUserInterface) {
  119.             trigger_deprecation('symfony/doctrine-bridge''5.3''The "%s::upgradePassword()" method expects an instance of "%s" as first argument, the "%s" class should implement it.'PasswordUpgraderInterface::class, PasswordAuthenticatedUserInterface::class, get_debug_type($user));
  120.             if (!$user instanceof UserInterface) {
  121.                 throw new \TypeError(sprintf('The "%s()" method expects an instance of "%s" as first argument, "%s" given.'__METHOD__PasswordAuthenticatedUserInterface::class, get_debug_type($user)));
  122.             }
  123.         }
  124.         $class $this->getClass();
  125.         if (!$user instanceof $class) {
  126.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'get_debug_type($user)));
  127.         }
  128.         $repository $this->getRepository();
  129.         if ($repository instanceof PasswordUpgraderInterface) {
  130.             $repository->upgradePassword($user$newHashedPassword);
  131.         }
  132.     }
  133.     private function getObjectManager(): ObjectManager
  134.     {
  135.         return $this->registry->getManager($this->managerName);
  136.     }
  137.     private function getRepository(): ObjectRepository
  138.     {
  139.         return $this->getObjectManager()->getRepository($this->classOrAlias);
  140.     }
  141.     private function getClass(): string
  142.     {
  143.         if (null === $this->class) {
  144.             $class $this->classOrAlias;
  145.             if (str_contains($class':')) {
  146.                 $class $this->getClassMetadata()->getName();
  147.             }
  148.             $this->class $class;
  149.         }
  150.         return $this->class;
  151.     }
  152.     private function getClassMetadata(): ClassMetadata
  153.     {
  154.         return $this->getObjectManager()->getClassMetadata($this->classOrAlias);
  155.     }
  156. }