Skip to content

Refactoring EntityUserProvider::__construct() to not do work, cause cache warm error #16772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 50 additions & 22 deletions src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,17 @@
*/
class EntityUserProvider implements UserProviderInterface
{
private $registry;
private $managerName;
private $classOrAlias;
private $class;
private $repository;
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->class = $class;
$this->metadata = $em->getClassMetadata($class);

if (false !== strpos($this->class, ':')) {
$this->class = $this->metadata->getName();
}

$this->repository = $em->getRepository($class);
$this->registry = $registry;
$this->managerName = $managerName;
$this->classOrAlias = $classOrAlias;
$this->property = $property;
}

Expand All @@ -51,14 +46,15 @@ public function __construct(ManagerRegistry $registry, $class, $property = null,
*/
public function loadUserByUsername($username)
{
$repository = $this->getRepository();
if (null !== $this->property) {
$user = $this->repository->findOneBy(array($this->property => $username));
$user = $repository->findOneBy(array($this->property => $username));
} else {
if (!$this->repository instanceof UserProviderInterface) {
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($this->repository)));
if (!$repository instanceof UserProviderInterface) {
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($repository)));
}

$user = $this->repository->loadUserByUsername($username);
$user = $repository->loadUserByUsername($username);
}

if (null === $user) {
Expand All @@ -73,26 +69,28 @@ public function loadUserByUsername($username)
*/
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)));
}

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

$refreshedUser = $this->repository->find($id);
$refreshedUser = $repository->find($id);
if (null === $refreshedUser) {
throw new UsernameNotFoundException(sprintf('User with id %s not found', json_encode($id)));
}
Expand All @@ -106,6 +104,36 @@ public function refreshUser(UserInterface $user)
*/
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testSupportProxy()
private function getManager($em, $name = null)
{
$manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$manager->expects($this->once())
$manager->expects($this->any())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't really care how many times getManager() is called anyways. And now, it's called multiple times in some tests, and zero times in others.

->method('getManager')
->with($this->equalTo($name))
->will($this->returnValue($em));
Expand Down