Skip to content

Commit 9c1fbb8

Browse files
committed
[DoctrineBridge] fixed the refreshing of the user for invalid users
1 parent 257351a commit 9c1fbb8

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public function refreshUser(UserInterface $user)
7575
if (!$user instanceof $this->class) {
7676
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
7777
}
78-
7978

8079
// The user must be reloaded via the primary key as all other data
8180
// might have changed without proper persistence in the database.
@@ -89,7 +88,11 @@ public function refreshUser(UserInterface $user)
8988
);
9089
}
9190

92-
return $this->repository->find($id);
91+
if (null === $refreshedUser = $this->repository->find($id)) {
92+
throw new UsernameNotFoundException(sprintf('User with id %s not found', json_encode($id)));
93+
}
94+
95+
return $refreshedUser;
9396
}
9497

9598
/**

tests/Symfony/Tests/Bridge/Doctrine/Security/User/EntityUserProviderTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,41 @@ public function testRefreshUserGetsUserByPrimaryKey()
4040

4141
$this->assertSame($user1, $provider->refreshUser($user1));
4242
}
43-
43+
4444
public function testRefreshUserRequiresId()
4545
{
4646
$em = $this->createTestEntityManager();
47-
47+
4848
$user1 = new CompositeIdentEntity(null, null, 'user1');
4949
$provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
50-
50+
5151
$this->setExpectedException(
5252
'InvalidArgumentException',
5353
'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'
5454
);
5555
$provider->refreshUser($user1);
5656
}
5757

58+
public function testRefreshInvalidUser()
59+
{
60+
$em = $this->createTestEntityManager();
61+
$this->createSchema($em);
62+
63+
$user1 = new CompositeIdentEntity(1, 1, 'user1');
64+
65+
$em->persist($user1);
66+
$em->flush();
67+
68+
$provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
69+
70+
$user2 = new CompositeIdentEntity(1, 2, 'user2');
71+
$this->setExpectedException(
72+
'Symfony\Component\Security\Core\Exception\UsernameNotFoundException',
73+
'User with id {"id1":1,"id2":2} not found'
74+
);
75+
$provider->refreshUser($user2);
76+
}
77+
5878
private function createSchema($em)
5979
{
6080
$schemaTool = new SchemaTool($em);

0 commit comments

Comments
 (0)