/
UserController.php
262 lines (217 loc) · 8.74 KB
/
UserController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/
namespace ZfcUser\Controller;
use Zend\Form\Form;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Stdlib\Parameters;
use Zend\View\Model\ViewModel;
use ZfcUser\Service\User as UserService;
use ZfcUser\Options\UserControllerOptionsInterface;
class UserController extends AbstractActionController
{
const ROUTE_LOGIN = 'zfcuser/login';
const ROUTE_REGISTER = 'zfcuser/register';
const CONTROLLER_NAME = 'zfcuser';
/**
* @var UserService
*/
protected $userService;
/**
* @var Form
*/
protected $loginForm;
/**
* @var Form
*/
protected $registerForm;
/**
* @todo Make this dynamic / translation-friendly
* @var string
*/
protected $failedLoginMessage = 'Authentication failed. Please try again.';
/**
* @var string
*/
protected $loginNamespace = 'zfcuser-login-form';
/**
* @var UserControllerOptionsInterface
*/
protected $options;
public function __construct($userService, $options, $registerForm, $loginForm)
{
$this->userService = $userService;
$this->options = $options;
$this->registerForm = $registerForm;
$this->loginForm = $loginForm;
}
/**
* User page
*/
public function indexAction()
{
if (!$this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute(static::ROUTE_LOGIN);
}
return new ViewModel();
}
/**
* Login form
*/
public function loginAction()
{
if ($this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute($this->options->getLoginRedirectRoute());
}
$request = $this->getRequest();
$post = $request->getPost();
$form = $this->loginForm;
$fm = $this->flashMessenger()->setNamespace($this->loginNamespace)->getMessages();
if (isset($fm[0])) {
$this->loginForm->setMessages(
array('identity' => array($fm[0]))
);
}
if ($this->options->getUseRedirectParameterIfPresent()) {
$redirect = $request->getQuery()->get('redirect', (!empty($post['redirect'])) ? $post['redirect'] : false);
} else {
$redirect = false;
}
if (!$request->isPost()) {
return array(
'loginForm' => $form,
'redirect' => $redirect,
'enableRegistration' => $this->options->getEnableRegistration(),
);
}
$form->setData($post);
if (!$form->isValid()) {
$this->flashMessenger()->setNamespace($this->loginNamespace)->addMessage($this->failedLoginMessage);
return $this->redirect()->toUrl($this->url()->fromRoute(static::ROUTE_LOGIN).($redirect ? '?redirect='. rawurlencode($redirect) : ''));
}
// clear adapters
$this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
$this->zfcUserAuthentication()->getAuthService()->clearIdentity();
return $this->forward()->dispatch(static::CONTROLLER_NAME, array('action' => 'authenticate'));
}
/**
* Logout and clear the identity
*/
public function logoutAction()
{
$this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
$this->zfcUserAuthentication()->getAuthAdapter()->logoutAdapters();
$this->zfcUserAuthentication()->getAuthService()->clearIdentity();
$redirect = $this->params()->fromPost('redirect', $this->params()->fromQuery('redirect', false));
if ($this->options->getUseRedirectParameterIfPresent() && $redirect) {
return $this->redirect()->toRoute($redirect);
}
return $this->redirect()->toRoute($this->options->getLogoutRedirectRoute());
}
/**
* General-purpose authentication action
*/
public function authenticateAction()
{
if ($this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute($this->options->getLoginRedirectRoute());
}
$adapter = $this->zfcUserAuthentication()->getAuthAdapter();
$redirect = $this->params()->fromPost('redirect', $this->params()->fromQuery('redirect', false));
$result = $adapter->prepareForAuthentication($this->getRequest());
// Return early if an adapter returned a response
if ($result instanceof Response) {
return $result;
}
$auth = $this->zfcUserAuthentication()->getAuthService()->authenticate($adapter);
if (!$auth->isValid()) {
$this->flashMessenger()->setNamespace($this->loginNamespace)->addMessage($this->failedLoginMessage);
$adapter->resetAdapters();
return $this->redirect()->toUrl(
$this->url()->fromRoute(static::ROUTE_LOGIN) .
($redirect ? '?redirect='. rawurlencode($redirect) : '')
);
}
if ($this->options->getUseRedirectParameterIfPresent() && $redirect) {
return $this->redirect()->toRoute($redirect);
}
$route = $this->options->getLoginRedirectRoute();
if (is_callable($route)) {
$route = $route($this->zfcUserAuthentication()->getIdentity());
}
return $this->redirect()->toRoute($route);
}
/**
* Register new user
*/
public function registerAction()
{
// if the user is logged in, we don't need to register
if ($this->zfcUserAuthentication()->hasIdentity()) {
// redirect to the login redirect route
return $this->redirect()->toRoute($this->options->getLoginRedirectRoute());
}
// if registration is disabled
if (!$this->options->getEnableRegistration()) {
return array('enableRegistration' => false);
}
$request = $this->getRequest();
$service = $this->userService;
$form = $this->registerForm;
if ($this->options->getUseRedirectParameterIfPresent() && $request->getQuery()->get('redirect')) {
$redirect = $request->getQuery()->get('redirect');
} else {
$redirect = false;
}
$redirectUrl = $this->url()->fromRoute(static::ROUTE_REGISTER)
. ($redirect ? '?redirect=' . rawurlencode($redirect) : '');
$prg = $this->prg($redirectUrl, true);
if ($prg instanceof Response) {
return $prg;
} elseif ($prg === false) {
return array(
'registerForm' => $form,
'enableRegistration' => $this->options->getEnableRegistration(),
'redirect' => $redirect,
);
}
$post = $prg;
$user = $service->register($post);
$redirect = isset($prg['redirect']) ? $prg['redirect'] : null;
if (!$user) {
return array(
'registerForm' => $form,
'enableRegistration' => $this->options->getEnableRegistration(),
'redirect' => $redirect,
);
}
if ($service->getOptions()->getLoginAfterRegistration()) {
$identityFields = $service->getOptions()->getAuthIdentityFields();
if (in_array('email', $identityFields)) {
$post['identity'] = $user->getEmail();
} elseif (in_array('username', $identityFields)) {
$post['identity'] = $user->getUsername();
}
$post['credential'] = $post['password'];
$request->setPost(new Parameters($post));
return $this->forward()->dispatch(static::CONTROLLER_NAME, array('action' => 'authenticate'));
}
// TODO: Add the redirect parameter here...
return $this->redirect()->toUrl($this->url()->fromRoute(static::ROUTE_LOGIN) . ($redirect ? '?redirect='. rawurlencode($redirect) : ''));
}
}