Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit0ba835a

Browse 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 parent157f6f7 commit0ba835a

File tree

6 files changed

+129
-32
lines changed

6 files changed

+129
-32
lines changed
Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44

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

1110
class ArrayUserProviderimplements UserProviderInterface
1211
{
13-
/** @varUser[] */
12+
/** @varUserInterface[] */
1413
private$users = [];
1514

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

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

4241
publicfunctionrefreshUser(UserInterface$user)
4342
{
44-
if (!$userinstanceofUser) {
43+
if (!$userinstanceofUserInterface) {
4544
thrownewUnsupportedUserException(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-
returnnewUser($storedUser->getUsername(),$storedUser->getPassword(),$storedUser->getRoles(),$storedUser->isEnabled(),$storedUser->isAccountNonExpired(),$storedUser->isCredentialsNonExpired() &&$storedUser->getPassword() ===$user->getPassword(),$storedUser->isAccountNonLocked());
50+
returnnew$class($storedUser->getUsername(),$storedUser->getPassword(),$storedUser->getRoles(),$storedUser->isEnabled(),$storedUser->isAccountNonExpired(),$storedUser->isCredentialsNonExpired() &&$storedUser->getPassword() ===$user->getPassword(),$storedUser->isAccountNonLocked());
5151
}
5252

5353
publicfunctionsupportsClass($class)

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

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

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

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

4545
/**
4646
* @dataProvider userWillBeMarkedAsChangedIfRolesHasChangedProvider
4747
*/
48-
publicfunctiontestUserWillBeMarkedAsChangedIfRolesHasChanged($adminRole,$userRole)
48+
publicfunctiontestUserWillBeMarkedAsChangedIfRolesHasChanged($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(newUser('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',newUser('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+
finalclass UserWithoutEquatableimplements 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+
publicfunction__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+
thrownew \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+
publicfunction__toString()
101+
{
102+
return$this->getUsername();
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
publicfunctiongetRoles()
109+
{
110+
return$this->roles;
111+
}
112+
113+
/**
114+
* {@inheritdoc}
115+
*/
116+
publicfunctiongetPassword()
117+
{
118+
return$this->password;
119+
}
120+
121+
/**
122+
* {@inheritdoc}
123+
*/
124+
publicfunctiongetSalt()
125+
{
126+
}
127+
128+
/**
129+
* {@inheritdoc}
130+
*/
131+
publicfunctiongetUsername()
132+
{
133+
return$this->username;
134+
}
135+
136+
/**
137+
* {@inheritdoc}
138+
*/
139+
publicfunctionisAccountNonExpired()
140+
{
141+
return$this->accountNonExpired;
142+
}
143+
144+
/**
145+
* {@inheritdoc}
146+
*/
147+
publicfunctionisAccountNonLocked()
148+
{
149+
return$this->accountNonLocked;
150+
}
151+
152+
/**
153+
* {@inheritdoc}
154+
*/
155+
publicfunctionisCredentialsNonExpired()
156+
{
157+
return$this->credentialsNonExpired;
158+
}
159+
160+
/**
161+
* {@inheritdoc}
162+
*/
163+
publicfunctionisEnabled()
164+
{
165+
return$this->enabled;
166+
}
167+
168+
/**
169+
* {@inheritdoc}
170+
*/
171+
publicfunctioneraseCredentials()
172+
{
173+
}
174+
}

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

Lines 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‎

Lines 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
returntrue;
@@ -292,18 +292,4 @@ private function hasUserChanged(UserInterface $user)
292292

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

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ public function isEqualTo(UserInterface $user)
135135
returnfalse;
136136
}
137137

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp