From 31ee48bf107aac508b986841b93d1cca04ac4407 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Wed, 14 Dec 2011 14:41:36 +0100 Subject: [PATCH] fixed security user reloading when the user has been changed via a form with validation errors --- Security/User/ModelUserProvider.php | 6 +- Tests/Fixtures/UserProxy.php | 71 +++++++++++++++++++ Tests/Security/User/ModelUserProviderTest.php | 66 +++++++++++++++++ Tests/TestCase.php | 9 +++ 4 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 Tests/Fixtures/UserProxy.php create mode 100644 Tests/Security/User/ModelUserProviderTest.php diff --git a/Security/User/ModelUserProvider.php b/Security/User/ModelUserProvider.php index a12fc63..847b159 100644 --- a/Security/User/ModelUserProvider.php +++ b/Security/User/ModelUserProvider.php @@ -98,7 +98,11 @@ class ModelUserProvider implements UserProviderInterface throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); } - return $this->loadUserByUsername($user->getUsername()); + $queryClass = $this->queryClass; + $user = $queryClass::create()->findPk($user->getPrimaryKey()); + $proxyClass = $this->proxyClass; + + return new $proxyClass($user); } /** diff --git a/Tests/Fixtures/UserProxy.php b/Tests/Fixtures/UserProxy.php new file mode 100644 index 0000000..e24830c --- /dev/null +++ b/Tests/Fixtures/UserProxy.php @@ -0,0 +1,71 @@ +user = $user; + } + + public function getRoles() + { + $roles = $this->getPropelUser()->getRoles(); + } + + public function getPassword() + { + return $this->getPropelUser()->getPassword(); + } + + public function getSalt() + { + return $this->getPropelUser()->getSalt(); + } + + public function getUsername() + { + return $this->getPropelUser()->getUsername(); + } + + public function eraseCredentials() + { + } + + public function equals(UserInterface $user) + { + return $this->getPropelUser()->equals($user); + } + + public function getAlgorithm() + { + return $this->getPropelUser()->getAlgorithm(); + } + + public function __call($method, $arguments) + { + if (is_callable(array($this->user, $method))) { + return call_user_func_array(array($this->user, $method), $arguments); + } + + throw new \BadMethodCallException('Can\'t call method '.$method); + } + + public function getPropelUser() + { + return $this->user; + } +} diff --git a/Tests/Security/User/ModelUserProviderTest.php b/Tests/Security/User/ModelUserProviderTest.php new file mode 100644 index 0000000..4baa988 --- /dev/null +++ b/Tests/Security/User/ModelUserProviderTest.php @@ -0,0 +1,66 @@ + + */ +class ModelUserProviderTest extends TestCase +{ + protected $con = null; + + public function setUp() + { + $this->loadPropelQuickBuilder(); + + $schema = << + + + + + + + + +
+ +SCHEMA; + + $builder = new \PropelQuickBuilder(); + $builder->setSchema($schema); + $this->con = $builder->build(); + } + + public function testRefreshUserGetsUserByPrimaryKey() + { + $user1 = new \User(); + $user1->setUsername('user1'); + $user1->save(); + + $user2 = new \User(); + $user2->setUsername('user2'); + $user2->save(); + + $provider = new ModelUserProvider('\User', 'Propel\PropelBundle\Tests\Fixtures\UserProxy', 'username'); + + // try to change the user identity + $user1->setUsername('user2'); + + $resultUser = $provider->refreshUser(new UserProxy($user1)); + $this->assertSame($user1, $resultUser->getPropelUser()); + } +} diff --git a/Tests/TestCase.php b/Tests/TestCase.php index 656e85b..0987916 100644 --- a/Tests/TestCase.php +++ b/Tests/TestCase.php @@ -35,4 +35,13 @@ class TestCase extends \PHPUnit_Framework_TestCase 'kernel.debug' => false, ))); } + + protected function loadPropelQuickBuilder() + { + require_once __DIR__.'/../vendor/propel/runtime/lib/Propel.php'; + require_once __DIR__.'/../vendor/propel/runtime/lib/adapter/DBAdapter.php'; + require_once __DIR__.'/../vendor/propel/runtime/lib/adapter/DBSqlite.php'; + require_once __DIR__.'/../vendor/propel/runtime/lib/connection/PropelPDO.php'; + require_once __DIR__.'/../vendor/propel/generator/lib/util/PropelQuickBuilder.php'; + } }