Skip to content

Commit 9248199

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: Refactoring EntityUserProvider::__construct() to not do work, cause cache warm error
2 parents 4a4ad58 + 0b632bf commit 9248199

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

Security/User/EntityUserProvider.php

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,17 @@
2727
*/
2828
class EntityUserProvider implements UserProviderInterface
2929
{
30+
private $registry;
31+
private $managerName;
32+
private $classOrAlias;
3033
private $class;
31-
private $repository;
3234
private $property;
33-
private $metadata;
3435

35-
public function __construct(ManagerRegistry $registry, $class, $property = null, $managerName = null)
36+
public function __construct(ManagerRegistry $registry, $classOrAlias, $property = null, $managerName = null)
3637
{
37-
$em = $registry->getManager($managerName);
38-
$this->class = $class;
39-
$this->metadata = $em->getClassMetadata($class);
40-
41-
if (false !== strpos($this->class, ':')) {
42-
$this->class = $this->metadata->getName();
43-
}
44-
45-
$this->repository = $em->getRepository($class);
38+
$this->registry = $registry;
39+
$this->managerName = $managerName;
40+
$this->classOrAlias = $classOrAlias;
4641
$this->property = $property;
4742
}
4843

@@ -51,18 +46,19 @@ public function __construct(ManagerRegistry $registry, $class, $property = null,
5146
*/
5247
public function loadUserByUsername($username)
5348
{
49+
$repository = $this->getRepository();
5450
if (null !== $this->property) {
55-
$user = $this->repository->findOneBy(array($this->property => $username));
51+
$user = $repository->findOneBy(array($this->property => $username));
5652
} else {
57-
if (!$this->repository instanceof UserLoaderInterface) {
58-
if (!$this->repository instanceof UserProviderInterface) {
59-
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.', get_class($this->repository)));
53+
if (!$repository instanceof UserLoaderInterface) {
54+
if (!$repository instanceof UserProviderInterface) {
55+
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.', get_class($repository)));
6056
}
6157

6258
@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);
6359
}
6460

65-
$user = $this->repository->loadUserByUsername($username);
61+
$user = $repository->loadUserByUsername($username);
6662
}
6763

6864
if (null === $user) {
@@ -77,26 +73,28 @@ public function loadUserByUsername($username)
7773
*/
7874
public function refreshUser(UserInterface $user)
7975
{
80-
if (!$user instanceof $this->class) {
76+
$class = $this->getClass();
77+
if (!$user instanceof $class) {
8178
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
8279
}
8380

84-
if ($this->repository instanceof UserProviderInterface) {
85-
$refreshedUser = $this->repository->refreshUser($user);
81+
$repository = $this->getRepository();
82+
if ($repository instanceof UserProviderInterface) {
83+
$refreshedUser = $repository->refreshUser($user);
8684
} else {
8785
// The user must be reloaded via the primary key as all other data
8886
// might have changed without proper persistence in the database.
8987
// That's the case when the user has been changed by a form with
9088
// validation errors.
91-
if (!$id = $this->metadata->getIdentifierValues($user)) {
89+
if (!$id = $this->getClassMetadata()->getIdentifierValues($user)) {
9290
throw new \InvalidArgumentException('You cannot refresh a user '.
9391
'from the EntityUserProvider that does not contain an identifier. '.
9492
'The user object has to be serialized with its own identifier '.
9593
'mapped by Doctrine.'
9694
);
9795
}
9896

99-
$refreshedUser = $this->repository->find($id);
97+
$refreshedUser = $repository->find($id);
10098
if (null === $refreshedUser) {
10199
throw new UsernameNotFoundException(sprintf('User with id %s not found', json_encode($id)));
102100
}
@@ -110,6 +108,36 @@ public function refreshUser(UserInterface $user)
110108
*/
111109
public function supportsClass($class)
112110
{
113-
return $class === $this->class || is_subclass_of($class, $this->class);
111+
return $class === $this->getClass() || is_subclass_of($class, $this->getClass());
112+
}
113+
114+
private function getObjectManager()
115+
{
116+
return $this->registry->getManager($this->managerName);
117+
}
118+
119+
private function getRepository()
120+
{
121+
return $this->getObjectManager()->getRepository($this->classOrAlias);
122+
}
123+
124+
private function getClass()
125+
{
126+
if (null === $this->class) {
127+
$class = $this->classOrAlias;
128+
129+
if (false !== strpos($class, ':')) {
130+
$class = $this->getClassMetadata()->getName();
131+
}
132+
133+
$this->class = $class;
134+
}
135+
136+
return $this->class;
137+
}
138+
139+
private function getClassMetadata()
140+
{
141+
return $this->getObjectManager()->getClassMetadata($this->classOrAlias);
114142
}
115143
}

Tests/Security/User/EntityUserProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function testLoadUserByUserNameShouldDeclineInvalidInterface()
125125
private function getManager($em, $name = null)
126126
{
127127
$manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
128-
$manager->expects($this->once())
128+
$manager->expects($this->any())
129129
->method('getManager')
130130
->with($this->equalTo($name))
131131
->will($this->returnValue($em));

0 commit comments

Comments
 (0)