|
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) |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
} |
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.