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
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext; |
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
|
|
|
isset($exceptionValue) ? $exceptionValue : 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('post', "/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
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.