Skip to content

Commit ab98b0a

Browse files
committed
[Security] Fix BC layer
1 parent 8fc53b1 commit ab98b0a

File tree

5 files changed

+48
-101
lines changed

5 files changed

+48
-101
lines changed

Tests/User/InMemoryUserProviderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function createProvider(): InMemoryUserProvider
7474
public function testCreateUser()
7575
{
7676
$provider = new InMemoryUserProvider();
77-
$provider->createUser(new User('fabien', 'foo'));
77+
$provider->createUser(new InMemoryUser('fabien', 'foo'));
7878

7979
$user = $provider->loadUserByUsername('fabien');
8080
$this->assertEquals('foo', $user->getPassword());
@@ -84,8 +84,8 @@ public function testCreateUserAlreadyExist()
8484
{
8585
$this->expectException(\LogicException::class);
8686
$provider = new InMemoryUserProvider();
87-
$provider->createUser(new User('fabien', 'foo'));
88-
$provider->createUser(new User('fabien', 'foo'));
87+
$provider->createUser(new InMemoryUser('fabien', 'foo'));
88+
$provider->createUser(new InMemoryUser('fabien', 'foo'));
8989
}
9090

9191
public function testLoadUserByUsernameDoesNotExist()

User/InMemoryUser.php

Lines changed: 24 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,115 +19,58 @@
1919
* @author Robin Chalas <[email protected]>
2020
* @author Fabien Potencier <[email protected]>
2121
*/
22-
final class InMemoryUser implements UserInterface, PasswordAuthenticatedUserInterface, EquatableInterface
22+
final class InMemoryUser extends User
2323
{
24-
private $username;
25-
private $password;
26-
private $enabled;
27-
private $roles;
28-
2924
/**
30-
* @param string[] $roles
25+
* {@inheritdoc}
26+
*
27+
* @deprecated since Symfony 5.3
3128
*/
32-
public function __construct(string $username, ?string $password, array $roles = [], bool $enabled = true)
29+
public function isAccountNonExpired(): bool
3330
{
34-
if ('' === $username) {
35-
throw new \InvalidArgumentException('The username cannot be empty.');
36-
}
31+
trigger_deprecation('symfony/security-core', '5.3', 'Method "%s()" is deprecated, you should stop using it.', __METHOD__);
3732

38-
$this->username = $username;
39-
$this->password = $password;
40-
$this->roles = $roles;
41-
$this->enabled = $enabled;
42-
}
43-
44-
public function __toString(): string
45-
{
46-
return $this->getUsername();
33+
return parent::isAccountNonExpired();
4734
}
4835

4936
/**
5037
* {@inheritdoc}
38+
*
39+
* @deprecated since Symfony 5.3
5140
*/
52-
public function getRoles(): array
41+
public function isAccountNonLocked(): bool
5342
{
54-
return $this->roles;
55-
}
43+
trigger_deprecation('symfony/security-core', '5.3', 'Method "%s()" is deprecated, you should stop using it.', __METHOD__);
5644

57-
/**
58-
* {@inheritdoc}
59-
*/
60-
public function getPassword(): ?string
61-
{
62-
return $this->password;
45+
return parent::isAccountNonLocked();
6346
}
6447

6548
/**
6649
* {@inheritdoc}
50+
*
51+
* @deprecated since Symfony 5.3
6752
*/
68-
public function getSalt(): ?string
53+
public function isCredentialsNonExpired(): bool
6954
{
70-
return null;
71-
}
55+
trigger_deprecation('symfony/security-core', '5.3', 'Method "%s()" is deprecated, you should stop using it.', __METHOD__);
7256

73-
/**
74-
* {@inheritdoc}
75-
*/
76-
public function getUsername(): string
77-
{
78-
return $this->username;
57+
return parent::isCredentialsNonExpired();
7958
}
8059

8160
/**
82-
* Checks whether the user is enabled.
83-
*
84-
* Internally, if this method returns false, the authentication system
85-
* will throw a DisabledException and prevent login.
86-
*
87-
* @return bool true if the user is enabled, false otherwise
88-
*
89-
* @see DisabledException
61+
* @deprecated since Symfony 5.3
9062
*/
91-
public function isEnabled(): bool
63+
public function getExtraFields(): array
9264
{
93-
return $this->enabled;
94-
}
65+
trigger_deprecation('symfony/security-core', '5.3', 'Method "%s()" is deprecated, you should stop using it.', __METHOD__);
9566

96-
/**
97-
* {@inheritdoc}
98-
*/
99-
public function eraseCredentials()
100-
{
67+
return parent::getExtraFields();
10168
}
10269

103-
/**
104-
* {@inheritdoc}
105-
*/
106-
public function isEqualTo(UserInterface $user): bool
70+
public function setPassword(string $password)
10771
{
108-
if (!$user instanceof self) {
109-
return false;
110-
}
111-
112-
if ($this->getPassword() !== $user->getPassword()) {
113-
return false;
114-
}
115-
116-
$currentRoles = array_map('strval', (array) $this->getRoles());
117-
$newRoles = array_map('strval', (array) $user->getRoles());
118-
$rolesChanged = \count($currentRoles) !== \count($newRoles) || \count($currentRoles) !== \count(array_intersect($currentRoles, $newRoles));
119-
if ($rolesChanged) {
120-
return false;
121-
}
122-
123-
if ($this->getUsername() !== $user->getUsername()) {
124-
return false;
125-
}
126-
127-
if ($this->isEnabled() !== $user->isEnabled()) {
128-
return false;
129-
}
72+
trigger_deprecation('symfony/security-core', '5.3', 'Method "%s()" is deprecated, you should stop using it.', __METHOD__);
13073

131-
return true;
74+
parent::setPassword($password);
13275
}
13376
}

User/InMemoryUserChecker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function checkPreAuth(UserInterface $user)
3838
}
3939

