8000 [Security] Rename PersistentTokenInterface::getUsername() to getUserI… · symfony/symfony@a0a9bfe · GitHub
[go: up one dir, main page]

Skip to content

Commit a0a9bfe

Browse files
committed
[Security] Rename PersistentTokenInterface::getUsername() to getUserIdentifier()
1 parent 45fd033 commit a0a9bfe

File tree

8 files changed

+43
-16
lines changed

8 files changed

+43
-16
lines changed

UPGRADE-5.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ Security
172172
* Deprecate `TokenInterface::getUsername()` in favor of `TokenInterface::getUserIdentifier()`
173173
* Deprecate `UserProviderInterface::loadUserByUsername()` in favor of `UserProviderInterface::loadUserByIdentifier()`
174174
* Deprecate `UsernameNotFoundException` in favor of `UserNotFoundException` and `getUsername()`/`setUsername()` in favor of `getUserIdentifier()`/`setUserIdentifier()`
175+
* Deprecate `PersistentTokenInterface::getUsername()` in favor of `PersistentTokenInterface::getUserIdentifier()`
175176
* Deprecate calling `PasswordUpgraderInterface::upgradePassword()` with a `UserInterface` instance that does not implement `PasswordAuthenticatedUserInterface`
176177
* Deprecate calling methods `hashPassword()`, `isPasswordValid()` and `needsRehash()` on `UserPasswordHasherInterface` with a `UserInterface` instance that does not implement `PasswordAuthenticatedUserInterface`
177178
* Deprecate all classes in the `Core\Encoder\` sub-namespace, use the `PasswordHasher` component instead

UPGRADE-6.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ Security
264264
* Remove `TokenInterface::getUsername()` in favor of `TokenInterface::getUserIdentifier()`
265265
* Remove `UserProviderInterface::loadUserByUsername()` in favor of `UserProviderInterface::loadUserByIdentifier()`
266266
* Remove `UsernameNotFoundException` in favor of `UserNotFoundException` and `getUsername()`/`setUsername()` in favor of `getUserIdentifier()`/`setUserIdentifier()`
267+
* Remove `PersistentTokenInterface::getUsername()` in favor of `PersistentTokenInterface::getUserIdentifier()`
267268
* Calling `PasswordUpgraderInterface::upgradePassword()` with a `UserInterface` instance that
268269
does not implement `PasswordAuthenticatedUserInterface` now throws a `\TypeError`.
269270
* Calling methods `hashPassword()`, `isPasswordValid()` and `needsRehash()` on `UserPasswordHasherInterface`

src/Symfony/Component/Security/CHANGELOG.md

Expand all lines: src/Symfony/Component/Security/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 `PersistentTokenInterface::getUsername()` in favor of `PersistentTokenInterface::getUserIdentifier()`
78
* Deprecate `UsernameNotFoundException` in favor of `UserNotFoundException` and `getUsername()`/`setUsername()` in favor of `getUserIdentifier()`/`setUserIdentifier()`
89
* Deprecate `UserProviderInterface::loadUserByUsername()` in favor of `UserProviderInterface::loadUserByIdentifier()`
910
* Deprecate `TokenInterface::getUsername()` in favor of `TokenInterface::getUserIdentifier()`

src/Symfony/Component/Security/Core/Authentication/RememberMe/InMemoryTokenProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function updateToken(string $series, string $tokenValue, \DateTime $lastU
4545

4646
$token = new PersistentToken(
4747
$this->tokens[$series]->getClass(),
48-
$this->tokens[$series]->getUsername(),
48+
method_exists($this->tokens[$series], 'getUserIdentifier') ? $this->tokens[$series]->getUserIdentifier() : $this->tokens[$series]->getUsername(),
4949
$series,
5050
$tokenValue,
5151
$lastUsed

src/Symfony/Component/Security/Core/Authentication/RememberMe/PersistentToken.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
final class PersistentToken implements PersistentTokenInterface
2020
{
2121
private $class;
22-
private $username;
22+
private $userIdentifier;
2323
private $series;
2424
private $tokenValue;
2525
private $lastUsed;
2626

27-
public function __construct(string $class, string $username, string $series, string $tokenValue, \DateTime $lastUsed)
27+
public function __construct(string $class, string $userIdentifier, string $series, string $tokenValue, \DateTime $lastUsed)
2828
{
2929
if (empty($class)) {
3030
throw new \InvalidArgumentException('$class must not be empty.');
3131
}
32-
if ('' === $username) {
33-
throw new \InvalidArgumentException('$username must not be empty.');
32+
if ('' === $userIdentifier) {
33+
throw new \InvalidArgumentException('$userIdentifier must not be empty.');
3434
}
3535
if (empty($series)) {
3636
throw new \InvalidArgumentException('$series must not be empty.');
@@ -40,7 +40,7 @@ public function __construct(string $class, string $username, string $series, str
4040
}
4141

4242
$this->class = $class;
43-
$this->username = $username;
43+
$this->userIdentifier = $userIdentifier;
4444
$this->series = $series;
4545
$this->tokenValue = $tokenValue;
4646
$this->lastUsed = $lastUsed;
@@ -59,7 +59,14 @@ public function getClass(): string
5959
*/
6060
public function getUsername(): string
6161
{
62-
return $this->username;
62+
trigger_deprecation('symfony/security-core', '5.3', 'Method "%s()" is deprecated, use getUserIdentifier() instead.', __METHOD__);
63+
64+
return $this->userIdentifier;
65+
}
66+
67+
public function getUserIdentifier(): string
68+
{
69+
return $this->userIdentifier;
6370
}
6471

6572
/**

src/Symfony/Component/Security/Core/Authentication/RememberMe/PersistentTokenInterface.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* Interface to be implemented by persistent token classes (such as
1616
* Doctrine entities representing a remember-me token).
1717
*
18+
* @method string getUserIdentifier() returns the identifier used to authenticate (e.g. their e-mailaddress or username)
19+
*
1820
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
1921
*/
2022
interface PersistentTokenInterface
@@ -26,13 +28,6 @@ interface PersistentTokenInterface
2628
*/
2729
public function getClass();
2830

29-
/**
30-
* Returns the username.
31-
*
32-
* @return string
33-
*/
34-
public function getUsername();
35-
3631
/**
3732
* Returns the series.
3833
*

src/Symfony/Component/Security/Core/Tests/Authentication/RememberMe/PersistentTokenTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,33 @@
1212
namespace Symfony\Component\Security\Core\Tests\Authentication\RememberMe;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
1617

1718
class PersistentTokenTest extends TestCase
1819
{
20+
use ExpectDeprecationTrait;
21+
1922
public function testConstructor()
2023
{
2124
$lastUsed = new \DateTime();
2225
$token = new PersistentToken('fooclass', 'fooname', 'fooseries', 'footokenvalue', $lastUsed);
2326

2427
$this->assertEquals('fooclass', $token->getClass());
25-
$this->assertEquals('fooname', $token->getUsername());
28+
$this->assertEquals('fooname', $token->getUserIdentifier());
2629
$this->assertEquals('fooseries', $token->getSeries());
2730
$this->assertEquals('footokenvalue', $token->getTokenValue());
2831
$this->assertSame($lastUsed, $token->getLastUsed());
2932
}
33+
34+
/**
35+
* @group legacy
36+
*/
37+
public function testLegacyGetUsername()
38+
{
39+
$token = new PersistentToken('fooclass', 'fooname', 'fooseries', 'footokenvalue', new \DateTime());
40+
41+
$this->expectDeprecation('Since symfony/security-core 5.3: Method "Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken::getUsername()" is deprecated, use getUserIdentifier() instead.');
42+
$this->assertEquals('fooname', $token->getUsername());
43+
}
3044
}

src/Symfony/Component/Security/Http/RememberMe/PersistentTokenBasedRememberMeServices.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,15 @@ protected function processAutoLoginCookie(array $cookieParts, Request $request)
9494
);
9595

9696
$userProvider = $this->getUserProvider($persistentToken->getClass());
97-
$userIdentifier = $persistentToken->getUsername();
97+
// @deprecated since 5.3, change to $persistentToken->getUserIdentifier() in 6.0
98+
if (method_exists($persistentToken, 'getUserIdentifier')) {
99+
$userIdentifier = $persistentToken->getUserIdentifier();
100+
} else {
101+
trigger_deprecation('symfony/security-core', '5.3', 'Not implementing method "getUserIdentifier()" in persistent token "%s" is deprecated. This method will replace "loadUserByUsername()" in Symfony 6.0.', get_debug_type($persistentToken));
102+
103+
$userIdentifier = $persistentToken->getUsername();
104+
}
105+
98106
// @deprecated since 5.3, change to $userProvider->loadUserByIdentifier() in 6.0
99107
if (method_exists($userProvider, 'loadUserByIdentifier')) {
100108
return $userProvider->loadUserByIdentifier($userIdentifier);

0 commit comments

Comments
 (0)
0