Completed
Push — master ( 6da558...92dcf1 )
by Łukasz
59:43 queued 31:01
created

Authentication::getCredentialsFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
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) {
0 ignored issues
show
Bug introduced by
The property authType does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
            case self::AUTHTYPE_BASICHTTP:
29
                $this->restDriver->setAuthentication(
0 ignored issues
show
Bug introduced by
The property restDriver does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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(
0 ignored issues
show
Bug introduced by
It seems like getRepository() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
43
            $this->getRepository()->getUserService()->loadUserByLogin($credentials['login'])
0 ignored issues
show
Bug introduced by
It seems like getRepository() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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');
0 ignored issues
show
Bug introduced by
It seems like createRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
71
        $this->setHeaderWithObject('accept', 'Session');
0 ignored issues
show
Bug introduced by
It seems like setHeaderWithObject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
72
        $this->setHeaderWithObject('content-type', 'SessionInput');
0 ignored issues
show
Bug introduced by
It seems like setHeaderWithObject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
73
74
        $this->makeObject('SessionInput');
0 ignored issues
show
Bug introduced by
It seems like makeObject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
75
        $this->setFieldToValue('login', $login);
0 ignored issues
show
Bug introduced by
It seems like setFieldToValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
76
        $this->setFieldToValue('password', $password);
0 ignored issues
show
Bug introduced by
It seems like setFieldToValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
77
        $this->sendRequest();
0 ignored issues
show
Bug introduced by
It seems like sendRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
78
79
        $this->userSession = $this->getResponseObject();
0 ignored issues
show
Bug introduced by
It seems like getResponseObject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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();
0 ignored issues
show
Bug introduced by
It seems like resetDriver() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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();
0 ignored issues
show
Bug introduced by
It seems like resetDriver() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
114
            $this->createRequest('delete', "/user/sessions/{$this->userSession->sessionId}");
0 ignored issues
show
Bug introduced by
It seems like createRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
115
            $this->restDriver->setHeader('cookie', "{$this->userSession->sessionName}={$this->userSession->sessionId}");
116
            $this->restDriver->setHeader('x-csrf-token', $this->userSession->csrfToken);
117
            $this->sendRequest();
0 ignored issues
show
Bug introduced by
It seems like sendRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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();
0 ignored issues
show
Bug introduced by
It seems like getRepository() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
128
129
        $currentUser = $repository->getCurrentUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
130
131
        $userService = $repository->getUserService();
132
        $repository->setCurrentUser($userService->loadUserByLogin('admin'));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
133
134
        $username = substr(uniqid('User#', true), 0, 15);
135
        $this->userFacade->createUser($username);
0 ignored issues
show
Bug introduced by
The property userFacade does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
136
        $this->userFacade->assignUserToRole($username, $roleIdentifier);
137
138
        $repository->setCurrentUser($currentUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
139
140
        return [
141
            'login' => $username,
142
            'password' => $this->userFacade->getDefaultPassword(),
143
        ];
144
    }
145
}
146