1 | <?php |
||
13 | class AdapterChain extends EventProvider implements AdapterInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var AdapterChainEvent |
||
17 | */ |
||
18 | protected $event; |
||
19 | |||
20 | /** |
||
21 | * Returns the authentication result |
||
22 | * |
||
23 | * @return AuthenticationResult |
||
24 | */ |
||
25 | public function authenticate() |
||
26 | { |
||
27 | $e = $this->getEvent(); |
||
28 | |||
29 | $result = new AuthenticationResult( |
||
30 | $e->getCode(), |
||
31 | $e->getIdentity(), |
||
32 | $e->getMessages() |
||
33 | ); |
||
34 | |||
35 | $this->resetAdapters(); |
||
36 | |||
37 | return $result; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * prepareForAuthentication |
||
42 | * |
||
43 | * @param Request $request |
||
44 | * @return Response|bool |
||
45 | * @throws Exception\AuthenticationEventException |
||
46 | */ |
||
47 | public function prepareForAuthentication(Request $request) |
||
48 | { |
||
49 | $e = $this->getEvent(); |
||
50 | $e->setRequest($request); |
||
51 | |||
52 | $this->getEventManager()->trigger('authenticate.pre', $e); |
||
53 | |||
54 | $result = $this->getEventManager()->trigger('authenticate', $e, function ($test) { |
||
55 | return ($test instanceof Response); |
||
56 | }); |
||
57 | |||
58 | if ($result->stopped()) { |
||
59 | if ($result->last() instanceof Response) { |
||
60 | return $result->last(); |
||
61 | } |
||
62 | |||
63 | throw new Exception\AuthenticationEventException( |
||
64 | sprintf( |
||
65 | 'Auth event was stopped without a response. Got "%s" instead', |
||
66 | is_object($result->last()) ? get_class($result->last()) : gettype($result->last()) |
||
67 | ) |
||
68 | ); |
||
69 | } |
||
70 | |||
71 | if ($e->getIdentity()) { |
||
72 | $this->getEventManager()->trigger('authenticate.success', $e); |
||
73 | return true; |
||
74 | } |
||
75 | |||
76 | $this->getEventManager()->trigger('authenticate.fail', $e); |
||
77 | return false; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * resetAdapters |
||
82 | * |
||
83 | * @return AdapterChain |
||
84 | */ |
||
85 | public function resetAdapters() |
||
96 | |||
97 | /** |
||
98 | * logoutAdapters |
||
99 | * |
||
100 | * @return AdapterChain |
||
101 | */ |
||
102 | public function logoutAdapters() |
||
107 | |||
108 | /** |
||
109 | * Get the auth event |
||
110 | * |
||
111 | * @return AdapterChainEvent |
||
112 | */ |
||
113 | public function getEvent() |
||
121 | |||
122 | /** |
||
123 | * Set an event to use during dispatch |
||
124 | * |
||
125 | * By default, will re-cast to AdapterChainEvent if another event type is provided. |
||
126 | * |
||
127 | * @param Event $e |
||
128 | * @return AdapterChain |
||
129 | */ |
||
130 | public function setEvent(Event $e) |
||
140 | } |
||
141 |