Completed
Push — develop ( 4bf430...c74260 )
by
unknown
18:30
created

UserSwitcher::getSessionContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Auth\Controller\Plugin;
12
13
use Zend\Authentication\AuthenticationService;
14
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
15
use Zend\Session\Container;
16
17
/**
18
 * Plugin to switch logged in user w/o authentication.
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @since 0.29
22
 */
23
class UserSwitcher extends AbstractPlugin
24
{
25
    const SESSION_NAMESPACE = "SwitchedUser";
26
27
    /**
28
     * AuthenticationService
29
     *
30
     * @var \Zend\Authentication\AuthenticationService
31
     */
32
    private $auth;
33
34
    /**
35
     * Creates an instance
36
     *
37
     * @param AuthenticationService $auth
38
     */
39
    public function __construct(AuthenticationService $auth)
40
    {
41
        $this->auth = $auth;
42
    }
43
44
    /**
45
     * Switch to or restore an user.
46
     *
47
     * If $userId is not null, attempt to switch the user,
48
     * restore the original user otherwise.
49
     *
50
     * @param null|string $userId
51
     *
52
     * @return bool
53
     */
54
    public function __invoke($userId = null)
0 ignored issues
show
Coding Style introduced by
function __invoke() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
55
    {
56
        if (null === $userId) {
57
            return $this->clear();
58
        }
59
60
        return $this->switchUser($userId);
61
    }
62
63
    /**
64
     * Restores the original user.
65
     *
66
     * @return bool
67
     */
68
    public function clear()
0 ignored issues
show
Coding Style introduced by
function clear() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
69
    {
70
        $session = $this->getSessionContainer();
71
        if (!$session->isSwitchedUser) {
72
            return false;
73
        }
74
75
        $originalUser = $session->originalUser;
76
        $this->exchangeAuthUser($originalUser);
77
        /* @var \Zend\Session\Storage\StorageInterface $sessionStorage */
78
        $sessionStorage = $session->getManager()->getStorage();
79
        $sessionStorage->clear(self::SESSION_NAMESPACE);
80
81
        return true;
82
    }
83
84
    /**
85
     * Switch to another user.
86
     *
87
     * @param string $id user id of the user to switch to.
88
     *
89
     * @return bool
90
     */
91
    public function switchUser($id)
0 ignored issues
show
Coding Style introduced by
function switchUser() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
92
    {
93
        $session = $this->getSessionContainer();
94
        if ($session->isSwitchedUser) {
95
            return false;
96
        }
97
98
        $session->isSwitchedUser = true;
99
        $session->originalUser = $this->exchangeAuthUser($id);
100
101
        return true;
102
    }
103
104
    /**
105
     * Gets the session container.
106
     *
107
     * @return Container
108
     */
109
    private function getSessionContainer()
110
    {
111
        return new Container(self::SESSION_NAMESPACE);
112
    }
113
114
    /**
115
     * Exchanges the authenticated user in AuthenticationService.
116
     *
117
     * @param string $id
118
     *
119
     * @return string The id of the previously authenticated user.
120
     */
121
    private function exchangeAuthUser($id)
122
    {
123
        $storage = $this->auth->getStorage();
124
        $originalUserId = $storage->read();
125
        $this->auth->clearIdentity();
126
        $storage->write($id);
127
128
        return $originalUserId;
129
    }
130
}