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
180 lines (167 loc) · 6.56 KB
/
Copy pathPasswordManagementTrait.php
File metadata and controls
180 lines (167 loc) · 6.56 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
<?php
/**
* Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Controller\Traits;
use CakeDC\Users\Controller\Component\UsersAuthComponent;
use CakeDC\Users\Exception\UserNotActiveException;
use CakeDC\Users\Exception\UserNotFoundException;
use CakeDC\Users\Exception\WrongPasswordException;
use Cake\Core\Configure;
use Cake\Log\Log;
use Cake\Validation\Validator;
use Exception;
/**
* Covers the password management: reset, change
*
* @property \Cake\Http\ServerRequest $request
*/
trait PasswordManagementTrait
{
use UserValidationTrait;
/**
* Change password
*
* @return mixed
*/
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->getSession()->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->getConfig('loginAction'));
return;
}
//@todo add to the documentation: list of routes used
$redirect = $this->Auth->getConfig('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->getData(),
['validate' => $validator]
);
if ($user->getErrors()) {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
} else {
$result = $this->getUsersTable()->changePassword($user);
if ($result) {
$event = $this->dispatchEvent(UsersAuthComponent::EVENT_AFTER_CHANGE_PASSWORD, ['user' => $result]);
if (!empty($event) && is_array($event->result)) {
return $this->redirect($event->result);
}
$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($wpe->getMessage());
} catch (Exception $exception) {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
$this->log($exception->getMessage());
}
}
$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\Http\Response
*/
public function requestResetPassword()
{
$this->set('user', $this->getUsersTable()->newEntity());
$this->set('_serialize', ['user']);
if (!$this->request->is('post')) {
return;
}
$reference = $this->request->getData('reference');
try {
$resetUser = $this->getUsersTable()->resetToken($reference, [
'expiration' => Configure::read('Users.Token.expiration'),
'checkActive' => false,
'sendEmail' => true,
'ensureActive' => Configure::read('Users.Registration.ensureActive'),
'type' => 'password'
]);
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'));
$this->log($exception->getMessage());
}
}
/**
* resetGoogleAuthenticator
*
* Resets Google Authenticator token by setting secret_verified
* to false.
*
* @param mixed $id of the user record.
* @return mixed.
*/
public function resetGoogleAuthenticator($id = null)
{
if ($this->request->is('post')) {
try {
$query = $this->getUsersTable()->query();
$query->update()
->set(['secret_verified' => false, 'secret' => null])
->where(['id' => $id]);
$query->execute();
$message = __d('CakeDC/Users', 'Google Authenticator token was successfully reset');
$this->Flash->success($message, 'default');
} catch (\Exception $e) {
$message = $e->getMessage();
$this->Flash->error($message, 'default');
}
}
return $this->redirect($this->request->referer());
}
}