1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Security\Core\User;
13
14use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
15use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
16
17/**
18 * InMemoryUserProvider is a simple non persistent user provider.
19 *
20 * Useful for testing, demonstration, prototyping, and for simple needs
21 * (a backend with a unique admin for instance)
22 *
23 * @author Fabien Potencier <fabien@symfony.com>
24 */
25class InMemoryUserProvider implements UserProviderInterface
26{
27    private $users;
28
29    /**
30     * Constructor.
31     *
32     * The user array is a hash where the keys are usernames and the values are
33     * an array of attributes: 'password', 'enabled', and 'roles'.
34     *
35     * @param array $users An array of users
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     * Adds a new User to the provider.
51     *
52     * @param UserInterface $user A UserInterface instance
53     *
54     * @throws \LogicException
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     * {@inheritdoc}
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     * {@inheritdoc}
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     * {@inheritdoc}
97     */
98    public function supportsClass($class)
99    {
100        return $class === 'Symfony\Component\Security\Core\User\User';
101    }
102}
103