Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Authentication.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext;
8
9 trait Authentication
10 {
12 protected $userSession;
13
17 public function usePermissionsOfRole($role)
18 {
19 $credentials = $this->getCredentialsFor($role);
20
21 switch ($this->authType) {
22 case self::AUTHTYPE_BASICHTTP:
23 $this->restDriver->setAuthentication(
24 $credentials['login'],
25 $credentials['password']
26 );
27 break;
28 case self::AUTHTYPE_SESSION:
29 $this->createSession($credentials['login'], $credentials['password']);
30 break;
31 default:
32 throw new \Exception("Unknown auth type: '{$this->authType}'.");
33 }
34
35 // also authenticate the user on the local repository instance
36 $this->getRepository()->setCurrentUser(
37 $this->getRepository()->getUserService()->loadUserByLogin($credentials['login'])
38 );
39 }
40
45 public function useAnonymousRole()
46 {
47 switch ($this->authType) {
48 case self::AUTHTYPE_BASICHTTP:
49 $this->restDriver->setAuthentication('anonymous', '');
50 break;
51 case self::AUTHTYPE_SESSION:
52 $this->cleanupSession();
53 break;
54 default:
55 throw new \Exception("Unknown auth type: '{$this->authType}'.");
56 }
57 }
58
62 public function createSession($login, $password)
63 {
64 $this->createRequest('post', '/user/sessions');
65 $this->setHeaderWithObject('accept', 'Session');
66 $this->setHeaderWithObject('content-type', 'SessionInput');
67
68 $this->makeObject('SessionInput');
69 $this->setFieldToValue('login', $login);
70 $this->setFieldToValue('password', $password);
71 $this->sendRequest();
72
73 $this->userSession = $this->getResponseObject();
74
75 if (!$this->userSession instanceof \eZ\Publish\Core\REST\Server\Values\UserSession) {
76 if ($this->userSession instanceof \eZ\Publish\Core\REST\Client\Values\ErrorMessage) {
77 $message = sprintf(
78 "Unexpected '%s' in HTTP request response: %s",
79 $this->userSession->message,
80 $this->userSession->description
81 );
82 } else {
83 $message = false;
84 }
85 throw new \RuntimeException(
86 $message ?: 'UserSession value expected, got ' . get_class($this->userSession),
87 0,
88 null
89 );
90 }
91
92 $this->resetDriver();
93
94 // apply session/csrf token to next request
95 $this->restDriver->setHeader('cookie', "{$this->userSession->sessionName}={$this->userSession->sessionId}");
96 $this->restDriver->setHeader('x-csrf-token', $this->userSession->csrfToken);
97 }
98
104 public function cleanupSession()
105 {
106 if ($this->userSession) {
107 $this->resetDriver();
108 $this->createRequest('delete', "/user/sessions/{$this->userSession->sessionId}");
109 $this->restDriver->setHeader('cookie', "{$this->userSession->sessionName}={$this->userSession->sessionId}");
110 $this->restDriver->setHeader('x-csrf-token', $this->userSession->csrfToken);
111 $this->sendRequest();
112 $this->userSession = null;
113 }
114 }
115 }
eZ\Publish\Core\REST\Client\Values\ErrorMessage
Definition: Values/ErrorMessage.php:20
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext
Definition: Authentication.php:7
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\createSession
createSession($login, $password)
@When I create a (new) session with login :login and password :password
Definition: Authentication.php:62
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\usePermissionsOfRole
usePermissionsOfRole($role)
@Given I have :role permissions
Definition: Authentication.php:17
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\setFieldToValue
setFieldToValue($field, $value)
@When I set field :field to :value
Definition: EzRest.php:103
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\cleanupSession
cleanupSession()
@AfterScenario
Definition: Authentication.php:104
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\Authentication
trait Authentication
Definition: Authentication.php:10
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\makeObject
makeObject($type)
@When I make a(n) :type object
Definition: EzRest.php:95
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\setHeaderWithObject
setHeaderWithObject($header, $object)
@When I set header :header with/for :object object
Definition: EzRest.php:44
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\getResponseObject
getResponseObject()
Get the response object (if it's not converted do the conversion also).
Definition: EzRest.php:230
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\useAnonymousRole
useAnonymousRole()
@Given I don't have permissions @Given I do not have permissions
Definition: Authentication.php:45
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.