|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LmcUser\Authentication\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Laminas\Authentication\Adapter\AdapterInterface; |
|
6
|
|
|
use Laminas\Authentication\Result as AuthenticationResult; |
|
7
|
|
|
use Laminas\EventManager\Event; |
|
8
|
|
|
use Laminas\EventManager\EventInterface; |
|
9
|
|
|
use Laminas\EventManager\EventManagerAwareTrait; |
|
10
|
|
|
use Laminas\Stdlib\RequestInterface as Request; |
|
11
|
|
|
use Laminas\Stdlib\ResponseInterface as Response; |
|
12
|
|
|
use LmcUser\Exception; |
|
13
|
|
|
|
|
14
|
|
|
class AdapterChain implements AdapterInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use EventManagerAwareTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var AdapterChainEvent |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $event; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Returns the authentication result |
|
25
|
|
|
* |
|
26
|
|
|
* @return AuthenticationResult |
|
27
|
|
|
*/ |
|
28
|
|
|
public function authenticate() |
|
29
|
|
|
{ |
|
30
|
|
|
$e = $this->getEvent(); |
|
31
|
|
|
|
|
32
|
|
|
$result = new AuthenticationResult( |
|
33
|
|
|
$e->getCode(), |
|
34
|
|
|
$e->getIdentity(), |
|
35
|
|
|
$e->getMessages() |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
$this->resetAdapters(); |
|
39
|
|
|
|
|
40
|
|
|
return $result; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* prepareForAuthentication |
|
45
|
|
|
* |
|
46
|
|
|
* @param Request $request |
|
47
|
|
|
* @return Response|bool |
|
48
|
|
|
* @throws Exception\AuthenticationEventException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function prepareForAuthentication(Request $request) |
|
51
|
|
|
{ |
|
52
|
|
|
$e = $this->getEvent(); |
|
53
|
|
|
$e->setRequest($request); |
|
54
|
|
|
|
|
55
|
|
|
$e->setName('authenticate.pre'); |
|
56
|
|
|
$this->getEventManager()->triggerEvent($e); |
|
57
|
|
|
|
|
58
|
|
|
$e->setName('authenticate'); |
|
59
|
|
|
$result = $this->getEventManager()->triggerEventUntil( |
|
60
|
|
|
function ($test) { |
|
61
|
|
|
return ($test instanceof Response); |
|
62
|
|
|
}, |
|
63
|
|
|
$e |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
if ($result && $result->stopped()) { |
|
67
|
|
|
if ($result->last() instanceof Response) { |
|
68
|
|
|
return $result->last(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
throw new Exception\AuthenticationEventException( |
|
72
|
|
|
sprintf( |
|
73
|
|
|
'Auth event was stopped without a response. Got "%s" instead', |
|
74
|
|
|
is_object($result->last()) ? get_class($result->last()) : gettype($result->last()) |
|
75
|
|
|
) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($e->getIdentity()) { |
|
80
|
|
|
$e->setName('authenticate.success'); |
|
81
|
|
|
$this->getEventManager()->triggerEvent($e); |
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$e->setName('authenticate.fail'); |
|
86
|
|
|
$this->getEventManager()->triggerEvent($e); |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* resetAdapters |
|
93
|
|
|
* |
|
94
|
|
|
* @return AdapterChain |
|
95
|
|
|
*/ |
|
96
|
|
|
public function resetAdapters() |
|
97
|
|
|
{ |
|
98
|
|
|
$sharedManager = $this->getEventManager()->getSharedManager(); |
|
99
|
|
|
|
|
100
|
|
|
if ($sharedManager) { |
|
101
|
|
|
$listeners = $sharedManager->getListeners(['authenticate'], 'authenticate'); |
|
102
|
|
|
foreach ($listeners as $listener) { |
|
103
|
|
|
if (is_array($listener) && $listener[0] instanceof ChainableAdapter) { |
|
104
|
|
|
$listener[0]->getStorage()->clear(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* logoutAdapters |
|
114
|
|
|
* |
|
115
|
|
|
* @return AdapterChain |
|
116
|
|
|
*/ |
|
117
|
|
|
public function logoutAdapters() |
|
118
|
|
|
{ |
|
119
|
|
|
//Adapters might need to perform additional cleanup after logout |
|
120
|
|
|
$e = $this->getEvent(); |
|
121
|
|
|
$e->setName('logout'); |
|
122
|
|
|
$this->getEventManager()->triggerEvent($e); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get the auth event |
|
127
|
|
|
* |
|
128
|
|
|
* @return AdapterChainEvent |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getEvent() |
|
131
|
|
|
{ |
|
132
|
|
|
if (null === $this->event) { |
|
133
|
|
|
$this->setEvent(new AdapterChainEvent); |
|
134
|
|
|
$this->event->setTarget($this); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $this->event; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Set an event to use during dispatch |
|
142
|
|
|
* |
|
143
|
|
|
* By default, will re-cast to AdapterChainEvent if another event type is provided. |
|
144
|
|
|
* |
|
145
|
|
|
* @param Event $e |
|
146
|
|
|
* @return AdapterChain |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setEvent(Event $e) |
|
149
|
|
|
{ |
|
150
|
|
|
if (!$e instanceof AdapterChainEvent) { |
|
151
|
|
|
$eventParams = $e->getParams(); |
|
152
|
|
|
$e = new AdapterChainEvent(); |
|
153
|
|
|
$e->setParams($eventParams); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$this->event = $e; |
|
157
|
|
|
|
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|