Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0ba835a

Browse filesBrowse files
committed
!squash update logic according master (5.0) changes
- Role/RoleInterface class was removed - Updated isEqualTo method to match roles as default User implements EquatableInterface
1 parent 157f6f7 commit 0ba835a
Copy full SHA for 0ba835a

File tree

6 files changed

+129
-32
lines changed
Filter options

6 files changed

+129
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
admin:
22
path: /admin
3-
defaults: { _controller: SecuredPageBundle:Admin:index }
3+
defaults: { _controller: \Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\SecuredPageBundle\Controller\AdminController::indexAction }

‎src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/SecuredPageBundle/Security/Core/User/ArrayUserProvider.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/SecuredPageBundle/Security/Core/User/ArrayUserProvider.php
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44

55
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
66
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
7-
use Symfony\Component\Security\Core\User\User;
87
use Symfony\Component\Security\Core\User\UserInterface;
98
use Symfony\Component\Security\Core\User\UserProviderInterface;
109

1110
class ArrayUserProvider implements UserProviderInterface
1211
{
13-
/** @var User[] */
12+
/** @var UserInterface[] */
1413
private $users = [];
1514

16-
public function addUser(User $user)
15+
public function addUser(UserInterface $user)
1716
{
1817
$this->users[$user->getUsername()] = $user;
1918
}
2019

21-
public function setUser($username, User $user)
20+
public function setUser($username, UserInterface $user)
2221
{
2322
$this->users[$username] = $user;
2423
}
@@ -41,13 +40,14 @@ public function loadUserByUsername($username)
4140

4241
public function refreshUser(UserInterface $user)
4342
{
44-
if (!$user instanceof User) {
43+
if (!$user instanceof UserInterface) {
4544
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
4645
}
4746

4847
$storedUser = $this->getUser($user->getUsername());
48+
$class = get_class($storedUser);
4949

50-
return new User($storedUser->getUsername(), $storedUser->getPassword(), $storedUser->getRoles(), $storedUser->isEnabled(), $storedUser->isAccountNonExpired(), $storedUser->isCredentialsNonExpired() && $storedUser->getPassword() === $user->getPassword(), $storedUser->isAccountNonLocked());
50+
return new $class($storedUser->getUsername(), $storedUser->getPassword(), $storedUser->getRoles(), $storedUser->isEnabled(), $storedUser->isAccountNonExpired(), $storedUser->isCredentialsNonExpired() && $storedUser->getPassword() === $user->getPassword(), $storedUser->isAccountNonLocked());
5151
}
5252

5353
public function supportsClass($class)

‎src/Symfony/Bundle/SecurityBundle/Tests/Functional/SecurityTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Tests/Functional/SecurityTest.php
+107-6Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\SecuredPageBundle\Security\Core\User\ArrayUserProvider;
1515
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
16-
use Symfony\Component\Security\Core\Role\Role;
1716
use Symfony\Component\Security\Core\User\User;
17+
use Symfony\Component\Security\Core\User\UserInterface;
1818

1919
class SecurityTest extends WebTestCase
2020
{
@@ -37,22 +37,22 @@ public function testServiceIsFunctional()
3737
public function userWillBeMarkedAsChangedIfRolesHasChangedProvider()
3838
{
3939
return [
40-
[new Role('ROLE_ADMIN'), new Role('ROLE_USER')],
41-
['ROLE_ADMIN', 'ROLE_USER'],
40+
[User::class],
41+
[UserWithoutEquatable::class],
4242
];
4343
}
4444

4545
/**
4646
* @dataProvider userWillBeMarkedAsChangedIfRolesHasChangedProvider
4747
*/
48-
public function testUserWillBeMarkedAsChangedIfRolesHasChanged($adminRole, $userRole)
48+
public function testUserWillBeMarkedAsChangedIfRolesHasChanged($userClass)
4949
{
5050
$client = $this->createClient(['test_case' => 'AbstractTokenCompareRoles', 'root_config' => 'config.yml']);
5151
$client->disableReboot();
5252

5353
/** @var ArrayUserProvider $userProvider */
5454
$userProvider = static::$kernel->getContainer()->get('security.user.provider.array');
55-
$userProvider->addUser(new User('user1', 'test', [$adminRole]));
55+
$userProvider->addUser(new $userClass('user1', 'test', ['ROLE_ADMIN']));
5656

5757
$client->request('POST', '/login', [
5858
'_username' => 'user1',
@@ -64,10 +64,111 @@ public function testUserWillBeMarkedAsChangedIfRolesHasChanged($adminRole, $user
6464
$this->assertEquals(200, $client->getResponse()->getStatusCode());
6565

6666
// revoking ROLE_ADMIN from user1
67-
$userProvider->setUser('user1', new User('user1', 'test', [$userRole]));
67+
$userProvider->setUser('user1', new $userClass('user1', 'test', ['ROLE_USER']));
6868

6969
// user1 has lost ROLE_ADMIN and MUST be redirected away from secure page
7070
$client->request('GET', '/admin');
7171
$this->assertEquals(302, $client->getResponse()->getStatusCode());
7272
}
7373
}
74+
75+
final class UserWithoutEquatable implements UserInterface
76+
{
77+
private $username;
78+
private $password;
79+
private $enabled;
80+
private $accountNonExpired;
81+
private $credentialsNonExpired;
82+
private $accountNonLocked;
83+
private $roles;
84+
85+
public function __construct(?string $username, ?string $password, array $roles = [], bool $enabled = true, bool $userNonExpired = true, bool $credentialsNonExpired = true, bool $userNonLocked = true)
86+
{
87+
if ('' === $username || null === $username) {
88+
throw new \InvalidArgumentException('The username cannot be empty.');
89+
}
90+
91+
$this->username = $username;
92+
$this->password = $password;
93+
$this->enabled = $enabled;
94+
$this->accountNonExpired = $userNonExpired;
95+
$this->credentialsNonExpired = $credentialsNonExpired;
96+
$this->accountNonLocked = $userNonLocked;
97+
$this->roles = $roles;
98+
}
99+
100+
public function __toString()
101+
{
102+
return $this->getUsername();
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function getRoles()
109+
{
110+
return $this->roles;
111+
}
112+
113+
/**
114+
* {@inheritdoc}
115+
*/
116+
public function getPassword()
117+
{
118+
return $this->password;
119+
}
120+
121+
/**
122+
* {@inheritdoc}
123+
*/
124+
public function getSalt()
125+
{
126+
}
127+
128+
/**
129+
* {@inheritdoc}
130+
*/
131+
public function getUsername()
132+
{
133+
return $this->username;
134+
}
135+
136+
/**
137+
* {@inheritdoc}
138+
*/
139+
public function isAccountNonExpired()
140+
{
141+
return $this->accountNonExpired;
142+
}
143+
144+
/**
145+
* {@inheritdoc}
146+
*/
147+
public function isAccountNonLocked()
148+
{
149+
return $this->accountNonLocked;
150+
}
151+
152+
/**
153+
* {@inheritdoc}
154+
*/
155+
public function isCredentialsNonExpired()
156+
{
157+
return $this->credentialsNonExpired;
158+
}
159+
160+
/**
161+
* {@inheritdoc}
162+
*/
163+
public function isEnabled()
164+
{
165+
return $this->enabled;
166+
}
167+
168+
/**
169+
* {@inheritdoc}
170+
*/
171+
public function eraseCredentials()
172+
{
173+
}
174+
}

‎src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AbstractTokenCompareRoles/config.yml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AbstractTokenCompareRoles/config.yml
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
security:
1111

1212
encoders:
13-
Symfony\Component\Security\Core\User\User: plaintext
13+
\Symfony\Component\Security\Core\User\UserInterface: plaintext
1414

1515
providers:
1616
array:
@@ -24,7 +24,6 @@ security:
2424
require_previous_session: false
2525
logout: ~
2626
anonymous: ~
27-
logout_on_user_change: true
2827
stateless: false
2928

3029
access_control:

‎src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php
+3-17Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ private function hasUserChanged(UserInterface $user)
277277

278278
$rolesChanged = \count(
279279
array_diff(
280-
array_map([$this, 'castRole'], (array) $this->user->getRoles()),
281-
array_map([$this, 'castRole'], (array) $user->getRoles())
280+
(array) $this->user->getRoles(),
281+
(array) $user->getRoles()
282282
)
283-
);
283+
) === 1;
284284

285285
if ($rolesChanged) {
286286
return true;
@@ -292,18 +292,4 @@ private function hasUserChanged(UserInterface $user)
292292

293293
return false;
294294
}
295-
296-
/**
297-
* @param string|Role $role
298-
*
299-
* @return string
300-
*/
301-
private function castRole($role)
302-
{
303-
if ($role instanceof Role) {
304-
return $role->getRole();
305-
}
306-
307-
return (string) $role;
308-
}
309295
}

‎src/Symfony/Component/Security/Core/User/User.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Core/User/User.php
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ public function isEqualTo(UserInterface $user)
135135
return false;
136136
}
137137

138+
$rolesChanged = \count(
139+
array_diff(
140+
(array) $this->getRoles(),
141+
(array) $user->getRoles()
142+
)
143+
) === 1;
144+
145+
if ($rolesChanged) {
146+
return false;
147+
}
148+
138149
if ($this->getUsername() !== $user->getUsername()) {
139150
return false;
140151
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.