|
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
|
|
|
* Setup password action. |
|
89
|
|
|
* |
|
90
|
|
|
* @param null|int $id User id. |
|
91
|
|
|
* @param null|string $token User activation token. |
|
92
|
|
|
* @return \Cake\Http\Response|null |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setupPassword($id = null, $token = null) |
|
95
|
|
|
{ |
|
96
|
|
|
$user = $this->_getUser($id, $token); |
|
97
|
|
|
|
|
98
|
|
|
if ($user === null) { |
|
99
|
|
|
throw new BadRequestException(__d('community', 'User was not found')); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($this->request->is(['patch', 'post', 'put'])) { |
|
103
|
|
|
$entity = $this->Users->patchEntity($user, $this->request->getData()); |
|
|
|
|
|
|
104
|
|
|
if ($user->status) { |
|
105
|
|
|
$entity->set('token', null); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** @var User $result */ |
|
109
|
|
|
$result = $this->Users->save($user); |
|
|
|
|
|
|
110
|
|
|
if ($result) { |
|
111
|
|
|
$this->Flash->success(__d( |
|
112
|
|
|
'community', |
|
113
|
|
|
'«{0}», You have successfully changed your password.', |
|
114
|
|
|
sprintf('<strong>%s</strong>', $user->get('name')) |
|
115
|
|
|
)); |
|
116
|
|
|
|
|
117
|
|
|
if (!$user->status && $result->token !== null) { |
|
118
|
|
|
return $this->redirect([ |
|
119
|
|
|
'id' => $user->id, |
|
120
|
|
|
'action' => 'activate', |
|
121
|
|
|
'token' => $user->token |
|
122
|
|
|
]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this->redirect(['action' => 'login']); |
|
126
|
|
|
} else { |
|
127
|
|
|
$this->Flash->error(__d('community', 'An error has occurred. Please, try again.')); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this |
|
132
|
|
|
->set('user', $user) |
|
133
|
|
|
->set('page_title', __d('community', 'Setup new password')); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get user by id and activation token. |
|
138
|
|
|
* |
|
139
|
|
|
* @param int $id User id. |
|
140
|
|
|
* @param string $token User activation token. |
|
141
|
|
|
* @return array|User|null |
|
142
|
|
|
*/ |
|
143
|
|
|
private function _getUser($id, $token) |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->Users->find() |
|
146
|
|
|
->where([ |
|
147
|
|
|
'id' => $id, |
|
148
|
|
|
'token' => $token |
|
149
|
|
|
]) |
|
150
|
|
|
->first(); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
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.