|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the Authenticator used for integration tests. |
|
5
|
|
|
* |
|
6
|
|
|
* ATTENTION: This is a only meant for the test setup for the REST server. DO |
|
7
|
|
|
* NOT USE IT IN PRODUCTION! |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
10
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace eZ\Publish\Core\REST\Server\Authenticator; |
|
13
|
|
|
|
|
14
|
|
|
use eZ\Publish\Core\REST\Server\Authenticator; |
|
15
|
|
|
use eZ\Publish\API\Repository\Repository; |
|
16
|
|
|
use Qafoo\RMF; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use RuntimeException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Authenticator for integration tests. |
|
22
|
|
|
* |
|
23
|
|
|
* @todo Remove when the REST client is refactored |
|
24
|
|
|
*/ |
|
25
|
|
|
class IntegrationTest extends Authenticator |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Creates an new Authenticator to $repository. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Repository $repository) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->repository = $repository; |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Authenticates the user based on the given request. |
|
39
|
|
|
* |
|
40
|
|
|
* Performs an authentication based on the given $request and sets the |
|
41
|
|
|
* authenticated user into the $repository. Returns true on success, false |
|
42
|
|
|
* of authentication was not possible or did not succeed. |
|
43
|
|
|
* |
|
44
|
|
|
* @param RMF\Request $request |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
|
|
public function authenticate(RMF\Request $request) |
|
49
|
|
|
{ |
|
50
|
|
|
try { |
|
51
|
|
|
$this->repository->setCurrentUser( |
|
52
|
|
|
$this->repository->getUserService()->loadUser($request->testUser) |
|
53
|
|
|
); |
|
54
|
|
|
} catch (InvalidArgumentException $e) { |
|
55
|
|
|
throw new RuntimeException('The Integration Test Authenticator requires a test user ID to be set using the HTTP Header X-Test-User.'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
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: