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 /Authentication/AuthenticationProviderManager.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 'Authentication/AuthenticationProviderManager.php')
-rw-r--r-- | Authentication/AuthenticationProviderManager.php | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/Authentication/AuthenticationProviderManager.php b/Authentication/AuthenticationProviderManager.php new file mode 100644 index 0000000..1b50ccb --- /dev/null +++ b/Authentication/AuthenticationProviderManager.php @@ -0,0 +1,120 @@ +<?php + +namespace Symfony\Component\Security\Authentication; + +use Symfony\Component\Security\Exception\AccountStatusException; +use Symfony\Component\Security\Exception\AuthenticationException; +use Symfony\Component\Security\Exception\ProviderNotFoundException; +use Symfony\Component\Security\Authentication\Provider\AuthenticationProviderInterface; +use Symfony\Component\Security\Authentication\Token\TokenInterface; + +/* + * 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. + */ + +/** + * AuthenticationProviderManager uses a list of AuthenticationProviderInterface + * instances to authenticate a Token. + * + * @author Fabien Potencier <fabien.potencier@symfony-project.com> + */ +class AuthenticationProviderManager implements AuthenticationManagerInterface +{ + protected $providers; + protected $eraseCredentials; + + /** + * Constructor. + * + * @param AuthenticationProviderInterface[] $providers An array of AuthenticationProviderInterface instances + * @param Boolean $eraseCredentials Whether to erase credentials after authentication or not + */ + public function __construct(array $providers = array(), $eraseCredentials = true) + { + $this->setProviders($providers); + $this->eraseCredentials = $eraseCredentials; + } + + /** + * {@inheritdoc} + */ + public function authenticate(TokenInterface $token) + { + if (!count($this->providers)) { + throw new \LogicException('You must add at least one provider.'); + } + + $lastException = null; + $result = null; + + foreach ($this->providers as $provider) { + if (!$provider->supports($token)) { + continue; + } + + try { + $result = $provider->authenticate($token); + } catch (AccountStatusException $e) { + $e->setToken($token); + + throw $e; + } catch (AuthenticationException $e) { + $lastException = $e; + } + } + + if (null !== $result) { + if ($this->eraseCredentials) { + $result->eraseCredentials(); + } + + return $result; + } + + if (null === $lastException) { + $lastException = new ProviderNotFoundException(sprintf('No Authentication Provider found for token of class "%s".', get_class($token))); + } + + $lastException->setToken($token); + + throw $lastException; + } + + /** + * Returns the list of current providers. + * + * @return AuthenticationProviderInterface[] An array of AuthenticationProviderInterface instances + */ + public function getProviders() + { + return $this->providers; + } + + /** + * Sets the providers instances. + * + * @param AuthenticationProviderInterface[] $providers An array of AuthenticationProviderInterface instances + */ + public function setProviders(array $providers) + { + $this->providers = array(); + foreach ($providers as $provider) { + $this->addProvider($provider); + } + } + + /** + * Adds a provider. + * + * @param AuthenticationProviderInterface $provider A AuthenticationProviderInterface instance + */ + public function addProvider(AuthenticationProviderInterface $provider) + { + $this->providers[] = $provider; + } +} |