diff options
author | Fabien Potencier <fabien.potencier@gmail.com> | 2010-10-19 13:06:43 +0200 |
---|---|---|
committer | Fabien Potencier <fabien.potencier@gmail.com> | 2010-10-19 13:33:17 +0200 |
commit | 3fec93d3ff1f6a31f078e5558a15a75539bf5185 (patch) | |
tree | 1a6229643289d9d0ca55871bab9497035a6e49f1 /User/InMemoryUserProvider.php | |
download | symfony-security-3fec93d3ff1f6a31f078e5558a15a75539bf5185.zip symfony-security-3fec93d3ff1f6a31f078e5558a15a75539bf5185.tar.gz symfony-security-3fec93d3ff1f6a31f078e5558a15a75539bf5185.tar.bz2 |
added the Security Component and its integration into the MVC framework
Happy birthday symfony!
Diffstat (limited to 'User/InMemoryUserProvider.php')
-rw-r--r-- | User/InMemoryUserProvider.php | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/User/InMemoryUserProvider.php b/User/InMemoryUserProvider.php new file mode 100644 index 0000000..6e1febe --- /dev/null +++ b/User/InMemoryUserProvider.php @@ -0,0 +1,78 @@ +<?php + +namespace Symfony\Component\Security\User; + +use Symfony\Component\Security\Exception\UsernameNotFoundException; +use Symfony\Component\Security\Exception\AccessDeniedException; +use Symfony\Component\Security\Authentication\Token\UsernamePasswordToken; + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * InMemoryUserProvider is a simple non persistent user provider. + * + * Useful for testing, demonstration, prototyping, and for + * simple needs (a backend with a unique admin for instance) + * + * @author Fabien Potencier <fabien.potencier@symfony-project.com> + */ +class InMemoryUserProvider implements UserProviderInterface +{ + protected $users; + + /** + * Constructor. + * + * The user array is hash where the keys are usernames and the values are + * an array of attributes: 'password', 'enabled', and 'roles'. + * + * @param array $users An array of users + */ + public function __construct(array $users = array()) + { + foreach ($users as $username => $attributes) { + $password = isset($attributes['password']) ? $attributes['password'] : null; + $enabled = isset($attributes['enabled']) ? $attributes['enabled'] : true; + $roles = isset($attributes['roles']) ? $attributes['roles'] : array(); + $user = new User($username, $password, $roles, $enabled, true, true, true); + + $this->createUser($user); + } + } + + /** + * Adds a new User to the provider. + * + * @param AccountInterface $user A AccountInterface instance + */ + public function createUser(AccountInterface $user) + { + if (isset($this->users[strtolower($user->getUsername())])) { + throw new \LogicException('Another user with the same username already exist.'); + } + + $this->users[strtolower($user->getUsername())] = $user; + } + + /** + * {@inheritdoc} + */ + public function loadUserByUsername($username) + { + if (!isset($this->users[strtolower($username)])) { + throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); + } + + $user = $this->users[strtolower($username)]; + + return new User($user->getUsername(), $user->getPassword(), $user->getRoles(), $user->isEnabled(), $user->isAccountNonExpired(), + $user->isCredentialsNonExpired(), $user->isAccountNonLocked()); + } +} |