1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the Authentication context class for RestBundle. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Repository; |
12
|
|
|
|
13
|
|
|
trait Authentication |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \eZ\Publish\Core\REST\Server\Values\UserSession |
17
|
|
|
*/ |
18
|
|
|
protected $userSession; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @Given I have :role permissions |
22
|
|
|
*/ |
23
|
|
|
public function usePermissionsOfRole($role) |
24
|
|
|
{ |
25
|
|
|
$credentials = $this->getCredentialsFor($role); |
26
|
|
|
|
27
|
|
|
switch ($this->authType) { |
|
|
|
|
28
|
|
|
case self::AUTHTYPE_BASICHTTP: |
29
|
|
|
$this->restDriver->setAuthentication( |
|
|
|
|
30
|
|
|
$credentials['login'], |
31
|
|
|
$credentials['password'] |
32
|
|
|
); |
33
|
|
|
break; |
34
|
|
|
case self::AUTHTYPE_SESSION: |
35
|
|
|
$this->createSession($credentials['login'], $credentials['password']); |
36
|
|
|
break; |
37
|
|
|
default: |
38
|
|
|
throw new \Exception("Unknown auth type: '{$this->authType}'."); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// also authenticate the user on the local repository instance |
42
|
|
|
$this->getRepository()->setCurrentUser( |
|
|
|
|
43
|
|
|
$this->getRepository()->getUserService()->loadUserByLogin($credentials['login']) |
|
|
|
|
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @Given I don't have permissions |
49
|
|
|
* @Given I do not have permissions |
50
|
|
|
*/ |
51
|
|
|
public function useAnonymousRole() |
52
|
|
|
{ |
53
|
|
|
switch ($this->authType) { |
54
|
|
|
case self::AUTHTYPE_BASICHTTP: |
55
|
|
|
$this->restDriver->setAuthentication('anonymous', ''); |
56
|
|
|
break; |
57
|
|
|
case self::AUTHTYPE_SESSION: |
58
|
|
|
$this->cleanupSession(); |
59
|
|
|
break; |
60
|
|
|
default: |
61
|
|
|
throw new \Exception("Unknown auth type: '{$this->authType}'."); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @When I create a (new) session with login :login and password :password |
67
|
|
|
*/ |
68
|
|
|
public function createSession($login, $password) |
69
|
|
|
{ |
70
|
|
|
$this->createRequest('post', '/user/sessions'); |
|
|
|
|
71
|
|
|
$this->setHeaderWithObject('accept', 'Session'); |
|
|
|
|
72
|
|
|
$this->setHeaderWithObject('content-type', 'SessionInput'); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->makeObject('SessionInput'); |
|
|
|
|
75
|
|
|
$this->setFieldToValue('login', $login); |
|
|
|
|
76
|
|
|
$this->setFieldToValue('password', $password); |
|
|
|
|
77
|
|
|
$this->sendRequest(); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->userSession = $this->getResponseObject(); |
|
|
|
|
80
|
|
|
|
81
|
|
|
if (!$this->userSession instanceof \eZ\Publish\Core\REST\Server\Values\UserSession) { |
82
|
|
|
if ($this->userSession instanceof \eZ\Publish\Core\REST\Client\Values\ErrorMessage) { |
83
|
|
|
$message = sprintf( |
84
|
|
|
"Unexpected '%s' in HTTP request response: %s", |
85
|
|
|
$this->userSession->message, |
86
|
|
|
$this->userSession->description |
87
|
|
|
); |
88
|
|
|
} else { |
89
|
|
|
$message = false; |
90
|
|
|
} |
91
|
|
|
throw new \RuntimeException( |
92
|
|
|
$message ?: 'UserSession value expected, got ' . get_class($this->userSession), |
93
|
|
|
0, |
94
|
|
|
null |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->resetDriver(); |
|
|
|
|
99
|
|
|
|
100
|
|
|
// apply session/csrf token to next request |
101
|
|
|
$this->restDriver->setHeader('cookie', "{$this->userSession->sessionName}={$this->userSession->sessionId}"); |
102
|
|
|
$this->restDriver->setHeader('x-csrf-token', $this->userSession->csrfToken); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @AfterScenario |
107
|
|
|
* |
108
|
|
|
* Cleanup session, if applicable. |
109
|
|
|
*/ |
110
|
|
|
public function cleanupSession() |
111
|
|
|
{ |
112
|
|
|
if ($this->userSession) { |
113
|
|
|
$this->resetDriver(); |
|
|
|
|
114
|
|
|
$this->createRequest('delete', "/user/sessions/{$this->userSession->sessionId}"); |
|
|
|
|
115
|
|
|
$this->restDriver->setHeader('cookie', "{$this->userSession->sessionName}={$this->userSession->sessionId}"); |
116
|
|
|
$this->restDriver->setHeader('x-csrf-token', $this->userSession->csrfToken); |
117
|
|
|
$this->sendRequest(); |
|
|
|
|
118
|
|
|
$this->userSession = null; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function getCredentialsFor($roleIdentifier) |
123
|
|
|
{ |
124
|
|
|
$roleIdentifier = ucfirst($roleIdentifier); |
125
|
|
|
|
126
|
|
|
/** @var Repository $repository */ |
127
|
|
|
$repository = $this->getRepository(); |
|
|
|
|
128
|
|
|
|
129
|
|
|
$currentUser = $repository->getCurrentUser(); |
|
|
|
|
130
|
|
|
|
131
|
|
|
$userService = $repository->getUserService(); |
132
|
|
|
$repository->setCurrentUser($userService->loadUserByLogin('admin')); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$username = substr(uniqid('User#', true), 0, 15); |
135
|
|
|
$this->userFacade->createUser($username); |
|
|
|
|
136
|
|
|
$this->userFacade->assignUserToRole($username, $roleIdentifier); |
137
|
|
|
|
138
|
|
|
$repository->setCurrentUser($currentUser); |
|
|
|
|
139
|
|
|
|
140
|
|
|
return [ |
141
|
|
|
'login' => $username, |
142
|
|
|
'password' => $this->userFacade->getDefaultPassword(), |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: