1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\Security\Core\User;
13:
14: use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
15: use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
16:
17: 18: 19: 20: 21: 22: 23: 24:
25: class InMemoryUserProvider implements UserProviderInterface
26: {
27: private $users;
28:
29: 30: 31: 32: 33: 34: 35: 36:
37: public function __construct(array $users = array())
38: {
39: foreach ($users as $username => $attributes) {
40: $password = isset($attributes['password']) ? $attributes['password'] : null;
41: $enabled = isset($attributes['enabled']) ? $attributes['enabled'] : true;
42: $roles = isset($attributes['roles']) ? $attributes['roles'] : array();
43: $user = new User($username, $password, $roles, $enabled, true, true, true);
44:
45: $this->createUser($user);
46: }
47: }
48:
49: 50: 51: 52: 53: 54: 55:
56: public function createUser(UserInterface $user)
57: {
58: if (isset($this->users[strtolower($user->getUsername())])) {
59: throw new \LogicException('Another user with the same username already exists.');
60: }
61:
62: $this->users[strtolower($user->getUsername())] = $user;
63: }
64:
65: 66: 67:
68: public function loadUserByUsername($username)
69: {
70: if (!isset($this->users[strtolower($username)])) {
71: $ex = new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
72: $ex->setUsername($username);
73:
74: throw $ex;
75: }
76:
77: $user = $this->users[strtolower($username)];
78:
79: return new User($user->getUsername(), $user->getPassword(), $user->getRoles(), $user->isEnabled(), $user->isAccountNonExpired(),
80: $user->isCredentialsNonExpired(), $user->isAccountNonLocked());
81: }
82:
83: 84: 85:
86: public function refreshUser(UserInterface $user)
87: {
88: if (!$user instanceof User) {
89: throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
90: }
91:
92: return $this->loadUserByUsername($user->getUsername());
93: }
94:
95: 96: 97:
98: public function supportsClass($class)
99: {
100: return $class === 'Symfony\Component\Security\Core\User\User';
101: }
102: }
103: