Repository navigation
Expand file tree
/
Copy pathUsersController.php
More file actions
340 lines (284 loc) · 10.7 KB
/
Copy pathUsersController.php
File metadata and controls
340 lines (284 loc) · 10.7 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* CakeManager (http://cakemanager.org)
* Copyright (c) http://cakemanager.org
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) http://cakemanager.org
* @link http://cakemanager.org CakeManager Project
* @since 1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace CakeManager\Controller;
use CakeManager\Controller\AppController;
use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
use Cake\Event\Event;
/**
* Users Controller
*
*/
class UsersController extends AppController
{
/**
* BeforeFilter Callback
*
* @param \Cake\Event\Event $event Event.
* @return void
*/
public function beforeFilter(\Cake\Event\Event $event)
{
parent::beforeFilter($event);
// Auth-settings: Allows all user-related methods
$this->Auth->allow(['resetPassword', 'forgotPassword', 'logout', 'login', 'activate', 'register']);
// Setting up the base-theme
if (!$this->theme) {
$this->theme = "CakeManager";
}
// Setting up the base-layout
if (!$this->layout) {
$this->layout = "base";
}
$this->set('title', 'Users');
}
/**
* Register action
*
* # Enabling / Disabling register action
*
* Configure::write('CM.Register', true);
*
* @return type
*/
public function register()
{
if (!Configure::read('CM.Register')) {
return $this->redirect('/');
}
// Redirect if user is already logged in
if ($this->authUser) {
return $this->redirect('/login');
}
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
$user->set('role_id', 3);
$user->set('active', true);
if (Configure::read('CM.ActivationOnRegister')) {
$user->set('activation_key', $this->Users->generateActivationKey());
$user->set('active', false);
}
if ($this->Users->save($user)) {
// firing an event: afterRegister
$_event = new Event('Controller.Users.afterRegister', $this, [
'user' => $user
]);
$this->eventManager()->dispatch($_event);
// firing an event: afterInvalidRegister
$_event = new Event('Controller.Users.afterInvalidRegister', $this, [
'user' => $user
]);
$this->eventManager()->dispatch($_event);
$this->Flash->success(__('Registered succesfully!'));
return $this->redirect(['action' => 'login']);
} else {
$this->Flash->error(__('Could not register. Please, try again.'));
}
}
$this->set(compact('user'));
$this->render(Configure::read('CM.UserViews.register'));
}
/**
* Login action
*
* # Changing the view:
*
* Configure::write('CM.UserViews.login', 'custom/view');
*
* @return void|\Cake\Network\Response
*/
public function login()
{
$this->loadModel('CakeManager.Roles');
// Redirect if user is already logged in
if ($this->authUser) {
$redirect = $this->Roles->redirectFrom($this->authUser['role_id']);
return $this->redirect($redirect);
}
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
$redirect = $this->Roles->redirectFrom($user['role_id']);
// firing an event: afterLogin
$_event = new Event('Controller.Users.afterLogin', $this, [
'user' => $user
]);
$this->eventManager()->dispatch($_event);
return $this->redirect($redirect);
}
// firing an event: afterInvalidLogin
$_event = new Event('Controller.Users.afterInvalidLogin', $this, [
'user' => $user
]);
$this->eventManager()->dispatch($_event);
$this->Flash->error(__('Your username or password is incorrect.'));
}
$this->set('registerEnabled', Configure::read('CM.Register'));
$this->render(Configure::read('CM.UserViews.login'));
}
/**
* Forgot password action
*
* Via this action you are able to request a new password.
* After the post it will send a mail with a link.
* This link will redirect to the action 'reset_password'.
*
* This action always gives a success-message.
* That's because else hackers (or other bad-guys) will be able
* to see if an e-mail is registered or not.
*
* # Changing the view:
*
* Configure::write('CM.UserViews.forgot_password', 'custom/view');
*
* @return void|\Cake\Network\Response
*/
public function forgotPassword()
{
// Redirect if user is already logged in
if ($this->authUser) {
return $this->redirect('/login');
}
if ($this->request->is('post')) {
$user = $this->Users->findByEmail($this->request->data['email']);
if ($user->Count()) {
$user = $user->first();
if ($user->get('active') === 1) {
$user->set('activation_key', $this->Users->generateActivationKey());
$this->Users->save($user);
// firing an event: afterForgotPassword
$_event = new Event('Controller.Users.afterForgotPassword', $this, [
'user' => $user
]);
$this->eventManager()->dispatch($_event);
}
}
// Always return a success-message. Else hackers will be able to see if an e-mail is registered or not
$this->Flash->success(__('We have sent you a mail. Check your e-mailaddress to activate your account.'));
return $this->redirect('/login');
}
$this->render(Configure::read('CM.UserViews.forgotPassword'));
}
/**
* Activate action
*
* Users will reach this action when they need to activate their account.
* Ths action will activate the account and redirect to the login-page.
*
* @param string $email The e-mailaddress from the user.
* @param string $activationKey The refering activation key.
* @return void|\Cake\Network\Response
*/
public function activate($email, $activationKey = null)
{
// If there's no activation_key
if (!$activationKey) {
$this->Flash->error(__('Your account could not be activated.'));
return $this->redirect($this->referer());
}
// Redirect if user is already logged in
if ($this->authUser) {
return $this->redirect('/login');
}
// If the email and key doesn't match
if (!$this->Users->validateActivationKey($email, $activationKey)) {
$this->Flash->error(__('Your account could not be activated.'));
return $this->redirect('/login');
}
// If the user has been activated
if ($this->Users->activateUser($email, $activationKey)) {
$this->Flash->success(__('Congratulations! Your account has been activated!'));
// firing an event: afterActivate
$_event = new Event('Controller.Users.afterActivate', $this, [
'email' => $email
]);
$this->eventManager()->dispatch($_event);
return $this->redirect('/login');
}
// If noting happened. Just for safety :)
$this->Flash->error(__('Your account could not be activated.'));
return $this->redirect('/login');
}
/**
* Reset password action
*
* Users will reach this action when they need to set a new password for their account.
* This action will set a new password and redirect to the login page
*
* # Changing the view:
*
* Configure::write('CM.UserViews.reset_password', 'custom/view');
*
* @param string $email The e-mailaddress from the user.
* @param string $activationKey The refering activation key.
* @return void|\Cake\Network\Response
*/
public function resetPassword($email, $activationKey = null)
{
// If there's no activation_key
if (!$activationKey) {
$this->Flash->error(__('Your account could not be activated.'));
return $this->redirect($this->referer());
}
// Redirect if user is already logged in
if ($this->authUser) {
return $this->redirect('/login');
}
// If the email and key doesn't match
if (!$this->Users->validateActivationKey($email, $activationKey)) {
$this->Flash->error(__('Your account could not be activated.'));
return $this->redirect('/login');
}
// If we passed and the POST isset
if ($this->request->is('post')) {
$data = $this->Users->find()->where([
'email' => $email,
'activation_key' => $activationKey,
]);
if ($data->Count() > 0) {
$data = $data->first();
$user = $this->Users->patchEntity($data, $this->request->data);
$user->set('active', 1);
$user->set('activation_key', null);
if ($this->Users->save($user)) {
// firing an event: afterResetPassword
$_event = new Event('Controller.Users.afterResetPassword', $this, [
'user' => $user
]);
$this->eventManager()->dispatch($_event);
$this->Flash->success(__('Your password has been changed.'));
return $this->redirect('/login');
}
}
$this->Flash->error(__('Your account could not be activated.'));
}
$this->render(Configure::read('CM.UserViews.resetPassword'));
}
/**
* Logout method
*
* This method logs the user out and redirects to the login-page.
* The login-page is chosen by the AuthComponent
*
* @return void|\Cake\Network\Response
*/
public function logout()
{
$this->Flash->success(__('You are now logged out.'));
return $this->redirect($this->Auth->logout());
}
}