diff --git a/src/Resources/skeleton/security/UserProvider.tpl.php b/src/Resources/skeleton/security/UserProvider.tpl.php index 3d9cf8cc7..9f2bf8cf5 100644 --- a/src/Resources/skeleton/security/UserProvider.tpl.php +++ b/src/Resources/skeleton/security/UserProvider.tpl.php @@ -28,6 +28,16 @@ public function inside '.__FILE__); } + + /** + * @deprecated since Symfony 5.3, loadUserByIdentifier() is used instead + */ + public function loadUserByUsername($username): UserInterface + { + return $this->loadUserByIdentifier($username); + } + + /** * Refreshes the user after being reloaded from the session. * diff --git a/src/Security/UserClassBuilder.php b/src/Security/UserClassBuilder.php index f338e9922..0f41ea6ac 100644 --- a/src/Security/UserClassBuilder.php +++ b/src/Security/UserClassBuilder.php @@ -98,12 +98,9 @@ private function addGetUsername(ClassSourceManipulator $manipulator, UserClassCo ); } - $getterIdentifierName = 'getUsername'; - // Check if we're using Symfony 5.3+ - UserInterface::getUsername() was replaced with UserInterface::getUserIdentifier() - if (class_exists(InMemoryUser::class)) { - $getterIdentifierName = 'getUserIdentifier'; - } + $symfony53GTE = class_exists(InMemoryUser::class); + $getterIdentifierName = $symfony53GTE ? 'getUserIdentifier' : 'getUsername'; // define getUsername (if it was defined above, this will override) $manipulator->addAccessorMethod( @@ -118,6 +115,18 @@ private function addGetUsername(ClassSourceManipulator $manipulator, UserClassCo ], true ); + + if ($symfony53GTE) { + // also add the deprecated getUsername method + $manipulator->addAccessorMethod( + $userClassConfig->getIdentityPropertyName(), + 'getUsername', + 'string', + false, + ['@deprecated since Symfony 5.3, use getUserIdentifier instead'], + true + ); + } } private function addGetRoles(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig) diff --git a/tests/Security/fixtures/expected/UserEntityWithEmailAsIdentifier.php b/tests/Security/fixtures/expected/UserEntityWithEmailAsIdentifier.php index a3e115373..cadbb6483 100644 --- a/tests/Security/fixtures/expected/UserEntityWithEmailAsIdentifier.php +++ b/tests/Security/fixtures/expected/UserEntityWithEmailAsIdentifier.php @@ -61,6 +61,14 @@ public function getUserIdentifier(): string return (string) $this->email; } + /** + * @deprecated since Symfony 5.3, use getUserIdentifier instead + */ + public function getUsername(): string + { + return (string) $this->email; + } + /** * @see UserInterface */ diff --git a/tests/Security/fixtures/expected/UserEntityWithPassword.php b/tests/Security/fixtures/expected/UserEntityWithPassword.php index fa68d000f..b7c75eebe 100644 --- a/tests/Security/fixtures/expected/UserEntityWithPassword.php +++ b/tests/Security/fixtures/expected/UserEntityWithPassword.php @@ -56,6 +56,14 @@ public function setUserIdentifier(string $userIdentifier): self return $this; } + /** + * @deprecated since Symfony 5.3, use getUserIdentifier instead + */ + public function getUsername(): string + { + return (string) $this->userIdentifier; + } + /** * @see UserInterface */ diff --git a/tests/Security/fixtures/expected/UserEntityWithUser_IdentifierAsIdentifier.php b/tests/Security/fixtures/expected/UserEntityWithUser_IdentifierAsIdentifier.php index 967163acf..08a2ee1f1 100644 --- a/tests/Security/fixtures/expected/UserEntityWithUser_IdentifierAsIdentifier.php +++ b/tests/Security/fixtures/expected/UserEntityWithUser_IdentifierAsIdentifier.php @@ -56,6 +56,14 @@ public function setUserIdentifier(string $user_identifier): self return $this; } + /** + * @deprecated since Symfony 5.3, use getUserIdentifier instead + */ + public function getUsername(): string + { + return (string) $this->user_identifier; + } + /** * @see UserInterface */ diff --git a/tests/Security/fixtures/expected/UserEntityWithoutPassword.php b/tests/Security/fixtures/expected/UserEntityWithoutPassword.php index e0ce41659..102307f42 100644 --- a/tests/Security/fixtures/expected/UserEntityWithoutPassword.php +++ b/tests/Security/fixtures/expected/UserEntityWithoutPassword.php @@ -50,6 +50,14 @@ public function setUserIdentifier(string $userIdentifier): self return $this; } + /** + * @deprecated since Symfony 5.3, use getUserIdentifier instead + */ + public function getUsername(): string + { + return (string) $this->userIdentifier; + } + /** * @see UserInterface */ diff --git a/tests/Security/fixtures/expected/UserModelWithEmailAsIdentifier.php b/tests/Security/fixtures/expected/UserModelWithEmailAsIdentifier.php index fbe959f22..ef3d1ae21 100644 --- a/tests/Security/fixtures/expected/UserModelWithEmailAsIdentifier.php +++ b/tests/Security/fixtures/expected/UserModelWithEmailAsIdentifier.php @@ -38,6 +38,14 @@ public function getUserIdentifier(): string return (string) $this->email; } + /** + * @deprecated since Symfony 5.3, use getUserIdentifier instead + */ + public function getUsername(): string + { + return (string) $this->email; + } + /** * @see UserInterface */ diff --git a/tests/Security/fixtures/expected/UserModelWithPassword.php b/tests/Security/fixtures/expected/UserModelWithPassword.php index 25e536add..357d20966 100644 --- a/tests/Security/fixtures/expected/UserModelWithPassword.php +++ b/tests/Security/fixtures/expected/UserModelWithPassword.php @@ -33,6 +33,14 @@ public function setUserIdentifier(string $userIdentifier): self return $this; } + /** + * @deprecated since Symfony 5.3, use getUserIdentifier instead + */ + public function getUsername(): string + { + return (string) $this->userIdentifier; + } + /** * @see UserInterface */ diff --git a/tests/Security/fixtures/expected/UserModelWithoutPassword.php b/tests/Security/fixtures/expected/UserModelWithoutPassword.php index e06802686..4d012f2c3 100644 --- a/tests/Security/fixtures/expected/UserModelWithoutPassword.php +++ b/tests/Security/fixtures/expected/UserModelWithoutPassword.php @@ -28,6 +28,14 @@ public function setUserIdentifier(string $userIdentifier): self return $this; } + /** + * @deprecated since Symfony 5.3, use getUserIdentifier instead + */ + public function getUsername(): string + { + return (string) $this->userIdentifier; + } + /** * @see UserInterface */