forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecurityAssertionsTrait.php
195 lines (168 loc) · 5.33 KB
/
SecurityAssertionsTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Security as LegacySecurity;
use Symfony\Component\Security\Core\User\UserInterface;
use function sprintf;
trait SecurityAssertionsTrait
{
/**
* Check that user is not authenticated.
*
* ```php
* <?php
* $I->dontSeeAuthentication();
* ```
*/
public function dontSeeAuthentication(): void
{
$security = $this->grabSecurityService();
$this->assertFalse(
$security->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY),
'There is an user authenticated'
);
}
/**
* Check that user is not authenticated with the 'remember me' option.
*
* ```php
* <?php
* $I->dontSeeRememberedAuthentication();
* ```
*/
public function dontSeeRememberedAuthentication(): void
{
$security = $this->grabSecurityService();
$hasRememberMeCookie = $this->client->getCookieJar()->get('REMEMBERME');
$hasRememberMeRole = $security->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED);
$isRemembered = $hasRememberMeCookie && $hasRememberMeRole;
$this->assertFalse(
$isRemembered,
'User does have remembered authentication'
);
}
/**
* Checks that a user is authenticated.
*
* ```php
* <?php
* $I->seeAuthentication();
* ```
*/
public function seeAuthentication(): void
{
$security = $this->grabSecurityService();
if (!$security->getUser()) {
$this->fail('There is no user in session');
}
$this->assertTrue(
$security->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY),
'There is no authenticated user'
);
}
/**
* Checks that a user is authenticated with the 'remember me' option.
*
* ```php
* <?php
* $I->seeRememberedAuthentication();
* ```
*/
public function seeRememberedAuthentication(): void
{
$security = $this->grabSecurityService();
if ($security->getUser() === null) {
$this->fail('There is no user in session');
}
$hasRememberMeCookie = $this->client->getCookieJar()->get('REMEMBERME');
$hasRememberMeRole = $security->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED);
$isRemembered = $hasRememberMeCookie && $hasRememberMeRole;
$this->assertTrue(
$isRemembered,
'User does not have remembered authentication'
);
}
/**
* Check that the current user has a role
*
* ```php
* <?php
* $I->seeUserHasRole('ROLE_ADMIN');
* ```
*/
public function seeUserHasRole(string $role): void
{
$security = $this->grabSecurityService();
if (!$user = $security->getUser()) {
$this->fail('There is no user in session');
}
$userIdentifier = method_exists($user, 'getUserIdentifier') ?
$user->getUserIdentifier() :
$user->getUsername();
$this->assertTrue(
$security->isGranted($role),
sprintf(
'User %s has no role %s',
$userIdentifier,
$role
)
);
}
/**
* Verifies that the current user has multiple roles
*
* ```php
* <?php
* $I->seeUserHasRoles(['ROLE_USER', 'ROLE_ADMIN']);
* ```
*
* @param string[] $roles
*/
public function seeUserHasRoles(array $roles): void
{
foreach ($roles as $role) {
$this->seeUserHasRole($role);
}
}
/**
* Checks that the user's password would not benefit from rehashing.
* If the user is not provided it is taken from the current session.
*
* You might use this function after performing tasks like registering a user or submitting a password update form.
*
* ```php
* <?php
* $I->seeUserPasswordDoesNotNeedRehash();
* $I->seeUserPasswordDoesNotNeedRehash($user);
* ```
*
* @param UserInterface|null $user
*/
public function seeUserPasswordDoesNotNeedRehash(?UserInterface $user = null): void
{
if ($user === null) {
$security = $this->grabSecurityService();
if (!$user = $security->getUser()) {
$this->fail('No user found to validate');
}
}
$hasher = $this->grabPasswordHasherService();
$this->assertFalse($hasher->needsRehash($user), 'User password needs rehash');
}
protected function grabSecurityService(): Security|LegacySecurity
{
return $this->grabService('security.helper');
}
protected function grabPasswordHasherService(): UserPasswordHasherInterface|UserPasswordEncoderInterface
{
$hasher = $this->getService('security.password_hasher') ?: $this->getService('security.password_encoder');
if ($hasher === null) {
$this->fail('Password hasher service could not be found.');
}
return $hasher;
}
}