Fixed security layer for Symfony2 2.1
This commit is contained in:
parent
29ca595387
commit
c23c76883d
|
@ -10,6 +10,7 @@
|
|||
|
||||
namespace Propel\PropelBundle;
|
||||
|
||||
use Symfony\Bridge\Propel1\DependencyInjection\Security\UserProvider\PropelFactory;
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
|
||||
|
@ -60,4 +61,16 @@ class PropelBundle extends Bundle
|
|||
\Propel::initialize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build(ContainerBuilder $container)
|
||||
{
|
||||
parent::build($container);
|
||||
|
||||
if ($container->hasExtension('security')) {
|
||||
$container->getExtension('security')->addUserProviderFactory(new PropelFactory('propel', 'propel.security.user.provider'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
<parameter key="propel.twig.extension.syntax.class">Propel\PropelBundle\Twig\Extension\SyntaxExtension</parameter>
|
||||
<parameter key="form.type_guesser.propel.class">Symfony\Bridge\Propel1\Form\PropelTypeGuesser</parameter>
|
||||
<parameter key="propel.security.acl.provider.model.class">Propel\PropelBundle\Security\Acl\AuditableAclProvider</parameter>
|
||||
<parameter key="propel.security.user.provider.class">Symfony\Bridge\Propel1\Security\User\PropelUserProvider</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
|
@ -50,5 +51,8 @@
|
|||
<argument type="service" id="propel.security.acl.connection" on-invalid="null" />
|
||||
<argument type="service" id="security.acl.cache" on-invalid="null" />
|
||||
</service>
|
||||
|
||||
<service id="propel.security.user.provider" class="%propel.security.user.provider.class%" abstract="true" public="false">
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
||||
|
|
|
@ -1,115 +0,0 @@
|
|||
<?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\Security\User;
|
||||
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
|
||||
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
|
||||
|
||||
/**
|
||||
* ModelUserProvider class.
|
||||
*
|
||||
* Provides easy to use provisioning for Propel model users.
|
||||
*
|
||||
* @author William DURAND <william.durand1@gmail.com>
|
||||
*/
|
||||
class ModelUserProvider implements UserProviderInterface
|
||||
{
|
||||
/**
|
||||
* A Model class name.
|
||||
* @var string
|
||||
*/
|
||||
protected $class;
|
||||
|
||||
/**
|
||||
* A Proxy class name for the model class.
|
||||
* @var string
|
||||
*/
|
||||
protected $proxyClass;
|
||||
|
||||
/**
|
||||
* A Query class name.
|
||||
* @var string
|
||||
*/
|
||||
protected $queryClass;
|
||||
|
||||
/**
|
||||
* A property to use to retrieve the user.
|
||||
* @var string
|
||||
*/
|
||||
protected $property;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*
|
||||
* @param $class The User model class.
|
||||
* @param $proxyClass The Proxy class name for the model class.
|
||||
* @param $property The property to use to retrieve a user.
|
||||
*/
|
||||
public function __construct($class, $proxyClass, $property = null)
|
||||
{
|
||||
$this->class = $class;
|
||||
$this->proxyClass = $proxyClass;
|
||||
$this->queryClass = $class . 'Query';
|
||||
$this->property = $property;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadUserByUsername($username)
|
||||
{
|
||||
$queryClass = $this->queryClass;
|
||||
$query = $queryClass::create();
|
||||
|
||||
if (null !== $this->property) {
|
||||
$filter = 'filterBy' . ucfirst($this->property);
|
||||
$query->$filter($username);
|
||||
} else {
|
||||
$query->filterByUsername($username);
|
||||
}
|
||||
|
||||
$user = $query->findOne();
|
||||
|
||||
if (null === $user) {
|
||||
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
|
||||
}
|
||||
|
||||
$proxyClass = $this->proxyClass;
|
||||
|
||||
return new $proxyClass($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function refreshUser(UserInterface $user)
|
||||
{
|
||||
if (!$user instanceof $this->proxyClass) {
|
||||
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
|
||||
}
|
||||
|
||||
$queryClass = $this->queryClass;
|
||||
$user = $queryClass::create()->findPk($user->getPrimaryKey());
|
||||
$proxyClass = $this->proxyClass;
|
||||
|
||||
return new $proxyClass($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsClass($class)
|
||||
{
|
||||
return $class === $this->proxyClass;
|
||||
}
|
||||
}
|
13
Tests/Fixtures/Model/User.php
Normal file
13
Tests/Fixtures/Model/User.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Propel\PropelBundle\Tests\Fixtures\Model;
|
||||
|
||||
use Propel\PropelBundle\Tests\Fixtures\Model\om\BaseUser;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
class User extends BaseUser implements UserInterface
|
||||
{
|
||||
public function eraseCredentials()
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
|
@ -10,24 +10,21 @@
|
|||
|
||||
namespace Propel\PropelBundle\Tests\Security\User;
|
||||
|
||||
use Propel\PropelBundle\Security\User\ModelUserProvider;
|
||||
|
||||
use Propel\PropelBundle\Tests\Fixtures\Model\User;
|
||||
use Propel\PropelBundle\Tests\TestCase;
|
||||
use Propel\PropelBundle\Tests\Fixtures\UserProxy;
|
||||
use Symfony\Bridge\Propel1\Security\User\PropelUserProvider;
|
||||
|
||||
/**
|
||||
* @author William Durand <william.durand1@gmail.com>
|
||||
*/
|
||||
class ModelUserProviderTest extends TestCase
|
||||
class PropelUserProviderTest extends TestCase
|
||||
{
|
||||
protected $con = null;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->loadPropelQuickBuilder();
|
||||
|
||||
$schema = <<<SCHEMA
|
||||
<database name="users" defaultIdMethod="native">
|
||||
<database name="users" defaultIdMethod="native" namespace="Propel\\PropelBundle\\Tests\\Fixtures\\Model">
|
||||
<table name="user">
|
||||
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
|
||||
<column name="username" type="varchar" size="255" primaryString="true" />
|
||||
|
@ -42,25 +39,26 @@ SCHEMA;
|
|||
|
||||
$builder = new \PropelQuickBuilder();
|
||||
$builder->setSchema($schema);
|
||||
$this->con = $builder->build();
|
||||
$builder->setClassTargets(array('tablemap', 'peer', 'object', 'query', 'peerstub', 'querystub'));
|
||||
$builder->build();
|
||||
}
|
||||
|
||||
public function testRefreshUserGetsUserByPrimaryKey()
|
||||
{
|
||||
$user1 = new \User();
|
||||
$user1 = new User();
|
||||
$user1->setUsername('user1');
|
||||
$user1->save();
|
||||
|
||||
$user2 = new \User();
|
||||
$user2 = new User();
|
||||
$user2->setUsername('user2');
|
||||
$user2->save();
|
||||
|
||||
$provider = new ModelUserProvider('\User', 'Propel\PropelBundle\Tests\Fixtures\UserProxy', 'username');
|
||||
$provider = new PropelUserProvider('Propel\PropelBundle\Tests\Fixtures\Model\User', 'username');
|
||||
|
||||
// try to change the user identity
|
||||
$user1->setUsername('user2');
|
||||
|
||||
$resultUser = $provider->refreshUser(new UserProxy($user1));
|
||||
$this->assertSame($user1, $resultUser->getPropelUser());
|
||||
$resultUser = $provider->refreshUser($user1);
|
||||
$this->assertSame($user1, $resultUser);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue