diff options
author | Johannes M. Schmitt <schmittjoh@gmail.com> | 2011-02-16 12:51:50 +0100 |
---|---|---|
committer | Fabien Potencier <fabien.potencier@gmail.com> | 2011-02-16 23:00:27 +0100 |
commit | c9cd5c60b7ee6a1eef90826a8f323b467c13b9f7 (patch) | |
tree | 0ea227fc462bdf43e7c28520001c1dd99984ab84 /Core/User/ChainUserProvider.php | |
parent | 731a0ea0c8e1fc1889d796348a9b83762e2fd7d1 (diff) | |
download | symfony-security-c9cd5c60b7ee6a1eef90826a8f323b467c13b9f7.zip symfony-security-c9cd5c60b7ee6a1eef90826a8f323b467c13b9f7.tar.gz symfony-security-c9cd5c60b7ee6a1eef90826a8f323b467c13b9f7.tar.bz2 |
[Security] adds a chain user provider
Diffstat (limited to 'Core/User/ChainUserProvider.php')
-rw-r--r-- | Core/User/ChainUserProvider.php | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Core/User/ChainUserProvider.php b/Core/User/ChainUserProvider.php new file mode 100644 index 0000000..296d099 --- /dev/null +++ b/Core/User/ChainUserProvider.php @@ -0,0 +1,70 @@ +<?php + +namespace Symfony\Component\Security\Core\User; + +use Symfony\Component\Security\Core\Exception\UnsupportedAccountException; +use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; + +/** + * Chain User Provider. + * + * This provider calls several leaf providers in a chain until one is able to + * handle the request. + * + * @author Johannes M. Schmitt <schmittjoh@gmail.com> + */ +class ChainUserProvider implements UserProviderInterface +{ + protected $providers; + + public function __construct(array $providers) + { + $this->providers = $providers; + } + + /** + * {@inheritDoc} + */ + public function loadUserByUsername($username) + { + foreach ($this->providers as $provider) { + try { + return $provider->loadUserByUsername($username); + } catch (UsernameNotFoundException $notFound) { + // try next one + } + } + + throw new UsernameNotFoundException(sprintf('There is no user with name "%s".', $username)); + } + + /** + * {@inheritDoc} + */ + public function loadUserByAccount(AccountInterface $account) + { + foreach ($this->providers as $provider) { + try { + return $provider->loadUserByAccount($account); + } catch (UnsupportedAccountException $unsupported) { + // try next one + } + } + + throw new UnsupportedAccountException(sprintf('The account "%s" is not supported.', get_class($account))); + } + + /** + * {@inheritDoc} + */ + public function supportsClass($class) + { + foreach ($this->providers as $provider) { + if ($provider->supportsClass($class)) { + return true; + } + } + + return false; + } +}
\ No newline at end of file |