forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUsersController.php
More file actions
909 lines (820 loc) · 25.1 KB
/
Copy pathUsersController.php
File metadata and controls
909 lines (820 loc) · 25.1 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
<?php
/**
* Copyright 2010 - 2014, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2014, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('CakeEmail', 'Network/Email');
App::uses('UsersAppController', 'Users.Controller');
/**
* Users Users Controller
*
* @package Users
* @subpackage Users.Controller
* @property AuthComponent $Auth
* @property CookieComponent $Cookie
* @property PaginatorComponent $Paginator
* @property SecurityComponent $Security
* @property SessionComponent $Session
* @property User $User
* @property RememberMeComponent $RememberMe
*/
class UsersController extends UsersAppController {
/**
* Controller name
*
* @var string
*/
public $name = 'Users';
/**
* If the controller is a plugin controller set the plugin name
*
* @var mixed
*/
public $plugin = null;
/**
* Helpers
*
* @var array
*/
public $helpers = array(
'Html',
'Form',
'Session',
'Time',
'Text'
);
/**
* Components
*
* @var array
*/
public $components = array(
'Auth',
'Session',
'Cookie',
'Paginator',
'Security',
'Search.Prg',
'Users.RememberMe',
);
/**
* Preset vars
*
* @var array $presetVars
* @link https://github.com/CakeDC/search
*/
public $presetVars = true;
/**
* Constructor
*
* @param CakeRequest $request Request object for this controller. Can be null for testing,
* but expect that features that use the request parameters will not work.
* @param CakeResponse $response Response object for this controller.
*/
public function __construct($request, $response) {
$this->_setupComponents();
parent::__construct($request, $response);
$this->_reInitControllerName();
}
/**
* Providing backward compatibility to a fix that was just made recently to the core
* for users that want to upgrade the plugin but not the core
*
* @link http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3550-inherited-controllers-get-wrong-property-names
* @return void
*/
protected function _reInitControllerName() {
$name = substr(get_class($this), 0, -10);
if ($this->name === null) {
$this->name = $name;
} elseif ($name !== $this->name) {
$this->name = $name;
}
}
/**
* Returns $this->plugin with a dot, used for plugin loading using the dot notation
*
* @return mixed string|null
*/
protected function _pluginDot() {
if (is_string($this->plugin)) {
return $this->plugin . '.';
}
return $this->plugin;
}
/**
* Wrapper for CakePlugin::loaded()
*
* @throws MissingPluginException
* @param string $plugin
* @param boolean $exceiption
* @return boolean
*/
protected function _pluginLoaded($plugin, $exception = true) {
$result = CakePlugin::loaded($plugin);
if ($exception === true && $result === false) {
throw new MissingPluginException(array('plugin' => $plugin));
}
return $result;
}
/**
* Setup components based on plugin availability
*
* @return void
* @link https://github.com/CakeDC/search
*/
protected function _setupComponents() {
if ($this->_pluginLoaded('Search', false)) {
$this->components[] = 'Search.Prg';
}
}
/**
* beforeFilter callback
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
$this->_setupAuth();
$this->_setupPagination();
$this->set('model', $this->modelClass);
$this->_setDefaultEmail();
}
/**
* Sets the default from email config
*
* @return void
*/
protected function _setDefaultEmail() {
if (!Configure::read('App.defaultEmail')) {
$config = $this->_getMailInstance()->config();
if (!empty($config['from'])) {
Configure::write('App.defaultEmail', $config['from']);
} else {
Configure::write('App.defaultEmail', 'noreply@' . env('HTTP_HOST'));
}
}
}
/**
* Sets the default pagination settings up
*
* Override this method or the index action directly if you want to change
* pagination settings.
*
* @return void
*/
protected function _setupPagination() {
$this->Paginator->settings = array(
'limit' => 12,
'conditions' => array(
$this->modelClass . '.active' => 1,
$this->modelClass . '.email_verified' => 1
)
);
}
/**
* Sets the default pagination settings up
*
* Override this method or the index() action directly if you want to change
* pagination settings. admin_index()
*
* @return void
*/
protected function _setupAdminPagination() {
$this->Paginator->settings[$this->modelClass] = array(
'limit' => 20,
'order' => array(
$this->modelClass . '.created' => 'desc'
),
);
}
/**
* Setup Authentication Component
*
* @return void
*/
protected function _setupAuth() {
if (Configure::read('Users.disableDefaultAuth') === true) {
return;
}
//prevent acces to admin routes if not admin
if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
// if ($this->Session->check('Auth.User.role') and 'admin' !== $this->Session->read('Auth.User.role')) {
if(!$this->Session->read('Auth.User.is_admin')) {
$this->Auth->deny('*');
return $this->flash('Non admin access - unauthorized', '/');
}
}
$this->Auth->allow('add', 'reset', 'verify', 'logout', 'view', 'reset_password', 'login', 'resend_verification');
if (!is_null(Configure::read('Users.allowRegistration')) && !Configure::read('Users.allowRegistration')) {
$this->Auth->deny('add');
}
if ($this->request->action == 'register') {
$this->Components->disable('Auth');
}
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array(
'username' => 'email',
'password' => 'password'),
'userModel' => $this->_pluginDot() . $this->modelClass,
'scope' => array(
$this->modelClass . '.active' => 1,
$this->modelClass . '.email_verified' => 1
)
)
);
$this->Auth->loginRedirect = '/';
$this->Auth->logoutRedirect = array('plugin' => Inflector::underscore($this->plugin), 'controller' => 'users', 'action' => 'login');
$this->Auth->loginAction = array('admin' => false, 'plugin' => Inflector::underscore($this->plugin), 'controller' => 'users', 'action' => 'login');
}
/**
* Simple listing of all users
*
* @return void
*/
public function index() {
$this->set('users', $this->Paginator->paginate($this->modelClass));
}
/**
* The homepage of a users giving him an overview about everything
*
* @return void
*/
public function dashboard() {
$user = $this->{$this->modelClass}->read(null, $this->Auth->user('id'));
$this->set('user', $user);
}
/**
* Shows a users profile
*
* @param string $slug User Slug
* @return void
*/
public function view($slug = null) {
try {
$this->set('user', $this->{$this->modelClass}->view($slug));
} catch (Exception $e) {
$this->Session->setFlash($e->getMessage());
$this->redirect('/');
}
}
/**
* Edit the current logged in user
*
* @param string $id User ID
* @return void
*/
public function edit($slug = null) {
// @todo replace this with something better than the user details that were removed
try {
$this->set('user', $this->{$this->modelClass}->view($slug));
if($this->{$this->modelClass}->Detail->hasAny(array('user_id'=>$this->Auth->user('id')))){
$detail = $this->{$this->modelClass}->Detail->find('first',array('user_id'=>$this->Auth->user('id')));
if(!$this->request->data) {
$this->request->data = $detail;
}
}
if ($this->request->is('post')) {
$this->Session->setFlash(__d('users', 'Your profile has been updated'));
$this->request->data['Detail']['user_id'] = $this->Auth->user('id');
$this->{$this->modelClass}->Detail->save($this->request->data);
}else{
}
} catch (Exception $e) {
$this->Session->setFlash($e->getMessage());
$this->redirect('/');
}
}
/**
* Admin Index
*
* @return void
*/
public function admin_index() {
if ($this->{$this->modelClass}->Behaviors->loaded('Searchable')) {
$this->Prg->commonProcess();
unset($this->{$this->modelClass}->validate['username']);
unset($this->{$this->modelClass}->validate['email']);
$this->{$this->modelClass}->data[$this->modelClass] = $this->passedArgs;
}
if ($this->{$this->modelClass}->Behaviors->loaded('Searchable')) {
$parsedConditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs);
} else {
$parsedConditions = array();
}
$this->_setupAdminPagination();
$this->Paginator->settings[$this->modelClass]['conditions'] = $parsedConditions;
$this->set('users', $this->Paginator->paginate());
}
/**
* Admin view
*
* @param string $id User ID
* @return void
*/
public function admin_view($id = null) {
try {
$user = $this->{$this->modelClass}->view($id, 'id');
} catch (NotFoundException $e) {
$this->Session->setFlash(__d('users', 'Invalid User.'));
$this->redirect(array('action' => 'index'));
}
$this->set('user', $user);
}
/**
* Admin add
*
* @return void
*/
public function admin_add() {
if (!empty($this->request->data)) {
$this->request->data[$this->modelClass]['tos'] = true;
$this->request->data[$this->modelClass]['email_verified'] = true;
if ($this->{$this->modelClass}->add($this->request->data)) {
$this->Session->setFlash(__d('users', 'The User has been saved'));
$this->redirect(array('action' => 'index'));
}
}
$this->set('roles', Configure::read('Users.roles'));
}
/**
* Admin edit
*
* @param null $userId
* @return void
*/
public function admin_edit($userId = null) {
try {
$result = $this->{$this->modelClass}->edit($userId, $this->request->data);
if ($result === true) {
$this->Session->setFlash(__d('users', 'User saved'));
$this->redirect(array('action' => 'index'));
} else {
unset($result[$this->modelClass]['password']);
$this->request->data = $result;
}
} catch (OutOfBoundsException $e) {
$this->Session->setFlash($e->getMessage());
$this->redirect(array('action' => 'index'));
}
if (empty($this->request->data)) {
$this->request->data = $this->{$this->modelClass}->read(null, $userId);
unset($this->request->data[$this->modelClass]['password']);
}
$this->set('roles', Configure::read('Users.roles'));
}
/**
* Delete a user account
*
* @param string $userId User ID
* @return void
*/
public function admin_delete($userId = null) {
if ($this->{$this->modelClass}->delete($userId)) {
$this->Session->setFlash(__d('users', 'User deleted'));
} else {
$this->Session->setFlash(__d('users', 'Invalid User'));
}
$this->redirect(array('action' => 'index'));
}
/**
* Search for a user
*
* @return void
*/
public function admin_search() {
$this->search();
}
/**
* User register action
*
* @return void
*/
public function add() {
if ($this->Auth->user()) {
$this->Session->setFlash(__d('users', 'You are already registered and logged in!'));
$this->redirect('/');
}
if (!empty($this->request->data)) {
$user = $this->{$this->modelClass}->register($this->request->data);
if ($user !== false) {
$Event = new CakeEvent(
'Users.Controller.Users.afterRegistration',
$this,
array(
'data' => $this->request->data,
)
);
$this->getEventManager()->dispatch($Event);
if ($Event->isStopped()) {
$this->redirect(array('action' => 'login'));
}
$this->_sendVerificationEmail($this->{$this->modelClass}->data);
$this->Session->setFlash(__d('users', 'Your account has been created. You should receive an e-mail shortly to authenticate your account. Once validated you will be able to login.'));
$this->redirect(array('action' => 'login'));
} else {
unset($this->request->data[$this->modelClass]['password']);
unset($this->request->data[$this->modelClass]['temppassword']);
$this->Session->setFlash(__d('users', 'Your account could not be created. Please, try again.'), 'default', array('class' => 'message warning'));
}
}
}
/**
* Common login action
*
* @return void
*/
public function login() {
$Event = new CakeEvent(
'Users.Controller.Users.beforeLogin',
$this,
array(
'data' => $this->request->data,
)
);
$this->getEventManager()->dispatch($Event);
if ($Event->isStopped()) {
return;
}
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$Event = new CakeEvent(
'Users.Controller.Users.afterLogin',
$this,
array(
'data' => $this->request->data,
'isFirstLogin' => !$this->Auth->user('last_login')
)
);
$this->getEventManager()->dispatch($Event);
$this->{$this->modelClass}->id = $this->Auth->user('id');
$this->{$this->modelClass}->saveField('last_login', date('Y-m-d H:i:s'));
if ($this->here == $this->Auth->loginRedirect) {
$this->Auth->loginRedirect = '/';
}
$this->Session->setFlash(sprintf(__d('users', '%s you have successfully logged in'), $this->Auth->user($this->{$this->modelClass}->displayField)));
if (!empty($this->request->data)) {
$data = $this->request->data[$this->modelClass];
if (empty($this->request->data[$this->modelClass]['remember_me'])) {
$this->RememberMe->destroyCookie();
} else {
$this->_setCookie();
}
}
if (empty($data[$this->modelClass]['return_to'])) {
$data[$this->modelClass]['return_to'] = null;
}
// Checking for 2.3 but keeping a fallback for older versions
if (method_exists($this->Auth, 'redirectUrl')) {
$this->redirect($this->Auth->redirectUrl($data[$this->modelClass]['return_to']));
} else {
$this->redirect($this->Auth->redirect($data[$this->modelClass]['return_to']));
}
} else {
$this->Auth->flash(__d('users', 'Invalid e-mail / password combination. Please try again'));
}
}
if (isset($this->request->params['named']['return_to'])) {
$this->set('return_to', urldecode($this->request->params['named']['return_to']));
} elseif (isset($this->request->query['return_to'])) {
$this->set('return_to', $this->request->query['return_to']);
} else {
$this->set('return_to', false);
}
$allowRegistration = Configure::read('Users.allowRegistration');
$this->set('allowRegistration', (is_null($allowRegistration) ? true : $allowRegistration));
}
/**
* Search - Requires the CakeDC Search plugin to work
*
* @throws MissingPluginException
* @return void
* @link https://github.com/CakeDC/search
*/
public function search() {
$this->_pluginLoaded('Search');
$searchTerm = '';
$this->Prg->commonProcess($this->modelClass);
$by = null;
if (!empty($this->request->params['named']['search'])) {
$searchTerm = $this->request->params['named']['search'];
$by = 'any';
}
if (!empty($this->request->params['named']['username'])) {
$searchTerm = $this->request->params['named']['username'];
$by = 'username';
}
if (!empty($this->request->params['named']['email'])) {
$searchTerm = $this->request->params['named']['email'];
$by = 'email';
}
$this->request->data[$this->modelClass]['search'] = $searchTerm;
$this->Paginator->settings = array(
'search',
'limit' => 12,
'by' => $by,
'search' => $searchTerm,
'conditions' => array(
'AND' => array(
$this->modelClass . '.active' => 1,
$this->modelClass . '.email_verified' => 1
)
)
);
$this->set('users', $this->Paginator->paginate($this->modelClass));
$this->set('searchTerm', $searchTerm);
}
/**
* Common logout action
*
* @return void
*/
public function logout() {
$user = $this->Auth->user();
$this->Session->destroy();
$this->RememberMe->destroyCookie();
$this->Session->setFlash(sprintf(__d('users', '%s you have successfully logged out'), $user[$this->{$this->modelClass}->displayField]));
$this->redirect($this->Auth->logout());
}
/**
* Checks if an email is already verified and if not renews the expiration time
*
* @return void
*/
public function resend_verification() {
if ($this->request->is('post')) {
try {
if ($this->{$this->modelClass}->checkEmailVerification($this->request->data)) {
$this->_sendVerificationEmail($this->{$this->modelClass}->data);
$this->Session->setFlash(__d('users', 'The email was resent. Please check your inbox.'));
$this->redirect('login');
} else {
$this->Session->setFlash(__d('users', 'The email could not be sent. Please check errors.'));
}
} catch (Exception $e) {
$this->Session->setFlash($e->getMessage());
}
}
$allowRegistration = Configure::read('Users.allowRegistration');
$this->set('allowRegistration', (is_null($allowRegistration) ? true : $allowRegistration));
}
/**
* Confirm email action
*
* @param string $type Type, deprecated, will be removed. Its just still there for a smooth transistion.
* @param string $token Token
* @return void
*/
public function verify($type = 'email', $token = null) {
if ($type == 'reset') {
// Backward compatiblity
$this->request_new_password($token);
}
try {
$this->{$this->modelClass}->verifyEmail($token);
$this->Session->setFlash(__d('users', 'Your e-mail has been validated!'));
return $this->redirect(array('action' => 'login'));
} catch (RuntimeException $e) {
$this->Session->setFlash($e->getMessage());
return $this->redirect('/');
}
}
/**
* This method will send a new password to the user
*
* @param string $token Token
* @throws NotFoundException
* @return void
*/
public function request_new_password($token = null) {
if (Configure::read('Users.sendPassword') !== true) {
throw new NotFoundException();
}
$data = $this->{$this->modelClass}->verifyEmail($token);
if (!$data) {
$this->Session->setFlash(__d('users', 'The url you accessed is not longer valid'));
return $this->redirect('/');
}
if ($this->{$this->modelClass}->save($data, array('validate' => false))) {
$this->_sendNewPassword($data);
$this->Session->setFlash(__d('users', 'Your password was sent to your registered email account'));
$this->redirect(array('action' => 'login'));
}
$this->Session->setFlash(__d('users', 'There was an error verifying your account. Please check the email you were sent, and retry the verification link.'));
$this->redirect('/');
}
/**
* Sends the password reset email
*
* @param array
* @return void
*/
protected function _sendNewPassword($userData) {
$Email = $this->_getMailInstance();
$Email->from(Configure::read('App.defaultEmail'))
->to($userData[$this->modelClass]['email'])
->replyTo(Configure::read('App.defaultEmail'))
->return(Configure::read('App.defaultEmail'))
->subject(env('HTTP_HOST') . ' ' . __d('users', 'Password Reset'))
->template($this->_pluginDot() . 'new_password')
->viewVars(array(
'model' => $this->modelClass,
'userData' => $userData))
->send();
}
/**
* Allows the user to enter a new password, it needs to be confirmed by entering the old password
*
* @return void
*/
public function change_password() {
if ($this->request->is('post')) {
$this->request->data[$this->modelClass]['id'] = $this->Auth->user('id');
if ($this->{$this->modelClass}->changePassword($this->request->data)) {
$this->Session->setFlash(__d('users', 'Password changed.'));
// we don't want to keep the cookie with the old password around
$this->RememberMe->destroyCookie();
$this->redirect('/');
}
}
}
/**
* Reset Password Action
*
* Handles the trigger of the reset, also takes the token, validates it and let the user enter
* a new password.
*
* @param string $token Token
* @param string $user User Data
* @return void
*/
public function reset_password($token = null, $user = null) {
if (empty($token)) {
$admin = false;
if ($user) {
$this->request->data = $user;
$admin = true;
}
$this->_sendPasswordReset($admin);
} else {
$this->_resetPassword($token);
}
}
/**
* Sets a list of languages to the view which can be used in selects
*
* @deprecated No fallback provided, use the Utils plugin in your app directly
* @param string $viewVar View variable name, default is languages
* @throws MissingPluginException
* @return void
* @link https://github.com/CakeDC/utils
*/
protected function _setLanguages($viewVar = 'languages') {
$this->_pluginLoaded('Utils');
$Languages = new Languages();
$this->set($viewVar, $Languages->lists('locale'));
}
/**
* Sends the verification email
*
* This method is protected and not private so that classes that inherit this
* controller can override this method to change the varification mail sending
* in any possible way.
*
* @param string $to Receiver email address
* @param array $options EmailComponent options
* @return void
*/
protected function _sendVerificationEmail($userData, $options = array()) {
$defaults = array(
'from' => Configure::read('App.defaultEmail'),
'subject' => __d('users', 'Account verification'),
'template' => $this->_pluginDot() . 'account_verification',
'layout' => 'default',
'emailFormat' => CakeEmail::MESSAGE_TEXT
);
$options = array_merge($defaults, $options);
$Email = $this->_getMailInstance();
$Email->to($userData[$this->modelClass]['email'])
->from($options['from'])
->emailFormat($options['emailFormat'])
->subject($options['subject'])
->template($options['template'], $options['layout'])
->viewVars(array(
'model' => $this->modelClass,
'user' => $userData
))
->send();
}
/**
* Checks if the email is in the system and authenticated, if yes create the token
* save it and send the user an email
*
* @param boolean $admin Admin boolean
* @param array $options Options
* @return void
*/
protected function _sendPasswordReset($admin = null, $options = array()) {
$defaults = array(
'from' => Configure::read('App.defaultEmail'),
'subject' => __d('users', 'Password Reset'),
'template' => $this->_pluginDot() . 'password_reset_request',
'emailFormat' => CakeEmail::MESSAGE_TEXT,
'layout' => 'default'
);
$options = array_merge($defaults, $options);
if (!empty($this->request->data)) {
$user = $this->{$this->modelClass}->passwordReset($this->request->data);
if (!empty($user)) {
$Email = $this->_getMailInstance();
$Email->to($user[$this->modelClass]['email'])
->from($options['from'])
->emailFormat($options['emailFormat'])
->subject($options['subject'])
->template($options['template'], $options['layout'])
->viewVars(array(
'model' => $this->modelClass,
'user' => $this->{$this->modelClass}->data,
'token' => $this->{$this->modelClass}->data[$this->modelClass]['password_token']))
->send();
if ($admin) {
$this->Session->setFlash(sprintf(
__d('users', '%s has been sent an email with instruction to reset their password.'),
$user[$this->modelClass]['email']));
$this->redirect(array('action' => 'index', 'admin' => true));
} else {
$this->Session->setFlash(__d('users', 'You should receive an email with further instructions shortly'));
$this->redirect(array('action' => 'login'));
}
} else {
$this->Session->setFlash(__d('users', 'No user was found with that email.'));
$this->redirect($this->referer('/'));
}
}
$this->render('request_password_change');
}
/**
* Sets the cookie to remember the user
*
* @param array RememberMe (Cookie) component properties as array, like array('domain' => 'yourdomain.com')
* @param string Cookie data keyname for the userdata, its default is "User". This is set to User and NOT using the model alias to make sure it works with different apps with different user models across different (sub)domains.
* @return void
* @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html
*/
protected function _setCookie($options = array(), $cookieKey = 'rememberMe') {
$this->RememberMe->settings['cookieKey'] = $cookieKey;
$this->RememberMe->configureCookie($options);
$this->RememberMe->setCookie();
}
/**
* This method allows the user to change his password if the reset token is correct
*
* @param string $token Token
* @return void
*/
protected function _resetPassword($token) {
$user = $this->{$this->modelClass}->checkPasswordToken($token);
if (empty($user)) {
$this->Session->setFlash(__d('users', 'Invalid password reset token, try again.'));
$this->redirect(array('action' => 'reset_password'));
return;
}
if (!empty($this->request->data) && $this->{$this->modelClass}->resetPassword(Hash::merge($user, $this->request->data))) {
if ($this->RememberMe->cookieIsSet()) {
$this->Session->setFlash(__d('users', 'Password changed.'));
$this->_setCookie();
} else {
$this->Session->setFlash(__d('users', 'Password changed, you can now login with your new password.'));
$this->redirect($this->Auth->loginAction);
}
}
$this->set('token', $token);
}
/**
* Returns a CakeEmail object
*
* @return object CakeEmail instance
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
*/
protected function _getMailInstance() {
return $this->{$this->modelClass}->getMailInstance();
}
/**
* Default isAuthorized method
*
* This is called to see if a user (when logged in) is able to access an action
*
* @param array $user
* @return boolean True if allowed
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-controllerauthorize
*/
public function isAuthorized($user = null) {
return parent::isAuthorized($user);
}
}