/
UsersController.php
441 lines (405 loc) · 13.2 KB
/
UsersController.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* Users Controller
*
* PHP version 5
*
* @category Controller
* @package Croogo
* @version 1.0
* @author Fahad Ibnay Heylaal <contact@fahad19.com>
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://www.croogo.org
*/
class UsersController extends AppController {
/**
* Controller name
*
* @var string
* @access public
*/
public $name = 'Users';
/**
* Components
*
* @var array
* @access public
*/
public $components = array(
'Email',
);
/**
* Models used by the Controller
*
* @var array
* @access public
*/
public $uses = array('User');
/**
* beforeFilter
*
* @return void
* @access public
*/
public function beforeFilter() {
parent::beforeFilter();
if ($this->request->is('post') && in_array($this->request->params['action'], array('admin_login', 'login'))) {
$field = $this->Auth->authenticate['all']['fields']['username'];
if (!empty($this->request->data) && empty($this->request->data['User'][$field])) {
$this->redirect(array('action' => $this->request->params['action']));
}
$cacheName = 'auth_failed_' . $this->request->data['User'][$field];
if (Cache::read($cacheName, 'users_login') >= Configure::read('User.failed_login_limit')) {
$this->Session->setFlash(__('You have reached maximum limit for failed login attempts. Please try again after a few minutes.'), 'default', array('class' => 'error'));
$this->redirect(array('action' => $this->request->params['action']));
}
}
}
/**
* beforeRender
*
* @return void
* @access public
*/
public function beforeRender() {
parent::beforeRender();
if (in_array($this->request->params['action'], array('admin_login', 'login'))) {
if ($this->request->is('post') && !empty($this->request->data)) {
$field = $this->Auth->authenticate['all']['fields']['username'];
$cacheName = 'auth_failed_' . $this->request->data['User'][$field];
$cacheValue = Cache::read($cacheName, 'users_login');
Cache::write($cacheName, (int)$cacheValue + 1, 'users_login');
}
}
}
/**
* Admin index
*
* @return void
* @access public
*/
public function admin_index() {
$this->set('title_for_layout', __('Users'));
$this->User->recursive = 0;
$this->set('users', $this->paginate());
$this->set('displayFields', $this->User->displayFields());
}
/**
* Admin add
*
* @return void
* @access public
*/
public function admin_add() {
if (!empty($this->request->data)) {
$this->User->create();
$this->request->data['User']['activation_key'] = md5(uniqid());
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The User has been saved'), 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The User could not be saved. Please, try again.'), 'default', array('class' => 'error'));
unset($this->request->data['User']['password']);
}
} else {
$this->request->data['User']['role_id'] = 2; // default Role: Registered
}
$roles = $this->User->Role->find('list');
$this->set(compact('roles'));
}
/**
* Admin edit
*
* @param integer $id
* @return void
* @access public
*/
public function admin_edit($id = null) {
if (!empty($this->request->data)) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The User has been saved'), 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The User could not be saved. Please, try again.'), 'default', array('class' => 'error'));
}
} else {
$this->request->data = $this->User->read(null, $id);
}
$roles = $this->User->Role->find('list');
$this->set(compact('roles'));
$this->set('editFields', $this->User->editFields());
}
/**
* Admin reset password
*
* @param integer $id
* @return void
* @access public
*/
public function admin_reset_password($id = null) {
if (!$id && empty($this->request->data)) {
$this->Session->setFlash(__('Invalid User'), 'default', array('class' => 'error'));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->request->data)) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('Password has been reset.'), 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Password could not be reset. Please, try again.'), 'default', array('class' => 'error'));
}
}
if (empty($this->request->data)) {
$this->request->data = $this->User->read(null, $id);
}
}
/**
* Admin delete
*
* @param integer $id
* @return void
* @access public
*/
public function admin_delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for User'), 'default', array('class' => 'error'));
$this->redirect(array('action' => 'index'));
}
if ($this->User->delete($id)) {
$this->Session->setFlash(__('User deleted'), 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('User cannot be deleted'), 'default', array('class' => 'error'));
$this->redirect(array('action' => 'index'));
}
}
/**
* Admin login
*
* @return void
* @access public
*/
public function admin_login() {
$this->set('title_for_layout', __('Admin Login'));
$this->layout = "admin_login";
if ($this->request->is('post')) {
if ($this->Auth->login()) {
Croogo::dispatchEvent('Controller.Users.adminLoginSuccessful', $this);
return $this->redirect($this->Auth->redirect());
} else {
Croogo::dispatchEvent('Controller.Users.adminLoginFailure', $this);
$this->Session->setFlash($this->Auth->authError, 'default', array(), 'auth');
$this->redirect($this->Auth->loginAction);
}
}
}
/**
* Admin logout
*
* @return void
* @access public
*/
public function admin_logout() {
Croogo::dispatchEvent('Controller.Users.adminLogoutSuccessful', $this);
$this->Session->setFlash(__('Log out successful.'), 'default', array('class' => 'success'));
$this->redirect($this->Auth->logout());
}
/**
* Index
*
* @return void
* @access public
*/
public function index() {
$this->set('title_for_layout', __('Users'));
}
/**
* Add
*
* @return void
* @access public
*/
public function add() {
$this->set('title_for_layout', __('Register'));
if (!empty($this->request->data)) {
$this->User->create();
$this->request->data['User']['role_id'] = 2; // Registered
$this->request->data['User']['activation_key'] = md5(uniqid());
$this->request->data['User']['status'] = 0;
$this->request->data['User']['username'] = htmlspecialchars($this->request->data['User']['username']);
$this->request->data['User']['website'] = htmlspecialchars($this->request->data['User']['website']);
$this->request->data['User']['name'] = htmlspecialchars($this->request->data['User']['name']);
if ($this->User->save($this->request->data)) {
Croogo::dispatchEvent('Controller.Users.registrationSuccessful', $this);
$this->request->data['User']['password'] = null;
$this->Email->from = Configure::read('Site.title') . ' ' .
'<croogo@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])) . '>';
$this->Email->to = $this->request->data['User']['email'];
$this->Email->subject = __('[%s] Please activate your account', Configure::read('Site.title'));
$this->Email->template = 'register';
$this->set('user', $this->request->data);
$this->Email->send();
$this->Session->setFlash(__('You have successfully registered an account. An email has been sent with further instructions.'), 'default', array('class' => 'success'));
$this->redirect(array('action' => 'login'));
} else {
Croogo::dispatchEvent('Controller.Users.registrationFailure', $this);
$this->Session->setFlash(__('The User could not be saved. Please, try again.'), 'default', array('class' => 'error'));
}
}
}
/**
* Activate
*
* @param string $username
* @param string $key
* @return void
* @access public
*/
public function activate($username = null, $key = null) {
if ($username == null || $key == null) {
$this->redirect(array('action' => 'login'));
}
if ($this->User->hasAny(array(
'User.username' => $username,
'User.activation_key' => $key,
'User.status' => 0,
))) {
$user = $this->User->findByUsername($username);
$this->User->id = $user['User']['id'];
$this->User->saveField('status', 1);
$this->User->saveField('activation_key', md5(uniqid()));
Croogo::dispatchEvent('Controller.Users.activationSuccessful', $this);
$this->Session->setFlash(__('Account activated successfully.'), 'default', array('class' => 'success'));
} else {
Croogo::dispatchEvent('Controller.Users.activationFailure', $this);
$this->Session->setFlash(__('An error occurred.'), 'default', array('class' => 'error'));
}
$this->redirect(array('action' => 'login'));
}
/**
* Edit
*
* @return void
* @access public
*/
public function edit() {
}
/**
* Forgot
*
* @return void
* @access public
*/
public function forgot() {
$this->set('title_for_layout', __('Forgot Password'));
if (!empty($this->request->data) && isset($this->request->data['User']['username'])) {
$user = $this->User->findByUsername($this->request->data['User']['username']);
if (!isset($user['User']['id'])) {
$this->Session->setFlash(__('Invalid username.'), 'default', array('class' => 'error'));
$this->redirect(array('action' => 'login'));
}
$this->User->id = $user['User']['id'];
$activationKey = md5(uniqid());
$this->User->saveField('activation_key', $activationKey);
$this->set(compact('user', 'activationKey'));
$this->Email->from = Configure::read('Site.title') . ' ' .
'<croogo@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])) . '>';
$this->Email->to = $user['User']['email'];
$this->Email->subject = __('[%s] Reset Password', Configure::read('Site.title'));
$this->Email->template = 'forgot_password';
if ($this->Email->send()) {
$this->Session->setFlash(__('An email has been sent with instructions for resetting your password.'), 'default', array('class' => 'success'));
$this->redirect(array('action' => 'login'));
} else {
$this->Session->setFlash(__('An error occurred. Please try again.'), 'default', array('class' => 'error'));
}
}
}
/**
* Reset
*
* @param string $username
* @param string $key
* @return void
* @access public
*/
public function reset($username = null, $key = null) {
$this->set('title_for_layout', __('Reset Password'));
if ($username == null || $key == null) {
$this->Session->setFlash(__('An error occurred.'), 'default', array('class' => 'error'));
$this->redirect(array('action' => 'login'));
}
$user = $this->User->find('first', array(
'conditions' => array(
'User.username' => $username,
'User.activation_key' => $key,
),
));
if (!isset($user['User']['id'])) {
$this->Session->setFlash(__('An error occurred.'), 'default', array('class' => 'error'));
$this->redirect(array('action' => 'login'));
}
if (!empty($this->request->data) && isset($this->request->data['User']['password'])) {
$this->User->id = $user['User']['id'];
$user['User']['activation_key'] = md5(uniqid());
$user['User']['password'] = $this->request->data['User']['password'];
$user['User']['verify_password'] = $this->request->data['User']['verify_password'];
$options = array('fieldList' => array('password', 'verify_password', 'activation_key'));
if ($this->User->save($user['User'], $options)) {
$this->Session->setFlash(__('Your password has been reset successfully.'), 'default', array('class' => 'success'));
$this->redirect(array('action' => 'login'));
} else {
$this->Session->setFlash(__('An error occurred. Please try again.'), 'default', array('class' => 'error'));
}
}
$this->set(compact('user', 'username', 'key'));
}
/**
* Login
*
* @return boolean
* @access public
*/
public function login() {
$this->set('title_for_layout', __('Log in'));
if ($this->request->is('post')) {
Croogo::dispatchEvent('Controller.Users.beforeLogin', $this);
if ($this->Auth->login()) {
Croogo::dispatchEvent('Controller.Users.loginSuccessful', $this);
return $this->redirect($this->Auth->redirect());
} else {
Croogo::dispatchEvent('Controller.Users.loginFailure', $this);
$this->Session->setFlash($this->Auth->authError, 'default', array(), 'auth');
$this->redirect($this->Auth->loginAction);
}
}
}
/**
* Logout
*
* @return void
* @access public
*/
public function logout() {
Croogo::dispatchEvent('Controller.Users.beforeLogout', $this);
$this->Session->setFlash(__('Log out successful.'), 'default', array('class' => 'success'));
$this->redirect($this->Auth->logout());
Croogo::dispatchEvent('Controller.Users.afterLogout', $this);
}
/**
* View
*
* @param string $username
* @return void
* @access public
*/
public function view($username) {
$user = $this->User->findByUsername($username);
if (!isset($user['User']['id'])) {
$this->Session->setFlash(__('Invalid User.'), 'default', array('class' => 'error'));
$this->redirect('/');
}
$this->set('title_for_layout', $user['User']['name']);
$this->set(compact('user'));
}
}