4040
// @deprecated since Symfony 5.3
41-
if ($user instanceof User) {
41+
if (User::class === \get_class($user)) {
4242
if (!$user->isAccountNonLocked()) {
4343
$ex = new LockedException('User account is locked.');
4444
$ex->setUser($user);
@@ -56,7 +56,7 @@ public function checkPreAuth(UserInterface $user)
5656
public function checkPostAuth(UserInterface $user)
5757
{
5858
// @deprecated since Symfony 5.3, noop in 6.0
59-
if (!$user instanceof User) {
59+
if (User::class !== \get_class($user)) {
6060
return;
6161
}
6262

User/InMemoryUserProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function refreshUser(UserInterface $user)
8080
$storedUser = $this->getUser($user->getUsername());
8181

8282
// @deprecated since Symfony 5.3
83-
if ($user instanceof User) {
84-
if (!$storedUser instanceof User) {
83+
if (User::class === \get_class($user)) {
84+
if (User::class !== \get_class($storedUser)) {
8585
$accountNonExpired = true;
8686
$credentialsNonExpired = $storedUser->getPassword() === $user->getPassword();
8787
$accountNonLocked = true;

User/User.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Security\Core\User;
1313

14-
trigger_deprecation('symfony/security-core', '5.3', 'The "%s" class is deprecated, use "%s" instead.', User::class, InMemoryUser::class);
15-
1614
/**
1715
* User is the user implementation used by the in-memory user provider.
1816
*
@@ -22,7 +20,7 @@
2220
*
2321
* @deprecated since Symfony 5.3, use {@link InMemoryUser} instead
2422
*/
25-
final class User implements UserInterface, PasswordAuthenticatedUserInterface, EquatableInterface
23+
class User implements UserInterface, PasswordAuthenticatedUserInterface, EquatableInterface
2624
{
2725
private $username;
2826
private $password;
@@ -35,6 +33,10 @@ final class User implements UserInterface, PasswordAuthenticatedUserInterface, E
3533

3634
public function __construct(?string $username, ?string $password, array $roles = [], bool $enabled = true, bool $userNonExpired = true, bool $credentialsNonExpired = true, bool $userNonLocked = true, array $extraFields = [])
3735
{
36+
if (InMemoryUser::class !== static::class) {
37+
trigger_deprecation('symfony/security-core', '5.3', 'The "%s" class is deprecated, use "%s" instead.', self::class, InMemoryUser::class);
38+
}
39+
3840
if ('' === $username || null === $username) {
3941
throw new \InvalidArgumentException('The username cannot be empty.');
4042
}
@@ -175,8 +177,8 @@ public function isEqualTo(UserInterface $user): bool
175177
return false;
176178
}
177179

178-
$currentRoles = array_map('strval', (array)$this->getRoles());
179-
$newRoles = array_map('strval', (array)$user->getRoles());
180+
$currentRoles = array_map('strval', (array) $this->getRoles());
181+
$newRoles = array_map('strval', (array) $user->getRoles());
180182
$rolesChanged = \count($currentRoles) !== \count($newRoles) || \count($currentRoles) !== \count(array_intersect($currentRoles, $newRoles));
181183
if ($rolesChanged) {
182184
return false;
@@ -186,16 +188,18 @@ public function isEqualTo(UserInterface $user): bool
186188
return false;
187189
}
188190

189-
if ($this->isAccountNonExpired() !== $user->isAccountNonExpired()) {
190-
return false;
191-
}
191+
if (self::class === static::class) {
192+
if ($this->isAccountNonExpired() !== $user->isAccountNonExpired()) {
193+
return false;
194+
}
192195

193-
if ($this->isAccountNonLocked() !== $user->isAccountNonLocked()) {
194-
return false;
195-
}
196+
if ($this->isAccountNonLocked() !== $user->isAccountNonLocked()) {
197+
return false;
198+
}
196199

197-
if ($this->isCredentialsNonExpired() !== $user->isCredentialsNonExpired()) {
198-
return false;
200+
if ($this->isCredentialsNonExpired() !== $user->isCredentialsNonExpired()) {
201+
return false;
202+
}
199203
}
200204

201205
if ($this->isEnabled() !== $user->isEnabled()) {

0 commit comments

Comments
 (0)