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

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