From 1d83924235162c12c8eb08427037346891464243 Mon Sep 17 00:00:00 2001 From: Jesse Rushlow Date: Mon, 19 Apr 2021 10:18:19 -0400 Subject: [PATCH] [make:user] implement getUserIdentifier if required --- src/Maker/MakeUser.php | 2 + .../skeleton/security/UserProvider.tpl.php | 16 +- src/Security/UserClassBuilder.php | 12 +- tests/Security/UserClassBuilderTest.php | 170 ++++++++++++++---- ...hp => UserEntityWithEmailAsIdentifier.php} | 2 +- ...assword.php => UserEntityWithPassword.php} | 10 +- ...EntityWithUser_IdentifierAsIdentifier.php} | 10 +- ...word.php => UserEntityWithoutPassword.php} | 35 ++-- ...php => UserModelWithEmailAsIdentifier.php} | 2 +- ...Password.php => UserModelWithPassword.php} | 10 +- .../expected/UserModelWithoutPassword.php | 78 ++++++++ .../legacy/UserEntityEmailWithPassword.php | 116 ------------ .../UserEntityNoPassword.php} | 0 .../UserEntityWithPassword.php} | 0 .../UserModelNoPassword.php} | 0 .../UserModelWithPassword.php} | 0 .../UserEntityGetUsername.php} | 3 +- .../UserModelGetUsername.php} | 3 +- .../UserModelWithEmailAsUsername.php} | 21 +-- 19 files changed, 268 insertions(+), 222 deletions(-) rename tests/Security/fixtures/expected/{UserEntityEmailWithPassword.php => UserEntityWithEmailAsIdentifier.php} (97%) rename tests/Security/fixtures/expected/{UserEntityUser_nameWithPassword.php => UserEntityWithPassword.php} (89%) rename tests/Security/fixtures/expected/{UserEntityUsernameWithPassword.php => UserEntityWithUser_IdentifierAsIdentifier.php} (89%) rename tests/Security/fixtures/expected/{legacy/UserEntityUser_nameWithPassword.php => UserEntityWithoutPassword.php} (67%) rename tests/Security/fixtures/expected/{UserModelEmailWithPassword.php => UserModelWithEmailAsIdentifier.php} (97%) rename tests/Security/fixtures/expected/{UserModelUsernameWithPassword.php => UserModelWithPassword.php} (87%) create mode 100644 tests/Security/fixtures/expected/UserModelWithoutPassword.php delete mode 100644 tests/Security/fixtures/expected/legacy/UserEntityEmailWithPassword.php rename tests/Security/fixtures/expected/{legacy/UserEntityUsernameNoPassword.php => legacy_get_password/UserEntityNoPassword.php} (100%) rename tests/Security/fixtures/expected/{legacy/UserEntityUsernameWithPassword.php => legacy_get_password/UserEntityWithPassword.php} (100%) rename tests/Security/fixtures/expected/{legacy/UserModelUsernameNoPassword.php => legacy_get_password/UserModelNoPassword.php} (100%) rename tests/Security/fixtures/expected/{legacy/UserModelUsernameWithPassword.php => legacy_get_password/UserModelWithPassword.php} (100%) rename tests/Security/fixtures/expected/{UserEntityUsernameNoPassword.php => legacy_get_username/UserEntityGetUsername.php} (93%) rename tests/Security/fixtures/expected/{UserModelUsernameNoPassword.php => legacy_get_username/UserModelGetUsername.php} (92%) rename tests/Security/fixtures/expected/{legacy/UserModelEmailWithPassword.php => legacy_get_username/UserModelWithEmailAsUsername.php} (75%) diff --git a/src/Maker/MakeUser.php b/src/Maker/MakeUser.php index 9be177a1b..302471cac 100644 --- a/src/Maker/MakeUser.php +++ b/src/Maker/MakeUser.php @@ -33,6 +33,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Security\Core\Encoder\Argon2iPasswordEncoder; use Symfony\Component\Security\Core\Encoder\NativePasswordEncoder; +use Symfony\Component\Security\Core\Exception\UserNotFoundException; use Symfony\Component\Security\Core\User\PasswordUpgraderInterface; use Symfony\Component\Yaml\Yaml; @@ -171,6 +172,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $userClassConfiguration->getUserProviderClass(), 'security/UserProvider.tpl.php', [ + 'uses_user_identifier' => class_exists(UserNotFoundException::class), 'user_short_name' => $userClassNameDetails->getShortName(), ] ); diff --git a/src/Resources/skeleton/security/UserProvider.tpl.php b/src/Resources/skeleton/security/UserProvider.tpl.php index 6b9f52096..3d9cf8cc7 100644 --- a/src/Resources/skeleton/security/UserProvider.tpl.php +++ b/src/Resources/skeleton/security/UserProvider.tpl.php @@ -3,7 +3,7 @@ namespace ; use Symfony\Component\Security\Core\Exception\UnsupportedUserException; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; +use Symfony\Component\Security\Core\Exception\; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserProviderInterface; @@ -17,17 +17,15 @@ class implements UserProviderInterface if the user is not found */ - public function loadUserByUsername($username) + public function : UserInterface { - // Load a User object from your data source or throw UsernameNotFoundException. - // The $username argument may not actually be a username: - // it is whatever value is being returned by the getUsername() + // Load a User object from your data source or throw . + // The argument may not actually be a username: + // it is whatever value is being returned by the () // method in your User class. - throw new \Exception('TODO: fill in loadUserByUsername() inside '.__FILE__); + throw new \Exception('TODO: fill in inside '.__FILE__); } /** diff --git a/src/Security/UserClassBuilder.php b/src/Security/UserClassBuilder.php index 52ec3945d..f338e9922 100644 --- a/src/Security/UserClassBuilder.php +++ b/src/Security/UserClassBuilder.php @@ -14,6 +14,7 @@ use PhpParser\Node; use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator; use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\Security\Core\User\InMemoryUser; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; @@ -67,7 +68,7 @@ private function addPasswordImplementation(ClassSourceManipulator $manipulator, $this->addGetPassword($manipulator, $userClassConfig); } - private function addGetUsername(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig) + private function addGetUsername(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig): void { if ($userClassConfig->isEntity()) { // add entity property @@ -97,10 +98,17 @@ 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'; + } + // define getUsername (if it was defined above, this will override) $manipulator->addAccessorMethod( $userClassConfig->getIdentityPropertyName(), - 'getUsername', + $getterIdentifierName, 'string', false, [ diff --git a/tests/Security/UserClassBuilderTest.php b/tests/Security/UserClassBuilderTest.php index be1da4f6a..4c632be3d 100644 --- a/tests/Security/UserClassBuilderTest.php +++ b/tests/Security/UserClassBuilderTest.php @@ -16,75 +16,175 @@ use Symfony\Bundle\MakerBundle\Security\UserClassConfiguration; use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; +use Symfony\Component\Security\Core\User\User; class UserClassBuilderTest extends TestCase { /** * @dataProvider getUserInterfaceTests */ - public function testAddUserInterfaceImplementation(UserClassConfiguration $userClassConfig, string $expectedFilename) + public function testAddUserInterfaceImplementation(UserClassConfiguration $userClassConfig, string $expectedFilename): void { - $sourceFilename = __DIR__.'/fixtures/source/'.($userClassConfig->isEntity() ? 'UserEntity.php' : 'UserModel.php'); + if (!interface_exists(PasswordAuthenticatedUserInterface::class)) { + self::markTestSkipped('Requires Symfony >= 5.3'); + } - $manipulator = new ClassSourceManipulator( - file_get_contents($sourceFilename), - true - ); + $manipulator = $this->getClassSourceManipulator($userClassConfig); $classBuilder = new UserClassBuilder(); $classBuilder->addUserInterfaceImplementation($manipulator, $userClassConfig); - $expectedPath = __DIR__.'/fixtures/expected'; + $expectedPath = $this->getExpectedPath($expectedFilename); - // Can be removed when < Symfony 6 support is dropped. - if (!interface_exists(PasswordAuthenticatedUserInterface::class)) { - $expectedPath = sprintf('%s/legacy', $expectedPath); - } + self::assertStringEqualsFile($expectedPath, $manipulator->getSourceCode()); + } - $expectedPath = sprintf('%s/%s', $expectedPath, $expectedFilename); + public function getUserInterfaceTests(): \Generator + { + yield 'entity_with_email_as_identifier' => [ + new UserClassConfiguration(true, 'email', true), + 'UserEntityWithEmailAsIdentifier.php', + ]; - if (!file_exists($expectedPath)) { - throw new \Exception(sprintf('Expected file missing: "%s"', $expectedPath)); + yield 'entity_with_password' => [ + new UserClassConfiguration(true, 'userIdentifier', true), + 'UserEntityWithPassword.php', + ]; + + yield 'entity_with_user_identifier_as_identifier' => [ + new UserClassConfiguration(true, 'user_identifier', true), + 'UserEntityWithUser_IdentifierAsIdentifier.php', + ]; + + yield 'entity_without_password' => [ + new UserClassConfiguration(true, 'userIdentifier', false), + 'UserEntityWithoutPassword.php', + ]; + + yield 'model_with_email_as_identifier' => [ + new UserClassConfiguration(false, 'email', true), + 'UserModelWithEmailAsIdentifier.php', + ]; + + yield 'model_with_password' => [ + new UserClassConfiguration(false, 'userIdentifier', true), + 'UserModelWithPassword.php', + ]; + + yield 'model_without_password' => [ + new UserClassConfiguration(false, 'userIdentifier', false), + 'UserModelWithoutPassword.php', + ]; + } + + /** + * Covers Symfony <= 5.2 UserInterface::getUsername(). + * + * In Symfony 5.3, getUsername was replaced with getUserIdentifier() + * + * @dataProvider legacyUserInterfaceGetUsernameDataProvider + */ + public function testLegacyUserInterfaceGetUsername(UserClassConfiguration $userClassConfig, string $expectedFilename): void + { + if (method_exists(User::class, 'getUserIdentifier')) { + self::markTestSkipped(); } - $this->assertSame(file_get_contents($expectedPath), $manipulator->getSourceCode()); + $manipulator = $this->getClassSourceManipulator($userClassConfig); + + $classBuilder = new UserClassBuilder(); + $classBuilder->addUserInterfaceImplementation($manipulator, $userClassConfig); + + $expectedPath = $this->getExpectedPath($expectedFilename, 'legacy_get_username'); + + self::assertStringEqualsFile($expectedPath, $manipulator->getSourceCode()); } - public function getUserInterfaceTests() + public function legacyUserInterfaceGetUsernameDataProvider(): \Generator { - yield 'entity_email_password' => [ - new UserClassConfiguration(true, 'email', true), - 'UserEntityEmailWithPassword.php', + yield 'entity_with_get_username' => [ + new UserClassConfiguration(true, 'username', false), + 'UserEntityGetUsername.php', ]; - yield 'entity_username_password' => [ - new UserClassConfiguration(true, 'username', true), - 'UserEntityUsernameWithPassword.php', + yield 'model_with_get_username' => [ + new UserClassConfiguration(false, 'username', false), + 'UserModelGetUsername.php', ]; - yield 'entity_user_name_password' => [ - new UserClassConfiguration(true, 'user_name', true), - 'UserEntityUser_nameWithPassword.php', + yield 'model_with_email_as_username' => [ + new UserClassConfiguration(false, 'email', false), + 'UserModelWithEmailAsUsername.php', ]; + } - yield 'entity_username_no_password' => [ - new UserClassConfiguration(true, 'username', false), - 'UserEntityUsernameNoPassword.php', + /** + * Covers Symfony <= 5.2 UserInterface::getPassword(). + * + * In Symfony 5.3, getPassword was moved from UserInterface::class to the + * new PasswordAuthenticatedUserInterface::class. + * + * @dataProvider legacyUserInterfaceGetPasswordDataProvider + */ + public function testLegacyUserInterfaceGetPassword(UserClassConfiguration $userClassConfig, string $expectedFilename): void + { + if (interface_exists(PasswordAuthenticatedUserInterface::class)) { + self::markTestSkipped(); + } + + $manipulator = $this->getClassSourceManipulator($userClassConfig); + + $classBuilder = new UserClassBuilder(); + $classBuilder->addUserInterfaceImplementation($manipulator, $userClassConfig); + + $expectedPath = $this->getExpectedPath($expectedFilename, 'legacy_get_password'); + + self::assertStringEqualsFile($expectedPath, $manipulator->getSourceCode()); + } + + public function legacyUserInterfaceGetPasswordDataProvider(): \Generator + { + yield 'entity_with_password' => [ + new UserClassConfiguration(true, 'username', true), + 'UserEntityWithPassword.php', ]; - yield 'model_email_password' => [ - new UserClassConfiguration(false, 'email', true), - 'UserModelEmailWithPassword.php', + yield 'entity_without_password' => [ + new UserClassConfiguration(true, 'username', false), + 'UserEntityNoPassword.php', ]; - yield 'model_username_password' => [ + yield 'model_with_password' => [ new UserClassConfiguration(false, 'username', true), - 'UserModelUsernameWithPassword.php', + 'UserModelWithPassword.php', ]; - yield 'model_username_no_password' => [ + yield 'model_without_password' => [ new UserClassConfiguration(false, 'username', false), - 'UserModelUsernameNoPassword.php', + 'UserModelNoPassword.php', ]; } + + private function getClassSourceManipulator(UserClassConfiguration $userClassConfiguration): ClassSourceManipulator + { + $sourceFilename = __DIR__.'/fixtures/source/'.($userClassConfiguration->isEntity() ? 'UserEntity.php' : 'UserModel.php'); + + return new ClassSourceManipulator( + file_get_contents($sourceFilename), + true + ); + } + + private function getExpectedPath(string $expectedFilename, string $subDirectory = null): string + { + $basePath = __DIR__.'/fixtures/expected'; + + $expectedPath = null === $subDirectory ? sprintf('%s/%s', $basePath, $expectedFilename) : sprintf('%s/%s/%s', $basePath, $subDirectory, $expectedFilename); + + if (!file_exists($expectedPath)) { + throw new \Exception(sprintf('Expected file missing: "%s"', $expectedPath)); + } + + return $expectedPath; + } } diff --git a/tests/Security/fixtures/expected/UserEntityEmailWithPassword.php b/tests/Security/fixtures/expected/UserEntityWithEmailAsIdentifier.php similarity index 97% rename from tests/Security/fixtures/expected/UserEntityEmailWithPassword.php rename to tests/Security/fixtures/expected/UserEntityWithEmailAsIdentifier.php index 6610169c8..a3e115373 100644 --- a/tests/Security/fixtures/expected/UserEntityEmailWithPassword.php +++ b/tests/Security/fixtures/expected/UserEntityWithEmailAsIdentifier.php @@ -56,7 +56,7 @@ public function setEmail(string $email): self * * @see UserInterface */ - public function getUsername(): string + public function getUserIdentifier(): string { return (string) $this->email; } diff --git a/tests/Security/fixtures/expected/UserEntityUser_nameWithPassword.php b/tests/Security/fixtures/expected/UserEntityWithPassword.php similarity index 89% rename from tests/Security/fixtures/expected/UserEntityUser_nameWithPassword.php rename to tests/Security/fixtures/expected/UserEntityWithPassword.php index 76363b0f2..fa68d000f 100644 --- a/tests/Security/fixtures/expected/UserEntityUser_nameWithPassword.php +++ b/tests/Security/fixtures/expected/UserEntityWithPassword.php @@ -21,7 +21,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface /** * @ORM\Column(type="string", length=180, unique=true) */ - private $user_name; + private $userIdentifier; /** * @ORM\Column(type="json") @@ -44,14 +44,14 @@ public function getId(): ?int * * @see UserInterface */ - public function getUsername(): string + public function getUserIdentifier(): string { - return (string) $this->user_name; + return (string) $this->userIdentifier; } - public function setUserName(string $user_name): self + public function setUserIdentifier(string $userIdentifier): self { - $this->user_name = $user_name; + $this->userIdentifier = $userIdentifier; return $this; } diff --git a/tests/Security/fixtures/expected/UserEntityUsernameWithPassword.php b/tests/Security/fixtures/expected/UserEntityWithUser_IdentifierAsIdentifier.php similarity index 89% rename from tests/Security/fixtures/expected/UserEntityUsernameWithPassword.php rename to tests/Security/fixtures/expected/UserEntityWithUser_IdentifierAsIdentifier.php index 9eb23b21e..967163acf 100644 --- a/tests/Security/fixtures/expected/UserEntityUsernameWithPassword.php +++ b/tests/Security/fixtures/expected/UserEntityWithUser_IdentifierAsIdentifier.php @@ -21,7 +21,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface /** * @ORM\Column(type="string", length=180, unique=true) */ - private $username; + private $user_identifier; /** * @ORM\Column(type="json") @@ -44,14 +44,14 @@ public function getId(): ?int * * @see UserInterface */ - public function getUsername(): string + public function getUserIdentifier(): string { - return (string) $this->username; + return (string) $this->user_identifier; } - public function setUsername(string $username): self + public function setUserIdentifier(string $user_identifier): self { - $this->username = $username; + $this->user_identifier = $user_identifier; return $this; } diff --git a/tests/Security/fixtures/expected/legacy/UserEntityUser_nameWithPassword.php b/tests/Security/fixtures/expected/UserEntityWithoutPassword.php similarity index 67% rename from tests/Security/fixtures/expected/legacy/UserEntityUser_nameWithPassword.php rename to tests/Security/fixtures/expected/UserEntityWithoutPassword.php index 7f74765cb..e0ce41659 100644 --- a/tests/Security/fixtures/expected/legacy/UserEntityUser_nameWithPassword.php +++ b/tests/Security/fixtures/expected/UserEntityWithoutPassword.php @@ -3,6 +3,7 @@ namespace App\Entity; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; /** @@ -20,19 +21,13 @@ class User implements UserInterface /** * @ORM\Column(type="string", length=180, unique=true) */ - private $user_name; + private $userIdentifier; /** * @ORM\Column(type="json") */ private $roles = []; - /** - * @var string The hashed password - * @ORM\Column(type="string") - */ - private $password; - public function getId(): ?int { return $this->id; @@ -43,14 +38,14 @@ public function getId(): ?int * * @see UserInterface */ - public function getUsername(): string + public function getUserIdentifier(): string { - return (string) $this->user_name; + return (string) $this->userIdentifier; } - public function setUserName(string $user_name): self + public function setUserIdentifier(string $userIdentifier): self { - $this->user_name = $user_name; + $this->userIdentifier = $userIdentifier; return $this; } @@ -75,23 +70,17 @@ public function setRoles(array $roles): self } /** - * @see UserInterface + * This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords. + * + * @see PasswordAuthenticatedUserInterface */ - public function getPassword(): string + public function getPassword(): ?string { - return $this->password; - } - - public function setPassword(string $password): self - { - $this->password = $password; - - return $this; + return null; } /** - * Returning a salt is only needed, if you are not using a modern - * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml. + * This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords. * * @see UserInterface */ diff --git a/tests/Security/fixtures/expected/UserModelEmailWithPassword.php b/tests/Security/fixtures/expected/UserModelWithEmailAsIdentifier.php similarity index 97% rename from tests/Security/fixtures/expected/UserModelEmailWithPassword.php rename to tests/Security/fixtures/expected/UserModelWithEmailAsIdentifier.php index 2d34e2bd7..fbe959f22 100644 --- a/tests/Security/fixtures/expected/UserModelEmailWithPassword.php +++ b/tests/Security/fixtures/expected/UserModelWithEmailAsIdentifier.php @@ -33,7 +33,7 @@ public function setEmail(string $email): self * * @see UserInterface */ - public function getUsername(): string + public function getUserIdentifier(): string { return (string) $this->email; } diff --git a/tests/Security/fixtures/expected/UserModelUsernameWithPassword.php b/tests/Security/fixtures/expected/UserModelWithPassword.php similarity index 87% rename from tests/Security/fixtures/expected/UserModelUsernameWithPassword.php rename to tests/Security/fixtures/expected/UserModelWithPassword.php index 976a5d2db..25e536add 100644 --- a/tests/Security/fixtures/expected/UserModelUsernameWithPassword.php +++ b/tests/Security/fixtures/expected/UserModelWithPassword.php @@ -7,7 +7,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface { - private $username; + private $userIdentifier; private $roles = []; @@ -21,14 +21,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface * * @see UserInterface */ - public function getUsername(): string + public function getUserIdentifier(): string { - return (string) $this->username; + return (string) $this->userIdentifier; } - public function setUsername(string $username): self + public function setUserIdentifier(string $userIdentifier): self { - $this->username = $username; + $this->userIdentifier = $userIdentifier; return $this; } diff --git a/tests/Security/fixtures/expected/UserModelWithoutPassword.php b/tests/Security/fixtures/expected/UserModelWithoutPassword.php new file mode 100644 index 000000000..e06802686 --- /dev/null +++ b/tests/Security/fixtures/expected/UserModelWithoutPassword.php @@ -0,0 +1,78 @@ +userIdentifier; + } + + public function setUserIdentifier(string $userIdentifier): self + { + $this->userIdentifier = $userIdentifier; + + return $this; + } + + /** + * @see UserInterface + */ + public function getRoles(): array + { + $roles = $this->roles; + // guarantee every user at least has ROLE_USER + $roles[] = 'ROLE_USER'; + + return array_unique($roles); + } + + public function setRoles(array $roles): self + { + $this->roles = $roles; + + return $this; + } + + /** + * This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords. + * + * @see PasswordAuthenticatedUserInterface + */ + public function getPassword(): ?string + { + return null; + } + + /** + * This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords. + * + * @see UserInterface + */ + public function getSalt(): ?string + { + return null; + } + + /** + * @see UserInterface + */ + public function eraseCredentials() + { + // If you store any temporary, sensitive data on the user, clear it here + // $this->plainPassword = null; + } +} diff --git a/tests/Security/fixtures/expected/legacy/UserEntityEmailWithPassword.php b/tests/Security/fixtures/expected/legacy/UserEntityEmailWithPassword.php deleted file mode 100644 index 7e412605d..000000000 --- a/tests/Security/fixtures/expected/legacy/UserEntityEmailWithPassword.php +++ /dev/null @@ -1,116 +0,0 @@ -id; - } - - public function getEmail(): ?string - { - return $this->email; - } - - public function setEmail(string $email): self - { - $this->email = $email; - - return $this; - } - - /** - * A visual identifier that represents this user. - * - * @see UserInterface - */ - public function getUsername(): string - { - return (string) $this->email; - } - - /** - * @see UserInterface - */ - public function getRoles(): array - { - $roles = $this->roles; - // guarantee every user at least has ROLE_USER - $roles[] = 'ROLE_USER'; - - return array_unique($roles); - } - - public function setRoles(array $roles): self - { - $this->roles = $roles; - - return $this; - } - - /** - * @see UserInterface - */ - public function getPassword(): string - { - return $this->password; - } - - public function setPassword(string $password): self - { - $this->password = $password; - - return $this; - } - - /** - * Returning a salt is only needed, if you are not using a modern - * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml. - * - * @see UserInterface - */ - public function getSalt(): ?string - { - return null; - } - - /** - * @see UserInterface - */ - public function eraseCredentials() - { - // If you store any temporary, sensitive data on the user, clear it here - // $this->plainPassword = null; - } -} diff --git a/tests/Security/fixtures/expected/legacy/UserEntityUsernameNoPassword.php b/tests/Security/fixtures/expected/legacy_get_password/UserEntityNoPassword.php similarity index 100% rename from tests/Security/fixtures/expected/legacy/UserEntityUsernameNoPassword.php rename to tests/Security/fixtures/expected/legacy_get_password/UserEntityNoPassword.php diff --git a/tests/Security/fixtures/expected/legacy/UserEntityUsernameWithPassword.php b/tests/Security/fixtures/expected/legacy_get_password/UserEntityWithPassword.php similarity index 100% rename from tests/Security/fixtures/expected/legacy/UserEntityUsernameWithPassword.php rename to tests/Security/fixtures/expected/legacy_get_password/UserEntityWithPassword.php diff --git a/tests/Security/fixtures/expected/legacy/UserModelUsernameNoPassword.php b/tests/Security/fixtures/expected/legacy_get_password/UserModelNoPassword.php similarity index 100% rename from tests/Security/fixtures/expected/legacy/UserModelUsernameNoPassword.php rename to tests/Security/fixtures/expected/legacy_get_password/UserModelNoPassword.php diff --git a/tests/Security/fixtures/expected/legacy/UserModelUsernameWithPassword.php b/tests/Security/fixtures/expected/legacy_get_password/UserModelWithPassword.php similarity index 100% rename from tests/Security/fixtures/expected/legacy/UserModelUsernameWithPassword.php rename to tests/Security/fixtures/expected/legacy_get_password/UserModelWithPassword.php diff --git a/tests/Security/fixtures/expected/UserEntityUsernameNoPassword.php b/tests/Security/fixtures/expected/legacy_get_username/UserEntityGetUsername.php similarity index 93% rename from tests/Security/fixtures/expected/UserEntityUsernameNoPassword.php rename to tests/Security/fixtures/expected/legacy_get_username/UserEntityGetUsername.php index f5d4c83c8..ccbd6846f 100644 --- a/tests/Security/fixtures/expected/UserEntityUsernameNoPassword.php +++ b/tests/Security/fixtures/expected/legacy_get_username/UserEntityGetUsername.php @@ -3,7 +3,6 @@ namespace App\Entity; use Doctrine\ORM\Mapping as ORM; -use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; /** @@ -72,7 +71,7 @@ public function setRoles(array $roles): self /** * This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords. * - * @see PasswordAuthenticatedUserInterface + * @see UserInterface */ public function getPassword(): ?string { diff --git a/tests/Security/fixtures/expected/UserModelUsernameNoPassword.php b/tests/Security/fixtures/expected/legacy_get_username/UserModelGetUsername.php similarity index 92% rename from tests/Security/fixtures/expected/UserModelUsernameNoPassword.php rename to tests/Security/fixtures/expected/legacy_get_username/UserModelGetUsername.php index f09c0b658..81d9820a6 100644 --- a/tests/Security/fixtures/expected/UserModelUsernameNoPassword.php +++ b/tests/Security/fixtures/expected/legacy_get_username/UserModelGetUsername.php @@ -2,7 +2,6 @@ namespace App\Security; -use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; class User implements UserInterface @@ -50,7 +49,7 @@ public function setRoles(array $roles): self /** * This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords. * - * @see PasswordAuthenticatedUserInterface + * @see UserInterface */ public function getPassword(): ?string { diff --git a/tests/Security/fixtures/expected/legacy/UserModelEmailWithPassword.php b/tests/Security/fixtures/expected/legacy_get_username/UserModelWithEmailAsUsername.php similarity index 75% rename from tests/Security/fixtures/expected/legacy/UserModelEmailWithPassword.php rename to tests/Security/fixtures/expected/legacy_get_username/UserModelWithEmailAsUsername.php index e6ca12abf..f17e53c10 100644 --- a/tests/Security/fixtures/expected/legacy/UserModelEmailWithPassword.php +++ b/tests/Security/fixtures/expected/legacy_get_username/UserModelWithEmailAsUsername.php @@ -10,11 +10,6 @@ class User implements UserInterface private $roles = []; - /** - * @var string The hashed password - */ - private $password; - public function getEmail(): ?string { return $this->email; @@ -57,23 +52,17 @@ public function setRoles(array $roles): self } /** + * This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords. + * * @see UserInterface */ - public function getPassword(): string - { - return $this->password; - } - - public function setPassword(string $password): self + public function getPassword(): ?string { - $this->password = $password; - - return $this; + return null; } /** - * Returning a salt is only needed, if you are not using a modern - * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml. + * This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords. * * @see UserInterface */