Merge branch '2.7' into 2.8

* 2.7:
  Refactoring EntityUserProvider::__construct() to not do work, cause cache warm error
This commit is contained in:
Christophe Coevoet 2015-12-02 17:06:15 +01:00
commit 8bd62a5cb4
2 changed files with 52 additions and 24 deletions

View File

@ -27,22 +27,17 @@ use Symfony\Component\Security\Core\User\UserInterface;
*/ */
class EntityUserProvider implements UserProviderInterface class EntityUserProvider implements UserProviderInterface
{ {
private $registry;
private $managerName;
private $classOrAlias;
private $class; private $class;
private $repository;
private $property; private $property;
private $metadata;
public function __construct(ManagerRegistry $registry, $class, $property = null, $managerName = null) public function __construct(ManagerRegistry $registry, $classOrAlias, $property = null, $managerName = null)
{ {
$em = $registry->getManager($managerName); $this->registry = $registry;
$this->class = $class; $this->managerName = $managerName;
$this->metadata = $em->getClassMetadata($class); $this->classOrAlias = $classOrAlias;
if (false !== strpos($this->class, ':')) {
$this->class = $this->metadata->getName();
}
$this->repository = $em->getRepository($class);
$this->property = $property; $this->property = $property;
} }
@ -51,18 +46,19 @@ class EntityUserProvider implements UserProviderInterface
*/ */
public function loadUserByUsername($username) public function loadUserByUsername($username)
{ {
$repository = $this->getRepository();
if (null !== $this->property) { if (null !== $this->property) {
$user = $this->repository->findOneBy(array($this->property => $username)); $user = $repository->findOneBy(array($this->property => $username));
} else { } else {
if (!$this->repository instanceof UserLoaderInterface) { if (!$repository instanceof UserLoaderInterface) {
if (!$this->repository instanceof UserProviderInterface) { if (!$repository instanceof UserProviderInterface) {
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.', get_class($this->repository))); throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.', get_class($repository)));
} }
@trigger_error('Implementing loadUserByUsername from Symfony\Component\Security\Core\User\UserProviderInterface is deprecated since version 2.8 and will be removed in 3.0. Implement the Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface instead.', E_USER_DEPRECATED); @trigger_error('Implementing loadUserByUsername from Symfony\Component\Security\Core\User\UserProviderInterface is deprecated since version 2.8 and will be removed in 3.0. Implement the Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface instead.', E_USER_DEPRECATED);
} }
$user = $this->repository->loadUserByUsername($username); $user = $repository->loadUserByUsername($username);
} }
if (null === $user) { if (null === $user) {
@ -77,18 +73,20 @@ class EntityUserProvider implements UserProviderInterface
*/ */
public function refreshUser(UserInterface $user) public function refreshUser(UserInterface $user)
{ {
if (!$user instanceof $this->class) { $class = $this->getClass();
if (!$user instanceof $class) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
} }
if ($this->repository instanceof UserProviderInterface) { $repository = $this->getRepository();
$refreshedUser = $this->repository->refreshUser($user); if ($repository instanceof UserProviderInterface) {
$refreshedUser = $repository->refreshUser($user);
} else { } else {
// The user must be reloaded via the primary key as all other data // The user must be reloaded via the primary key as all other data
// might have changed without proper persistence in the database. // might have changed without proper persistence in the database.
// That's the case when the user has been changed by a form with // That's the case when the user has been changed by a form with
// validation errors. // validation errors.
if (!$id = $this->metadata->getIdentifierValues($user)) { if (!$id = $this->getClassMetadata()->getIdentifierValues($user)) {
throw new \InvalidArgumentException('You cannot refresh a user '. throw new \InvalidArgumentException('You cannot refresh a user '.
'from the EntityUserProvider that does not contain an identifier. '. 'from the EntityUserProvider that does not contain an identifier. '.
'The user object has to be serialized with its own identifier '. 'The user object has to be serialized with its own identifier '.
@ -96,7 +94,7 @@ class EntityUserProvider implements UserProviderInterface
); );
} }
$refreshedUser = $this->repository->find($id); $refreshedUser = $repository->find($id);
if (null === $refreshedUser) { if (null === $refreshedUser) {
throw new UsernameNotFoundException(sprintf('User with id %s not found', json_encode($id))); throw new UsernameNotFoundException(sprintf('User with id %s not found', json_encode($id)));
} }
@ -110,6 +108,36 @@ class EntityUserProvider implements UserProviderInterface
*/ */
public function supportsClass($class) public function supportsClass($class)
{ {
return $class === $this->class || is_subclass_of($class, $this->class); return $class === $this->getClass() || is_subclass_of($class, $this->getClass());
}
private function getObjectManager()
{
return $this->registry->getManager($this->managerName);
}
private function getRepository()
{
return $this->getObjectManager()->getRepository($this->classOrAlias);
}
private function getClass()
{
if (null === $this->class) {
$class = $this->classOrAlias;
if (false !== strpos($class, ':')) {
$class = $this->getClassMetadata()->getName();
}
$this->class = $class;
}
return $this->class;
}
private function getClassMetadata()
{
return $this->getObjectManager()->getClassMetadata($this->classOrAlias);
} }
} }

View File

@ -125,7 +125,7 @@ class EntityUserProviderTest extends \PHPUnit_Framework_TestCase
private function getManager($em, $name = null) private function getManager($em, $name = null)
{ {
$manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); $manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$manager->expects($this->once()) $manager->expects($this->any())
->method('getManager') ->method('getManager')
->with($this->equalTo($name)) ->with($this->equalTo($name))
->will($this->returnValue($em)); ->will($this->returnValue($em));