fixed security user reloading when the user has been changed via a form with validation errors

This commit is contained in:
William DURAND 2011-12-14 14:41:36 +01:00
parent a7ed277e78
commit 31ee48bf10
4 changed files with 151 additions and 1 deletions

View file

@ -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);
}
/**

View file

@ -0,0 +1,71 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Tests\Fixtures;
use Symfony\Component\Security\Core\User\UserInterface;
class UserProxy implements UserInterface
{
protected $user;
public function __construct($user)
{
$this->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;
}
}

View file

@ -0,0 +1,66 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Tests\Security\User;
use Propel\PropelBundle\Security\User\ModelUserProvider;
use Propel\PropelBundle\Tests\TestCase;
use Propel\PropelBundle\Tests\Fixtures\UserProxy;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class ModelUserProviderTest extends TestCase
{
protected $con = null;
public function setUp()
{
$this->loadPropelQuickBuilder();
$schema = <<<SCHEMA
<database name="users" defaultIdMethod="native">
<table name="user">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
<column name="username" type="varchar" size="255" primaryString="true" />
<column name="algorithm" type="varchar" size="50" />
<column name="salt" type="varchar" size="255" />
<column name="password" type="varchar" size="255" />
<column name="expires_at" type="timestamp" />
<column name="roles" type="array" />
</table>
</database>
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());
}
}

View file

@ -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';
}
}