forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordManagementTrait.php
More file actions
136 lines (126 loc) · 5.01 KB
/
Copy pathPasswordManagementTrait.php
File metadata and controls
136 lines (126 loc) · 5.01 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
<?php
/**
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Controller\Traits;
use CakeDC\Users\Exception\UserNotActiveException;
use CakeDC\Users\Exception\UserNotFoundException;
use CakeDC\Users\Exception\WrongPasswordException;
use Cake\Core\Configure;
use Cake\Validation\Validator;
use Exception;
/**
* Covers the password management: reset, change
*
*/
trait PasswordManagementTrait
{
use UserValidationTrait;
/**
* Change password
*
* @return void|\Cake\Network\Response
*/
public function changePassword()
{
$user = $this->getUsersTable()->newEntity();
$id = $this->Auth->user('id');
if (!empty($id)) {
$user->id = $this->Auth->user('id');
$validatePassword = true;
//@todo add to the documentation: list of routes used
$redirect = Configure::read('Users.Profile.route');
} else {
$user->id = $this->request->session()->read(Configure::read('Users.Key.Session.resetPasswordUserId'));
$validatePassword = false;
if (!$user->id) {
$this->Flash->error(__d('CakeDC/Users', 'User was not found'));
$this->redirect($this->Auth->config('loginAction'));
return;
}
//@todo add to the documentation: list of routes used
$redirect = $this->Auth->config('loginAction');
}
$this->set('validatePassword', $validatePassword);
if ($this->request->is('post')) {
try {
$validator = $this->getUsersTable()->validationPasswordConfirm(new Validator());
if (!empty($id)) {
$validator = $this->getUsersTable()->validationCurrentPassword($validator);
}
$user = $this->getUsersTable()->patchEntity($user, $this->request->data(), ['validate' => $validator]);
if ($user->errors()) {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
} else {
$user = $this->getUsersTable()->changePassword($user);
if ($user) {
$this->Flash->success(__d('CakeDC/Users', 'Password has been changed successfully'));
return $this->redirect($redirect);
} else {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
}
}
} catch (UserNotFoundException $exception) {
$this->Flash->error(__d('CakeDC/Users', 'User was not found'));
} catch (WrongPasswordException $wpe) {
$this->Flash->error(__d('CakeDC/Users', '{0}', $wpe->getMessage()));
} catch (Exception $exception) {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
/**
* Reset password
*
* @param null $token token data.
* @return void
*/
public function resetPassword($token = null)
{
$this->validate('password', $token);
}
/**
* Reset password
*
* @return void|\Cake\Network\Response
*/
public function requestResetPassword()
{
$this->set('user', $this->getUsersTable()->newEntity());
$this->set('_serialize', ['user']);
if (!$this->request->is('post')) {
return;
}
$reference = $this->request->data('reference');
try {
$resetUser = $this->getUsersTable()->resetToken($reference, [
'expiration' => Configure::read('Users.Token.expiration'),
'checkActive' => false,
'sendEmail' => true,
'ensureActive' => Configure::read('Users.Registration.ensureActive')
]);
if ($resetUser) {
$msg = __d('CakeDC/Users', 'Please check your email to continue with password reset process');
$this->Flash->success($msg);
} else {
$msg = __d('CakeDC/Users', 'The password token could not be generated. Please try again');
$this->Flash->error($msg);
}
return $this->redirect(['action' => 'login']);
} catch (UserNotFoundException $exception) {
$this->Flash->error(__d('CakeDC/Users', 'User {0} was not found', $reference));
} catch (UserNotActiveException $exception) {
$this->Flash->error(__d('CakeDC/Users', 'The user is not active'));
} catch (Exception $exception) {
$this->Flash->error(__d('CakeDC/Users', 'Token could not be reset'));
}
}
}