1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakeCMS Community |
4
|
|
|
* |
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @package Community |
10
|
|
|
* @license MIT |
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
12
|
|
|
* @link https://github.com/CakeCMS/Community". |
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Community\Controller; |
17
|
|
|
|
18
|
|
|
use Core\Event\EventManager; |
19
|
|
|
use Community\Model\Entity\User; |
20
|
|
|
use Community\Model\Table\UsersTable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class UsersController |
24
|
|
|
* |
25
|
|
|
* @package Community\Controller |
26
|
|
|
* @property UsersTable $Users |
27
|
|
|
*/ |
28
|
|
|
class UsersController extends AppController |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Default profile page action. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
* |
36
|
|
|
* @throws \Aura\Intl\Exception |
37
|
|
|
*/ |
38
|
|
|
public function profile() |
39
|
|
|
{ |
40
|
|
|
$userId = $this->Auth->user('id'); |
41
|
|
|
if (!$userId) { |
42
|
|
|
$this->Flash->error(__d('community', 'You are not logged in')); |
43
|
|
|
$this->redirect($this->Auth->getConfig('loginAction')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this |
47
|
|
|
->set('user', $this->Users->get($userId, ['contain' => 'Groups'])) |
48
|
|
|
->set('page_title', __d('community', 'Edit profile')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Activation user profile action. |
53
|
|
|
* |
54
|
|
|
* @param null|int $id User id. |
55
|
|
|
* @param null|string $token User activation token. |
56
|
|
|
* @return \Cake\Http\Response|null |
57
|
|
|
* |
58
|
|
|
* @throws \Aura\Intl\Exception |
59
|
|
|
*/ |
60
|
|
|
public function activate($id = null, $token = null) |
61
|
|
|
{ |
62
|
|
|
$user = $this->_getUser($id, $token); |
63
|
|
|
|
64
|
|
View Code Duplication |
if ($user === null) { |
|
|
|
|
65
|
|
|
$this->Flash->error(__d('community', 'User was not found')); |
66
|
|
|
return $this->redirect(['action' => 'login']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($user->status) { |
70
|
|
|
$this->Flash->error(__d( |
71
|
|
|
'community', |
72
|
|
|
'«{0}», you have already activated your profile before.', |
73
|
|
|
sprintf('<strong>%s</strong>', $user->name) |
74
|
|
|
)); |
75
|
|
|
} else { |
76
|
|
|
$user |
77
|
|
|
->set('token', null) |
78
|
|
|
->set('status', true); |
79
|
|
|
|
80
|
|
|
$result = $this->Users->save($user); |
|
|
|
|
81
|
|
|
if ($result) { |
82
|
|
|
EventManager::trigger('Controller.Users.successActivate', $this, ['user' => $result]); |
83
|
|
|
$this->Flash->success(__d( |
84
|
|
|
'community', |
85
|
|
|
'«{0}», you profile has been activate successfully.', |
86
|
|
|
sprintf('<strong>%s</strong>', $user->name) |
87
|
|
|
)); |
88
|
|
|
} else { |
89
|
|
|
$this->Flash->error(__d( |
90
|
|
|
'community', |
91
|
|
|
'An error has occurred. Please, try again.', |
92
|
|
|
sprintf('<strong>%s</strong>', $user->name) |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->redirect(['action' => 'login']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Login action. |
102
|
|
|
* |
103
|
|
|
* @return \Cake\Http\Response|null |
104
|
|
|
* |
105
|
|
|
* @throws \Aura\Intl\Exception |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function login() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
if ($this->request->is('post')) { |
110
|
|
|
$user = $this->Auth->identify(); |
111
|
|
|
if ($user !== false) { |
112
|
|
|
$this->Auth->setUser($user); |
|
|
|
|
113
|
|
|
return $this->redirect($this->Auth->redirectUrl()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->Flash->error(__d('community', 'Login or password is incorrect')); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->set('page_title', __d('community', 'Authorize profile')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Logout action. |
124
|
|
|
* |
125
|
|
|
* @return \Cake\Http\Response|null |
126
|
|
|
*/ |
127
|
|
|
public function logout() |
128
|
|
|
{ |
129
|
|
|
return $this->redirect($this->Auth->logout()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Setup password action. |
134
|
|
|
* |
135
|
|
|
* @param null|int $id User id. |
136
|
|
|
* @param null|string $token User activation token. |
137
|
|
|
* @return \Cake\Http\Response|null |
138
|
|
|
* |
139
|
|
|
* @throws \Aura\Intl\Exception |
140
|
|
|
*/ |
141
|
|
|
public function setupPassword($id = null, $token = null) |
142
|
|
|
{ |
143
|
|
|
$user = $this->_getUser($id, $token); |
144
|
|
|
|
145
|
|
View Code Duplication |
if ($user === null) { |
|
|
|
|
146
|
|
|
$this->Flash->error(__d('community', 'User was not found')); |
147
|
|
|
return $this->redirect(['action' => 'login']); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ($this->request->is(['patch', 'post', 'put'])) { |
151
|
|
|
$entity = $this->Users->patchEntity($user, $this->request->getData()); |
|
|
|
|
152
|
|
|
if ($user->status) { |
153
|
|
|
$entity->set('token', null); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** @var User $result */ |
157
|
|
|
$result = $this->Users->save($user); |
|
|
|
|
158
|
|
|
if ($result) { |
159
|
|
|
$this->Flash->success(__d( |
160
|
|
|
'community', |
161
|
|
|
'«{0}», You have successfully changed your password.', |
162
|
|
|
sprintf('<strong>%s</strong>', $user->get('name')) |
163
|
|
|
)); |
164
|
|
|
|
165
|
|
|
if (!$user->status && $result->token !== null) { |
166
|
|
|
return $this->redirect([ |
167
|
|
|
'action' => 'activate', |
168
|
|
|
$user->id, |
169
|
|
|
$user->token |
170
|
|
|
]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->redirect(['action' => 'login']); |
174
|
|
|
} else { |
175
|
|
|
$this->Flash->error(__d('community', 'An error has occurred. Please, try again.')); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this |
180
|
|
|
->set('user', $user) |
181
|
|
|
->set('page_title', __d('community', 'Setup new password')); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get user by id and activation token. |
186
|
|
|
* |
187
|
|
|
* @param int $id User id. |
188
|
|
|
* @param string $token User activation token. |
189
|
|
|
* @return array|User|null |
190
|
|
|
*/ |
191
|
|
|
private function _getUser($id, $token) |
192
|
|
|
{ |
193
|
|
|
return $this->Users->find() |
194
|
|
|
->where([ |
195
|
|
|
'id' => $id, |
196
|
|
|
'token' => $token |
197
|
|
|
]) |
198
|
|
|
->first(); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.