Skip to content

Commit 33a9925

Browse files
committed
bug #890 [make:user] Keep implementing deprecated username methods (wouterj, weaverryan)
This PR was merged into the 1.0-dev branch. Discussion ---------- [make:user] Keep implementing deprecated username methods This updates the makers for symfony/symfony#41493 (where these methods are reintroduced to the interface for 5.3.1) Commits ------- 2d9312e Fix tests bd55d19 Update src/Resources/skeleton/security/UserProvider.tpl.php c3d4046 Still implement deprecated username method
2 parents 4edd87e + 2d9312e commit 33a9925

9 files changed

+80
-5
lines changed

src/Resources/skeleton/security/UserProvider.tpl.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public function <?= $uses_user_identifier ? 'loadUserByIdentifier($identifier)'
2828
throw new \Exception('TODO: fill in <?= $uses_user_identifier ? 'loadUserByIdentifier()' : 'loadUserByUsername()' ?> inside '.__FILE__);
2929
}
3030

31+
<?php if ($uses_user_identifier) : ?>
32+
/**
33+
* @deprecated since Symfony 5.3, loadUserByIdentifier() is used instead
34+
*/
35+
public function loadUserByUsername($username): UserInterface
36+
{
37+
return $this->loadUserByIdentifier($username);
38+
}
39+
40+
<?php endif ?>
3141
/**
3242
* Refreshes the user after being reloaded from the session.
3343
*

src/Security/UserClassBuilder.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,9 @@ private function addGetUsername(ClassSourceManipulator $manipulator, UserClassCo
9898
);
9999
}
100100

101-
$getterIdentifierName = 'getUsername';
102-
103101
// Check if we're using Symfony 5.3+ - UserInterface::getUsername() was replaced with UserInterface::getUserIdentifier()
104-
if (class_exists(InMemoryUser::class)) {
105-
$getterIdentifierName = 'getUserIdentifier';
106-
}
102+
$symfony53GTE = class_exists(InMemoryUser::class);
103+
$getterIdentifierName = $symfony53GTE ? 'getUserIdentifier' : 'getUsername';
107104

108105
// define getUsername (if it was defined above, this will override)
109106
$manipulator->addAccessorMethod(
@@ -118,6 +115,18 @@ private function addGetUsername(ClassSourceManipulator $manipulator, UserClassCo
118115
],
119116
true
120117
);
118+
119+
if ($symfony53GTE) {
120+
// also add the deprecated getUsername method
121+
$manipulator->addAccessorMethod(
122+
$userClassConfig->getIdentityPropertyName(),
123+
'getUsername',
124+
'string',
125+
false,
126+
['@deprecated since Symfony 5.3, use getUserIdentifier instead'],
127+
true
128+
);
129+
}
121130
}
122131

123132
private function addGetRoles(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig)

tests/Security/fixtures/expected/UserEntityWithEmailAsIdentifier.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public function getUserIdentifier(): string
6161
return (string) $this->email;
6262
}
6363

64+
/**
65+
* @deprecated since Symfony 5.3, use getUserIdentifier instead
66+
*/
67+
public function getUsername(): string
68+
{
69+
return (string) $this->email;
70+
}
71+
6472
/**
6573
* @see UserInterface
6674
*/

tests/Security/fixtures/expected/UserEntityWithPassword.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public function setUserIdentifier(string $userIdentifier): self
5656
return $this;
5757
}
5858

59+
/**
60+
* @deprecated since Symfony 5.3, use getUserIdentifier instead
61+
*/
62+
public function getUsername(): string
63+
{
64+
return (string) $this->userIdentifier;
65+
}
66+
5967
/**
6068
* @see UserInterface
6169
*/

tests/Security/fixtures/expected/UserEntityWithUser_IdentifierAsIdentifier.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public function setUserIdentifier(string $user_identifier): self
5656
return $this;
5757
}
5858

59+
/**
60+
* @deprecated since Symfony 5.3, use getUserIdentifier instead
61+
*/
62+
public function getUsername(): string
63+
{
64+
return (string) $this->user_identifier;
65+
}
66+
5967
/**
6068
* @see UserInterface
6169
*/

tests/Security/fixtures/expected/UserEntityWithoutPassword.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public function setUserIdentifier(string $userIdentifier): self
5050
return $this;
5151
}
5252

53+
/**
54+
* @deprecated since Symfony 5.3, use getUserIdentifier instead
55+
*/
56+
public function getUsername(): string
57+
{
58+
return (string) $this->userIdentifier;
59+
}
60+
5361
/**
5462
* @see UserInterface
5563
*/

tests/Security/fixtures/expected/UserModelWithEmailAsIdentifier.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public function getUserIdentifier(): string
3838
return (string) $this->email;
3939
}
4040

41+
/**
42+
* @deprecated since Symfony 5.3, use getUserIdentifier instead
43+
*/
44+
public function getUsername(): string
45+
{
46+
return (string) $this->email;
47+
}
48+
4149
/**
4250
* @see UserInterface
4351
*/

tests/Security/fixtures/expected/UserModelWithPassword.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public function setUserIdentifier(string $userIdentifier): self
3333
return $this;
3434
}
3535

36+
/**
37+
* @deprecated since Symfony 5.3, use getUserIdentifier instead
38+
*/
39+
public function getUsername(): string
40+
{
41+
return (string) $this->userIdentifier;
42+
}
43+
3644
/**
3745
* @see UserInterface
3846
*/

tests/Security/fixtures/expected/UserModelWithoutPassword.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public function setUserIdentifier(string $userIdentifier): self
2828
return $this;
2929
}
3030

31+
/**
32+
* @deprecated since Symfony 5.3, use getUserIdentifier instead
33+
*/
34+
public function getUsername(): string
35+
{
36+
return (string) $this->userIdentifier;
37+
}
38+
3139
/**
3240
* @see UserInterface
3341
*/

0 commit comments

Comments
 (0)