Skip to content

Commit acbf86e

Browse files
wouterjchalasr
authored andcommitted
[Security] Rename UserInterface::getUsername() to getUserIdentifier()
1 parent b8c1485 commit acbf86e

File tree

7 files changed

+47
-25
lines changed

7 files changed

+47
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.3
55
---
66

7+
* Deprecate `UserLoaderInterface::loadUserByUsername()` in favor of `UserLoaderInterface::loadUserByIdentifier()
78
* Deprecate `DoctrineTestHelper` and `TestRepositoryFactory`
89
* [BC BREAK] Remove `UuidV*Generator` classes
910
* Add `UuidGenerator`

Security/RememberMe/DoctrineTokenProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ public function createNewToken(PersistentTokenInterface $token)
119119
.' VALUES (:class, :username, :series, :value, :lastUsed)';
120120
$paramValues = [
121121
'class' => $token->getClass(),
122-
'username' => $token->getUsername(),
122+
// @deprecated since 5.3, change to $token->getUserIdentifier() in 6.0
123+
'username' => method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername(),
123124
'series' => $token->getSeries(),
124125
'value' => $token->getTokenValue(),
125126
'lastUsed' => $token->getLastUsed(),

Security/User/EntityUserProvider.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Doctrine\Persistence\ObjectManager;
1717
use Doctrine\Persistence\ObjectRepository;
1818
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
19-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
19+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
2020
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
2121
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
2222
use Symfony\Component\Security\Core\User\UserInterface;
@@ -50,21 +50,35 @@ public function __construct(ManagerRegistry $registry, string $classOrAlias, str
5050
* {@inheritdoc}
5151
*/
5252
public function loadUserByUsername(string $username)
53+
{
54+
trigger_deprecation('symfony/doctrine-bridge', '5.3', 'Method "%s()" is deprecated, use loadUserByIdentifier() instead.', __METHOD__);
55+
56+
return $this->loadUserByIdentifier($username);
57+
}
58+
59+
public function loadUserByIdentifier(string $identifier): UserInterface
5360
{
5461
$repository = $this->getRepository();
5562
if (null !== $this->property) {
56-
$user = $repository->findOneBy([$this->property => $username]);
63+
$user = $repository->findOneBy([$this->property => $identifier]);
5764
} else {
5865
if (!$repository instanceof UserLoaderInterface) {
5966
throw new \InvalidArgumentException(sprintf('You must either make the "%s" entity Doctrine Repository ("%s") implement "Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface" or set the "property" option in the corresponding entity provider configuration.', $this->classOrAlias, get_debug_type($repository)));
6067
}
6168

62-
$user = $repository->loadUserByUsername($username);
69+
// @deprecated since 5.3, change to $repository->loadUserByIdentifier() in 6.0
70+
if (method_exists($repository, 'loadUserByIdentifier')) {
71+
$user = $repository->loadUserByIdentifier($identifier);
72+
} else {
73+
trigger_deprecation('symfony/doctrine-bridge', '5.3', 'Not implementing method "loadUserByIdentifier()" in user loader "%s" is deprecated. This method will replace "loadUserByUsername()" in Symfony 6.0.', get_debug_type($repository));
74+
75+
$user = $repository->loadUserByUsername($identifier);
76+
}
6377
}
6478

6579
if (null === $user) {
66-
$e = new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
67-
$e->setUsername($username);
80+
$e = new UserNotFoundException(sprintf('User "%s" not found.', $identifier));
81+
$e->setUserIdentifier($identifier);
6882

6983
throw $e;
7084
}
@@ -96,8 +110,8 @@ public function refreshUser(UserInterface $user)
96110

97111
$refreshedUser = $repository->find($id);
98112
if (null === $refreshedUser) {
99-
$e = new UsernameNotFoundException('User with id '.json_encode($id).' not found.');
100-
$e->setUsername(json_encode($id));
113+
$e = new UserNotFoundException('User with id '.json_encode($id).' not found.');
114+
$e->setUserIdentifier(json_encode($id));
101115

102116
throw $e;
103117
}

Security/User/UserLoaderInterface.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,11 @@
2222
*
2323
* @see UserInterface
2424
*
25+
* @method UserInterface|null loadUserByIdentifier(string $identifier) loads the user for the given user identifier (e.g. username or email).
26+
* This method must return null if the user is not found.
27+
*
2528
* @author Michal Trojanowski <[email protected]>
2629
*/
2730
interface UserLoaderInterface
2831
{
29-
/**
30-
* Loads the user for the given username.
31-
*
32-
* This method must return null if the user is not found.
33-
*
34-
* @return UserInterface|null
35-
*/
36-
public function loadUserByUsername(string $username);
3732
}

Tests/Fixtures/BaseUser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public function getUsername(): string
3737
{
3838
return $this->username;
3939
}
40+
41+
public function getUserIdentifier(): string
42+
{
43+
return $this->username;
44+
}
4045
}

Tests/Fixtures/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public function getUsername(): string
5353
return $this->name;
5454
}
5555

56+
public function getUserIdentifier(): string
57+
{
58+
return $this->name;
59+
}
60+
5661
public function eraseCredentials()
5762
{
5863
}

Tests/Security/User/EntityUserProviderTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
2222
use Symfony\Bridge\Doctrine\Tests\DoctrineTestHelper;
2323
use Symfony\Bridge\Doctrine\Tests\Fixtures\User;
24-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
24+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
2525
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
2626
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
2727
use Symfony\Component\Security\Core\User\UserInterface;
@@ -60,7 +60,7 @@ public function testLoadUserByUsername()
6060

6161
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
6262

63-
$this->assertSame($user, $provider->loadUserByUsername('user1'));
63+
$this->assertSame($user, $provider->loadUserByIdentifier('user1'));
6464
}
6565

6666
public function testLoadUserByUsernameWithUserLoaderRepositoryAndWithoutProperty()
@@ -70,7 +70,7 @@ public function testLoadUserByUsernameWithUserLoaderRepositoryAndWithoutProperty
7070
$repository = $this->createMock(UserLoaderRepository::class);
7171
$repository
7272
->expects($this->once())
73-
->method('loadUserByUsername')
73+
->method('loadUserByIdentifier')
7474
->with('user1')
7575
->willReturn($user);
7676

@@ -82,7 +82,7 @@ public function testLoadUserByUsernameWithUserLoaderRepositoryAndWithoutProperty
8282
->willReturn($repository);
8383

8484
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
85-
$this->assertSame($user, $provider->loadUserByUsername('user1'));
85+
$this->assertSame($user, $provider->loadUserByIdentifier('user1'));
8686
}
8787

8888
public function testLoadUserByUsernameWithNonUserLoaderRepositoryAndWithoutProperty()
@@ -98,7 +98,7 @@ public function testLoadUserByUsernameWithNonUserLoaderRepositoryAndWithoutPrope
9898
$em->flush();
9999

100100
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
101-
$provider->loadUserByUsername('user1');
101+
$provider->loadUserByIdentifier('user1');
102102
}
103103

104104
public function testRefreshUserRequiresId()
@@ -126,7 +126,7 @@ public function testRefreshInvalidUser()
126126
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
127127

128128
$user2 = new User(1, 2, 'user2');
129-
$this->expectException(UsernameNotFoundException::class);
129+
$this->expectException(UserNotFoundException::class);
130130
$this->expectExceptionMessage('User with id {"id1":1,"id2":2} not found');
131131

132132
$provider->refreshUser($user2);
@@ -153,7 +153,7 @@ public function testLoadUserByUserNameShouldLoadUserWhenProperInterfaceProvided(
153153
{
154154
$repository = $this->createMock(UserLoaderRepository::class);
155155
$repository->expects($this->once())
156-
->method('loadUserByUsername')
156+
->method('loadUserByIdentifier')
157157
->with('name')
158158
->willReturn(
159159
$this->createMock(UserInterface::class)
@@ -164,7 +164,7 @@ public function testLoadUserByUserNameShouldLoadUserWhenProperInterfaceProvided(
164164
'Symfony\Bridge\Doctrine\Tests\Fixtures\User'
165165
);
166166

167-
$provider->loadUserByUsername('name');
167+
$provider->loadUserByIdentifier('name');
168168
}
169169

170170
public function testLoadUserByUserNameShouldDeclineInvalidInterface()
@@ -177,7 +177,7 @@ public function testLoadUserByUserNameShouldDeclineInvalidInterface()
177177
'Symfony\Bridge\Doctrine\Tests\Fixtures\User'
178178
);
179179

180-
$provider->loadUserByUsername('name');
180+
$provider->loadUserByIdentifier('name');
181181
}
182182

183183
public function testPasswordUpgrades()
@@ -231,6 +231,7 @@ private function createSchema($em)
231231

232232
abstract class UserLoaderRepository implements ObjectRepository, UserLoaderInterface
233233
{
234+
abstract public function loadUserByIdentifier(string $identifier): ?UserInterface;
234235
}
235236

236237
abstract class PasswordUpgraderRepository implements ObjectRepository, PasswordUpgraderInterface

0 commit comments

Comments
 (0)