1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JwPersistentUser\Listener; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use JwPersistentUser\Service\CookieService; |
7
|
|
|
use JwPersistentUser\Service\RememberMeService; |
8
|
|
|
use Zend\EventManager\SharedEventManagerInterface; |
9
|
|
|
use Zend\Http\Request; |
10
|
|
|
use Zend\Http\Response; |
11
|
|
|
use Zend\Stdlib\RequestInterface; |
12
|
|
|
use Zend\Stdlib\ResponseInterface; |
13
|
|
|
use ZfcUser\Authentication\Adapter\AdapterChainEvent; |
14
|
|
|
|
15
|
|
|
class WriteTokenToCookie |
16
|
|
|
{ |
17
|
|
|
protected $sharedListeners = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ContainerInterface |
21
|
|
|
*/ |
22
|
|
|
protected $serviceLocator; |
23
|
|
|
|
24
|
|
|
public function __construct(ContainerInterface $serviceLocator) |
25
|
|
|
{ |
26
|
|
|
$this->serviceLocator = $serviceLocator; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function attachShared(SharedEventManagerInterface $events) |
30
|
|
|
{ |
31
|
|
|
$events->attach( |
32
|
|
|
'ZfcUser\Authentication\Adapter\AdapterChain', |
33
|
|
|
'authenticate.success', |
34
|
|
|
[$this, 'authenticate'] |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$events->attach( |
38
|
|
|
'ZfcUser\Authentication\Adapter\AdapterChain', |
39
|
|
|
'logout', |
40
|
|
|
[$this, 'logout'] |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function detachShared(SharedEventManagerInterface $events) |
45
|
|
|
{ |
46
|
|
|
foreach ($this->sharedListeners as $key => $handle) { |
47
|
|
|
if ($events->detach($handle[0], $handle[1])) { |
48
|
|
|
unset($this->sharedListeners[$key]); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function authenticate(AdapterChainEvent $e) |
54
|
|
|
{ |
55
|
|
|
if (!$this->isValidRequestAndResponse()) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$serieToken = $this->getRememberMeService()->createNew($e->getIdentity()); |
60
|
|
|
|
61
|
|
|
$this->getCookieService()->writeSerie($this->getResponse(), $serieToken); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function logout(AdapterChainEvent $e) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if (!$this->isValidRequestAndResponse()) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$serieToken = $this->getCookieService()->read($this->getRequest(), $this->getResponse()); |
|
|
|
|
71
|
|
|
if ($serieToken) { |
72
|
|
|
$this->getRememberMeService()->removeSerie($serieToken->getUserId(), $serieToken->getSerie()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->getCookieService()->writeNull($this->getResponse()); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
protected function isValidRequestAndResponse() |
82
|
|
|
{ |
83
|
|
|
return $this->getRequest() instanceof Request |
84
|
|
|
&& $this->getResponse() instanceof Response; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return RememberMeService |
89
|
|
|
*/ |
90
|
|
|
public function getRememberMeService() |
91
|
|
|
{ |
92
|
|
|
return $this->serviceLocator->get('JwPersistentUser\Service\RememberMe'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return ResponseInterface |
97
|
|
|
*/ |
98
|
|
|
public function getResponse() |
99
|
|
|
{ |
100
|
|
|
return $this->serviceLocator->get('Response'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return RequestInterface |
105
|
|
|
*/ |
106
|
|
|
public function getRequest() |
107
|
|
|
{ |
108
|
|
|
return $this->serviceLocator->get('Request'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return CookieService |
113
|
|
|
*/ |
114
|
|
|
public function getCookieService() |
115
|
|
|
{ |
116
|
|
|
return $this->serviceLocator->get('JwPersistentUser\Service\Cookie'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.