diff options
author | Fabien Potencier <fabien.potencier@gmail.com> | 2012-06-18 08:12:50 +0200 |
---|---|---|
committer | Fabien Potencier <fabien.potencier@gmail.com> | 2012-06-18 08:12:50 +0200 |
commit | e13a63c0cbbd938589e2ecdf6af0402651cbb673 (patch) | |
tree | 84af98c95faf22555a8591efeb28f42fe5eb53f4 | |
parent | 81db3fb8af37dd884580433a647df23d6de5347e (diff) | |
download | symfony-security-e13a63c0cbbd938589e2ecdf6af0402651cbb673.zip symfony-security-e13a63c0cbbd938589e2ecdf6af0402651cbb673.tar.gz symfony-security-e13a63c0cbbd938589e2ecdf6af0402651cbb673.tar.bz2 |
[Security] allowed class names to be passed as an argument to EncoderFactoryInterface::getEncoder()
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | Core/Encoder/EncoderFactory.php | 6 | ||||
-rw-r--r-- | Core/Encoder/EncoderFactoryInterface.php | 8 | ||||
-rw-r--r-- | Tests/Core/Encoder/EncoderFactoryTest.php | 19 |
4 files changed, 28 insertions, 6 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a555f1e..36ca15b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 2.1.0 ----- + * EncoderFactoryInterface::getEncoder() can now also take a class name as an argument * allow switching to the user that is already impersonated * added support for the remember_me parameter in the query * added AccessMapInterface diff --git a/Core/Encoder/EncoderFactory.php b/Core/Encoder/EncoderFactory.php index 738706a..866c809 100644 --- a/Core/Encoder/EncoderFactory.php +++ b/Core/Encoder/EncoderFactory.php @@ -30,10 +30,10 @@ class EncoderFactory implements EncoderFactoryInterface /** * {@inheritDoc} */ - public function getEncoder(UserInterface $user) + public function getEncoder($user) { foreach ($this->encoders as $class => $encoder) { - if (!$user instanceof $class) { + if ((is_object($user) && !$user instanceof $class) || (!is_subclass_of($user, $class) && $user != $class)) { continue; } @@ -44,7 +44,7 @@ class EncoderFactory implements EncoderFactoryInterface return $this->encoders[$class]; } - throw new \RuntimeException(sprintf('No encoder has been configured for account "%s".', get_class($user))); + throw new \RuntimeException(sprintf('No encoder has been configured for account "%s".', is_object($class) ? get_class($user) : $class)); } /** diff --git a/Core/Encoder/EncoderFactoryInterface.php b/Core/Encoder/EncoderFactoryInterface.php index 3ae07e6..125e57b 100644 --- a/Core/Encoder/EncoderFactoryInterface.php +++ b/Core/Encoder/EncoderFactoryInterface.php @@ -23,9 +23,11 @@ interface EncoderFactoryInterface /** * Returns the password encoder to use for the given account. * - * @param UserInterface $user + * @param UserInterface|string $user A UserInterface instance of a class name * - * @return PasswordEncoderInterface never null + * @return PasswordEncoderInterface + * + * @throws \RuntimeException when no password encoder could be found for the user */ - function getEncoder(UserInterface $user); + function getEncoder($user); } diff --git a/Tests/Core/Encoder/EncoderFactoryTest.php b/Tests/Core/Encoder/EncoderFactoryTest.php index a060809..d0bcb3e 100644 --- a/Tests/Core/Encoder/EncoderFactoryTest.php +++ b/Tests/Core/Encoder/EncoderFactoryTest.php @@ -13,6 +13,7 @@ namespace Symfony\Component\Security\Tests\Core\Encoder; use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder; use Symfony\Component\Security\Core\Encoder\EncoderFactory; +use Symfony\Component\Security\Core\User\User; class EncoderFactoryTest extends \PHPUnit_Framework_TestCase { @@ -37,7 +38,25 @@ class EncoderFactoryTest extends \PHPUnit_Framework_TestCase $encoder = $factory->getEncoder($this->getMock('Symfony\Component\Security\Core\User\UserInterface')); $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); + $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); + + $encoder = $factory->getEncoder(new User('user', 'pass')); + $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); + $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); + } + + public function testGetEncoderWithClassName() + { + $factory = new EncoderFactory(array( + 'Symfony\Component\Security\Core\User\UserInterface' => new MessageDigestPasswordEncoder('sha1'), + )); + $encoder = $factory->getEncoder('Symfony\Component\Security\Core\User\UserInterface'); + $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); + $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); + + $encoder = $factory->getEncoder('Symfony\Component\Security\Core\User\User'); + $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); } } |