Update Security/User/ModelUserProvider.php to support a proxy class.

This commit is contained in:
gregoryb 2011-11-04 20:38:17 +01:00 committed by unknown
parent b4724a766a
commit 968dfd014f

View file

@ -1,100 +1,112 @@
<?php <?php
/** /**
* This file is part of the PropelBundle package. * This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
* *
* @license MIT License * @license MIT License
*/ */
namespace Propel\PropelBundle\Security\User; namespace Propel\PropelBundle\Security\User;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
/** /**
* ModelUserProvider class. * ModelUserProvider class.
* *
* Provides easy to use provisioning for Propel model users. * Provides easy to use provisioning for Propel model users.
* *
* @author William DURAND <william.durand1@gmail.com> * @author William DURAND <william.durand1@gmail.com>
*/ */
class ModelUserProvider implements UserProviderInterface class ModelUserProvider implements UserProviderInterface
{ {
/**
* A Model class name. /**
* @var string * A Model class name.
*/ * @var string
protected $class; */
/** protected $class;
* A Query class name.
* @var string /**
*/ * A Proxy class name for the model class.
protected $queryClass; * @var string
/** */
* A property to use to retrieve the user. protected $proxyClass;
* @var string
*/ /**
protected $property; * A Query class name.
* @var string
/** */
* Default constructor protected $queryClass;
*
* @param $class The User model class. /**
* @param $property The property to use to retrieve a user. * A property to use to retrieve the user.
*/ * @var string
public function __construct($class, $property = null) */
{ protected $property;
$this->class = $class;
$this->queryClass = $class.'Query'; /**
$this->property = $property; * Default constructor
} *
* @param $class The User model class.
/** * @param $proxyClass The Proxy class name for the model class.
* {@inheritdoc} * @param $property The property to use to retrieve a user.
*/ */
public function loadUserByUsername($username) public function __construct($class, $proxyClass, $property = null)
{ {
$queryClass = $this->queryClass; $this->class = $class;
$this->proxyClass = $proxyClass;
$query = $queryClass::create(); $this->queryClass = $class . 'Query';
$this->property = $property;
if (null !== $this->property) { }
$filter = 'filterBy' . ucfirst($this->property);
$query->$filter($username); /**
} else { * {@inheritdoc}
$query->filterByUsername($username); */
} public function loadUserByUsername($username)
{
$user = $query->findOne(); $queryClass = $this->queryClass;
if (null === $user) { $query = $queryClass::create();
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
} if (null !== $this->property) {
$filter = 'filterBy' . ucfirst($this->property);
return $user; $query->$filter($username);
} } else {
$query->filterByUsername($username);
/** }
* {@inheritdoc}
*/ $user = $query->findOne();
public function refreshUser(UserInterface $user)
{ if (null === $user) {
if (!$user instanceof $this->class) { throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); }
} $proxyClass = $this->proxyClass;
return new $proxyClass($user);
return $this->loadUserByUsername($user->getUsername()); }
}
/**
/** * {@inheritdoc}
* {@inheritdoc} */
*/ public function refreshUser(UserInterface $user)
public function supportsClass($class) {
{ if (!$user instanceof $this->proxyClass) {
return $class === $this->class; throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
} }
}
return $this->loadUserByUsername($user->getUsername());
}
/**
* {@inheritdoc}
*/
public function supportsClass($class)
{
return $class === $this->proxyClass;
}
}