vendor/symfony/security-core/User/InMemoryUserProvider.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.  * InMemoryUserProvider is a simple non persistent user provider.
  15.  *
  16.  * Useful for testing, demonstration, prototyping, and for simple needs
  17.  * (a backend with a unique admin for instance)
  18.  *
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  */
  21. class InMemoryUserProvider implements UserProviderInterface
  22. {
  23.     /**
  24.      * @var array<string, UserInterface>
  25.      */
  26.     private $users;
  27.     /**
  28.      * The user array is a hash where the keys are usernames and the values are
  29.      * an array of attributes: 'password', 'enabled', and 'roles'.
  30.      *
  31.      * @param array<string, array{password?: string, enabled?: bool, roles?: list<string>}> $users An array of users
  32.      */
  33.     public function __construct(array $users = [])
  34.     {
  35.         foreach ($users as $username => $attributes) {
  36.             $password $attributes['password'] ?? null;
  37.             $enabled $attributes['enabled'] ?? true;
  38.             $roles $attributes['roles'] ?? [];
  39.             $user = new InMemoryUser($username$password$roles$enabled);
  40.             $this->createUser($user);
  41.         }
  42.     }
  43.     /**
  44.      * Adds a new User to the provider.
  45.      *
  46.      * @throws \LogicException
  47.      */
  48.     public function createUser(UserInterface $user)
  49.     {
  50.         // @deprecated since Symfony 5.3, change to $user->getUserIdentifier() in 6.0
  51.         $userIdentifier strtolower(method_exists($user'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername());
  52.         if (isset($this->users[$userIdentifier])) {
  53.             throw new \LogicException('Another user with the same username already exists.');
  54.         }
  55.         $this->users[$userIdentifier] = $user;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function loadUserByUsername(string $username)
  61.     {
  62.         trigger_deprecation('symfony/security-core''5.3''Method "%s()" is deprecated, use loadUserByIdentifier() instead.'__METHOD__);
  63.         return $this->loadUserByIdentifier($username);
  64.     }
  65.     public function loadUserByIdentifier(string $identifier): UserInterface
  66.     {
  67.         $user $this->getUser($identifier);
  68.         // @deprecated since Symfony 5.3, change to $user->getUserIdentifier() in 6.0
  69.         return new InMemoryUser(method_exists($user'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), $user->getPassword(), $user->getRoles(), $user->isEnabled());
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public function refreshUser(UserInterface $user)
  75.     {
  76.         if (!$user instanceof InMemoryUser && !$user instanceof User) {
  77.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'get_debug_type($user)));
  78.         }
  79.         // @deprecated since Symfony 5.3, change to $user->getUserIdentifier() in 6.0
  80.         $storedUser $this->getUser(method_exists($user'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername());
  81.         $userIdentifier method_exists($storedUser'getUserIdentifier') ? $storedUser->getUserIdentifier() : $storedUser->getUsername();
  82.         // @deprecated since Symfony 5.3
  83.         if (User::class === \get_class($user)) {
  84.             if (User::class !== \get_class($storedUser)) {
  85.                 $accountNonExpired true;
  86.                 $credentialsNonExpired $storedUser->getPassword() === $user->getPassword();
  87.                 $accountNonLocked true;
  88.             } else {
  89.                 $accountNonExpired $storedUser->isAccountNonExpired();
  90.                 $credentialsNonExpired $storedUser->isCredentialsNonExpired() && $storedUser->getPassword() === $user->getPassword();
  91.                 $accountNonLocked $storedUser->isAccountNonLocked();
  92.             }
  93.             return new User($userIdentifier$storedUser->getPassword(), $storedUser->getRoles(), $storedUser->isEnabled(), $accountNonExpired$credentialsNonExpired$accountNonLocked);
  94.         }
  95.         return new InMemoryUser($userIdentifier$storedUser->getPassword(), $storedUser->getRoles(), $storedUser->isEnabled());
  96.     }
  97.     /**
  98.      * {@inheritdoc}
  99.      */
  100.     public function supportsClass(string $class)
  101.     {
  102.         // @deprecated since Symfony 5.3
  103.         if (User::class === $class) {
  104.             return true;
  105.         }
  106.         return InMemoryUser::class == $class;
  107.     }
  108.     /**
  109.      * Returns the user by given username.
  110.      *
  111.      * @throws UserNotFoundException if user whose given username does not exist
  112.      */
  113.     private function getUser(string $username)/* : InMemoryUser */
  114.     {
  115.         if (!isset($this->users[strtolower($username)])) {
  116.             $ex = new UserNotFoundException(sprintf('Username "%s" does not exist.'$username));
  117.             $ex->setUserIdentifier($username);
  118.             throw $ex;
  119.         }
  120.         return $this->users[strtolower($username)];
  121.     }
  122. }