InMemoryUserProvider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\UsernameNotFoundException;
  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. private $users;
  24. /**
  25. * The user array is a hash where the keys are usernames and the values are
  26. * an array of attributes: 'password', 'enabled', and 'roles'.
  27. *
  28. * @param array $users An array of users
  29. */
  30. public function __construct(array $users = [])
  31. {
  32. foreach ($users as $username => $attributes) {
  33. $password = $attributes['password'] ?? null;
  34. $enabled = $attributes['enabled'] ?? true;
  35. $roles = $attributes['roles'] ?? [];
  36. $user = new User($username, $password, $roles, $enabled, true, true, true);
  37. $this->createUser($user);
  38. }
  39. }
  40. /**
  41. * Adds a new User to the provider.
  42. *
  43. * @throws \LogicException
  44. */
  45. public function createUser(UserInterface $user)
  46. {
  47. if (isset($this->users[strtolower($user->getUsername())])) {
  48. throw new \LogicException('Another user with the same username already exists.');
  49. }
  50. $this->users[strtolower($user->getUsername())] = $user;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function loadUserByUsername(string $username)
  56. {
  57. $user = $this->getUser($username);
  58. return new User($user->getUsername(), $user->getPassword(), $user->getRoles(), $user->isEnabled(), $user->isAccountNonExpired(), $user->isCredentialsNonExpired(), $user->isAccountNonLocked());
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function refreshUser(UserInterface $user)
  64. {
  65. if (!$user instanceof User) {
  66. throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_debug_type($user)));
  67. }
  68. $storedUser = $this->getUser($user->getUsername());
  69. return new User($storedUser->getUsername(), $storedUser->getPassword(), $storedUser->getRoles(), $storedUser->isEnabled(), $storedUser->isAccountNonExpired(), $storedUser->isCredentialsNonExpired() && $storedUser->getPassword() === $user->getPassword(), $storedUser->isAccountNonLocked());
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function supportsClass(string $class)
  75. {
  76. return 'Symfony\Component\Security\Core\User\User' === $class;
  77. }
  78. /**
  79. * Returns the user by given username.
  80. *
  81. * @throws UsernameNotFoundException if user whose given username does not exist
  82. */
  83. private function getUser(string $username): User
  84. {
  85. if (!isset($this->users[strtolower($username)])) {
  86. $ex = new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
  87. $ex->setUsername($username);
  88. throw $ex;
  89. }
  90. return $this->users[strtolower($username)];
  91. }
  92. }