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
|
|
|
use Cake\Network\Exception\BadRequestException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class UsersController |
25
|
|
|
* |
26
|
|
|
* @package Community\Controller |
27
|
|
|
* @property UsersTable $Users |
28
|
|
|
*/ |
29
|
|
|
class UsersController extends AppController |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Activation user profile action. |
34
|
|
|
* |
35
|
|
|
* @param null|int $id User id. |
36
|
|
|
* @param null|string $token User activation token. |
37
|
|
|
* @return \Cake\Http\Response|null |
38
|
|
|
*/ |
39
|
|
|
public function activate($id = null, $token = null) |
40
|
|
|
{ |
41
|
|
|
$user = $this->_getUser($id, $token); |
42
|
|
|
|
43
|
|
|
if ($user === null) { |
44
|
|
|
throw new BadRequestException(__d('community', 'User was not found')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($user->status) { |
48
|
|
|
$this->Flash->error(__d( |
49
|
|
|
'community', |
50
|
|
|
'«{0}», you have already activated your profile before.', |
51
|
|
|
sprintf('<strong>%s</strong>', $user->name) |
52
|
|
|
)); |
53
|
|
|
} else { |
54
|
|
|
$user |
55
|
|
|
->set('token', null) |
56
|
|
|
->set('status', true); |
57
|
|
|
|
58
|
|
|
$result = $this->Users->save($user); |
|
|
|
|
59
|
|
|
if ($result) { |
60
|
|
|
EventManager::trigger('Controller.Users.successActivate', $this, ['user' => $result]); |
61
|
|
|
$this->Flash->success(__d( |
62
|
|
|
'community', |
63
|
|
|
'«{0}», you profile has been activate successfully.', |
64
|
|
|
sprintf('<strong>%s</strong>', $user->name) |
65
|
|
|
)); |
66
|
|
|
} else { |
67
|
|
|
$this->Flash->error(__d( |
68
|
|
|
'community', |
69
|
|
|
'An error has occurred. Please, try again.', |
70
|
|
|
sprintf('<strong>%s</strong>', $user->name) |
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->redirect(['action' => 'login']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Login action. |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function login() |
84
|
|
|
{ |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Logout action. |
89
|
|
|
* |
90
|
|
|
* @return \Cake\Http\Response|null |
91
|
|
|
*/ |
92
|
|
|
public function logout() |
93
|
|
|
{ |
94
|
|
|
return $this->redirect($this->Auth->logout()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Setup password action. |
100
|
|
|
* |
101
|
|
|
* @param null|int $id User id. |
102
|
|
|
* @param null|string $token User activation token. |
103
|
|
|
* @return \Cake\Http\Response|null |
104
|
|
|
*/ |
105
|
|
|
public function setupPassword($id = null, $token = null) |
106
|
|
|
{ |
107
|
|
|
$user = $this->_getUser($id, $token); |
108
|
|
|
|
109
|
|
|
if ($user === null) { |
110
|
|
|
throw new BadRequestException(__d('community', 'User was not found')); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($this->request->is(['patch', 'post', 'put'])) { |
114
|
|
|
$entity = $this->Users->patchEntity($user, $this->request->getData()); |
|
|
|
|
115
|
|
|
if ($user->status) { |
116
|
|
|
$entity->set('token', null); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** @var User $result */ |
120
|
|
|
$result = $this->Users->save($user); |
|
|
|
|
121
|
|
|
if ($result) { |
122
|
|
|
$this->Flash->success(__d( |
123
|
|
|
'community', |
124
|
|
|
'«{0}», You have successfully changed your password.', |
125
|
|
|
sprintf('<strong>%s</strong>', $user->get('name')) |
126
|
|
|
)); |
127
|
|
|
|
128
|
|
|
if (!$user->status && $result->token !== null) { |
129
|
|
|
return $this->redirect([ |
130
|
|
|
'action' => 'activate', |
131
|
|
|
$user->id, |
132
|
|
|
$user->token |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this->redirect(['action' => 'login']); |
137
|
|
|
} else { |
138
|
|
|
$this->Flash->error(__d('community', 'An error has occurred. Please, try again.')); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this |
143
|
|
|
->set('user', $user) |
144
|
|
|
->set('page_title', __d('community', 'Setup new password')); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get user by id and activation token. |
149
|
|
|
* |
150
|
|
|
* @param int $id User id. |
151
|
|
|
* @param string $token User activation token. |
152
|
|
|
* @return array|User|null |
153
|
|
|
*/ |
154
|
|
|
private function _getUser($id, $token) |
155
|
|
|
{ |
156
|
|
|
return $this->Users->find() |
157
|
|
|
->where([ |
158
|
|
|
'id' => $id, |
159
|
|
|
'token' => $token |
160
|
|
|
]) |
161
|
|
|
->first(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.