vendor/symfony/security-core/User/ChainUserProvider.php line 25

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\Component\Security\Core\User;
  11. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  12. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  13. /**
  14.  * Chain User Provider.
  15.  *
  16.  * This provider calls several leaf providers in a chain until one is able to
  17.  * handle the request.
  18.  *
  19.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  20.  */
  21. class ChainUserProvider implements UserProviderInterfacePasswordUpgraderInterface
  22. {
  23.     private $providers;
  24.     /**
  25.      * @param iterable<array-key, UserProviderInterface> $providers
  26.      */
  27.     public function __construct(iterable $providers)
  28.     {
  29.         $this->providers $providers;
  30.     }
  31.     /**
  32.      * @return UserProviderInterface[]
  33.      */
  34.     public function getProviders()
  35.     {
  36.         if ($this->providers instanceof \Traversable) {
  37.             return iterator_to_array($this->providers);
  38.         }
  39.         return $this->providers;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function loadUserByUsername(string $username)
  45.     {
  46.         trigger_deprecation('symfony/security-core''5.3''Method "%s()" is deprecated, use loadUserByIdentifier() instead.'__METHOD__);
  47.         return $this->loadUserByIdentifier($username);
  48.     }
  49.     public function loadUserByIdentifier(string $identifier): UserInterface
  50.     {
  51.         foreach ($this->providers as $provider) {
  52.             try {
  53.                 // @deprecated since Symfony 5.3, change to $provider->loadUserByIdentifier() in 6.0
  54.                 if (!method_exists($provider'loadUserByIdentifier')) {
  55.                     trigger_deprecation('symfony/security-core''5.3''Not implementing method "loadUserByIdentifier()" in user provider "%s" is deprecated. This method will replace "loadUserByUsername()" in Symfony 6.0.'get_debug_type($provider));
  56.                     return $provider->loadUserByUsername($identifier);
  57.                 }
  58.                 return $provider->loadUserByIdentifier($identifier);
  59.             } catch (UserNotFoundException $e) {
  60.                 // try next one
  61.             }
  62.         }
  63.         $ex = new UserNotFoundException(sprintf('There is no user with identifier "%s".'$identifier));
  64.         $ex->setUserIdentifier($identifier);
  65.         throw $ex;
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function refreshUser(UserInterface $user)
  71.     {
  72.         $supportedUserFound false;
  73.         foreach ($this->providers as $provider) {
  74.             try {
  75.                 if (!$provider->supportsClass(get_debug_type($user))) {
  76.                     continue;
  77.                 }
  78.                 return $provider->refreshUser($user);
  79.             } catch (UnsupportedUserException $e) {
  80.                 // try next one
  81.             } catch (UserNotFoundException $e) {
  82.                 $supportedUserFound true;
  83.                 // try next one
  84.             }
  85.         }
  86.         if ($supportedUserFound) {
  87.             // @deprecated since Symfony 5.3, change to $user->getUserIdentifier() in 6.0
  88.             $username method_exists($user'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
  89.             $e = new UserNotFoundException(sprintf('There is no user with name "%s".'$username));
  90.             $e->setUserIdentifier($username);
  91.             throw $e;
  92.         } else {
  93.             throw new UnsupportedUserException(sprintf('There is no user provider for user "%s". Shouldn\'t the "supportsClass()" method of your user provider return true for this classname?'get_debug_type($user)));
  94.         }
  95.     }
  96.     /**
  97.      * {@inheritdoc}
  98.      */
  99.     public function supportsClass(string $class)
  100.     {
  101.         foreach ($this->providers as $provider) {
  102.             if ($provider->supportsClass($class)) {
  103.                 return true;
  104.             }
  105.         }
  106.         return false;
  107.     }
  108.     /**
  109.      * @param PasswordAuthenticatedUserInterface $user
  110.      *
  111.      * {@inheritdoc}
  112.      */
  113.     public function upgradePassword($userstring $newHashedPassword): void
  114.     {
  115.         if (!$user instanceof PasswordAuthenticatedUserInterface) {
  116.             trigger_deprecation('symfony/security-core''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));
  117.             if (!$user instanceof UserInterface) {
  118.                 throw new \TypeError(sprintf('The "%s::upgradePassword()" method expects an instance of "%s" as first argument, "%s" given.', static::class, PasswordAuthenticatedUserInterface::class, get_debug_type($user)));
  119.             }
  120.         }
  121.         foreach ($this->providers as $provider) {
  122.             if ($provider instanceof PasswordUpgraderInterface) {
  123.                 try {
  124.                     $provider->upgradePassword($user$newHashedPassword);
  125.                 } catch (UnsupportedUserException $e) {
  126.                     // ignore: password upgrades are opportunistic
  127.                 }
  128.             }
  129.         }
  130.     }
  131. }