Permalink
Browse files

[Security] allowed class names to be passed as an argument to Encoder…

…FactoryInterface::getEncoder()
  • Loading branch information...
1 parent 81db3fb commit e13a63c0cbbd938589e2ecdf6af0402651cbb673 @fabpot fabpot committed Jun 18, 2012
View
@@ -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
@@ -30,10 +30,10 @@ public function __construct(array $encoders)
/**
* {@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)) {
@vlastv

vlastv Jun 19, 2012

<?php

require_once __DIR__.'/../vendor/autoload.php';

$user = new Symfony\Component\Security\Core\User\User('test', 'test');

$class = 'Symfony\Component\Security\Core\User\UserInterface';
var_dump(is_subclass_of($user, $class));
var_dump($user instanceof $class);
 (!is_subclass_of($user, $class) && $user != $class)

Always return true

continue;
}
@@ -44,7 +44,7 @@ public function getEncoder(UserInterface $user)
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));
}
/**
@@ -23,9 +23,11 @@
/**
* 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);
}
@@ -13,6 +13,7 @@
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 @@ public function testGetEncoderWithService()
$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', ''));
}
}

0 comments on commit e13a63c

Please sign in to comment.