|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JwPersistentUser\Listener; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
use Interop\Container\Exception\ContainerException; |
|
7
|
|
|
use JwPersistentUser\Service\CookieService; |
|
8
|
|
|
use JwPersistentUser\Service\RememberMeService; |
|
9
|
|
|
|
|
10
|
|
|
use Zend\EventManager\Event; |
|
11
|
|
|
use Zend\Http\Request; |
|
12
|
|
|
use Zend\Http\Response; |
|
13
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
|
14
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
|
15
|
|
|
use Zend\ServiceManager\Factory\DelegatorFactoryInterface; |
|
16
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
17
|
|
|
use Zend\Stdlib\RequestInterface; |
|
18
|
|
|
use Zend\Stdlib\ResponseInterface; |
|
19
|
|
|
use Zend\EventManager\SharedEventManagerInterface; |
|
20
|
|
|
|
|
21
|
|
|
use ZfcUser\Authentication\Adapter\AdapterChain; |
|
22
|
|
|
use ZfcUser\Authentication\Adapter\AdapterChainEvent; |
|
23
|
|
|
|
|
24
|
|
|
class WriteTokenToCookie implements DelegatorFactoryInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var [] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $sharedListeners = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ContainerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $serviceLocator; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var RequestInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $request; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ResponseInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $response; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var RememberMeService |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $rememberMeService; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var CookieService |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $cookieService; |
|
55
|
|
|
|
|
56
|
|
|
public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->serviceLocator = $container; |
|
59
|
|
|
|
|
60
|
|
|
/** @var AdapterChain $original */ |
|
61
|
|
|
$original = call_user_func($callback); |
|
62
|
|
|
|
|
63
|
|
|
$original->getEventManager()->attach( |
|
64
|
|
|
'authenticate.success', |
|
65
|
3 |
|
[$this, 'authenticate'] |
|
66
|
|
|
); |
|
67
|
3 |
|
|
|
68
|
2 |
|
$original->getEventManager()->attach( |
|
69
|
|
|
'logout', |
|
70
|
|
|
[$this, 'logout'] |
|
71
|
1 |
|
); |
|
72
|
|
|
|
|
73
|
1 |
|
return $original; |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function authenticate(Event $e) |
|
77
|
|
|
{ |
|
78
|
|
|
$e = $e->getTarget(); |
|
79
|
3 |
|
|
|
80
|
|
|
if (!$this->isValidRequestAndResponse()) { |
|
81
|
3 |
|
return; |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
$serieToken = $this->getRememberMeService()->createNew($e->getIdentity()); |
|
85
|
1 |
|
|
|
86
|
1 |
|
$this->getCookieService()->writeSerie($this->getResponse(), $serieToken); |
|
|
|
|
|
|
87
|
1 |
|
} |
|
88
|
1 |
|
|
|
89
|
|
|
public function logout(Event $e) |
|
90
|
1 |
|
{ |
|
91
|
1 |
|
$e = $e->getTarget(); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
if (!$this->isValidRequestAndResponse()) { |
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
6 |
|
|
|
97
|
|
|
$serieToken = $this->getCookieService()->read($this->getRequest(), $this->getResponse()); |
|
|
|
|
|
|
98
|
6 |
|
if ($serieToken) { |
|
99
|
6 |
|
$this->getRememberMeService()->removeSerie($serieToken->getUserId(), $serieToken->getSerie()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->getCookieService()->writeNull($this->getResponse()); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function isValidRequestAndResponse() |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->getRequest() instanceof Request |
|
111
|
|
|
&& $this->getResponse() instanceof Response; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
public function detachShared(SharedEventManagerInterface $events) |
|
115
|
|
|
{ |
|
116
|
2 |
|
foreach ($this->sharedListeners as $index => $listener) { |
|
117
|
|
|
if ($events->detach($this->eventIdentifier, $listener)) { |
|
|
|
|
|
|
118
|
|
|
unset($this->sharedListeners[$index]); |
|
119
|
2 |
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return RememberMeService |
|
125
|
|
|
*/ |
|
126
|
6 |
|
public function getRememberMeService() |
|
127
|
|
|
{ |
|
128
|
6 |
|
if ($this->rememberMeService === null) { |
|
129
|
6 |
|
$this->rememberMeService = $this->serviceLocator->get('JwPersistentUser\Service\RememberMe'); |
|
130
|
|
|
} |
|
131
|
|
|
return $this->rememberMeService; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
4 |
|
* @param RememberMeService $rememberMeService |
|
136
|
|
|
* @return $this |
|
137
|
4 |
|
*/ |
|
138
|
|
|
public function setRememberMeService($rememberMeService) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->rememberMeService = $rememberMeService; |
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
6 |
|
/** |
|
145
|
|
|
* @return ResponseInterface |
|
146
|
6 |
|
*/ |
|
147
|
6 |
|
public function getResponse() |
|
148
|
|
|
{ |
|
149
|
|
|
if (!$this->response) { |
|
150
|
|
|
$this->response = $this->serviceLocator->get('Response'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
6 |
|
return $this->response; |
|
154
|
|
|
} |
|
155
|
6 |
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param ResponseInterface $response |
|
158
|
|
|
* @return $this |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setResponse($response) |
|
161
|
|
|
{ |
|
162
|
6 |
|
$this->response = $response; |
|
163
|
|
|
return $this; |
|
164
|
6 |
|
} |
|
165
|
6 |
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return RequestInterface |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getRequest() |
|
170
|
|
|
{ |
|
171
|
2 |
|
if (!$this->request) { |
|
172
|
|
|
$this->request = $this->serviceLocator->get('Request'); |
|
173
|
2 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $this->request; |
|
176
|
2 |
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param RequestInterface $request |
|
180
|
|
|
* @return $this |
|
181
|
|
|
*/ |
|
182
|
|
|
public function setRequest($request) |
|
183
|
6 |
|
{ |
|
184
|
|
|
$this->request = $request; |
|
185
|
6 |
|
return $this; |
|
186
|
6 |
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return CookieService |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getCookieService() |
|
192
|
|
|
{ |
|
193
|
|
|
if ($this->cookieService === null) { |
|
194
|
|
|
$this->cookieService = $this->serviceLocator->get('JwPersistentUser\Service\Cookie'); |
|
195
|
|
|
} |
|
196
|
|
|
return $this->cookieService; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param CookieService $cookieService |
|
201
|
|
|
* @return $this |
|
202
|
|
|
*/ |
|
203
|
|
|
public function setCookieService($cookieService) |
|
204
|
|
|
{ |
|
205
|
|
|
$this->cookieService = $cookieService; |
|
206
|
|
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.