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\Admin; |
17
|
|
|
|
18
|
|
|
use Community\Token; |
19
|
|
|
use JBZoo\Utils\Filter; |
20
|
|
|
use Community\Model\Entity\User; |
21
|
|
|
use Community\Model\Table\UsersTable; |
22
|
|
|
use Community\Controller\Component\AuthComponent; |
23
|
|
|
use Cake\ORM\Exception\RolledbackTransactionException; |
24
|
|
|
use Cake\Datasource\Exception\RecordNotFoundException; |
25
|
|
|
use Cake\Datasource\Exception\InvalidPrimaryKeyException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class UsersController |
29
|
|
|
* |
30
|
|
|
* @package Community\Controller\Admin |
31
|
|
|
* |
32
|
|
|
* @property AuthComponent $Auth |
33
|
|
|
* @property UsersTable $Users |
34
|
|
|
*/ |
35
|
|
|
class UsersController extends AppController |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add action. |
40
|
|
|
* |
41
|
|
|
* @return \Cake\Http\Response|null |
42
|
|
|
* |
43
|
|
|
* @throws \Cake\ORM\Exception\RolledbackTransactionException |
44
|
|
|
*/ |
45
|
|
|
public function add() |
46
|
|
|
{ |
47
|
|
|
$user = $this->Users->newEntity(); |
48
|
|
|
if ($this->request->is('post')) { |
49
|
|
|
$user = $this->Users->patchEntity($user, $this->request->getData()); |
50
|
|
|
if (Filter::bool($user->get('notify'))) { |
51
|
|
|
$user->set('token', Token::generate()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$result = $this->Users->save($user); |
55
|
|
|
if ($result) { |
56
|
|
|
$this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf( |
57
|
|
|
'<strong>«%s»</strong>', |
58
|
|
|
$result->get('login') |
59
|
|
|
))); |
60
|
|
|
|
61
|
|
|
return $this->App->redirect(['apply' => ['action' => 'edit', $result->id]]); |
|
|
|
|
62
|
|
|
} else { |
63
|
|
|
$this->Flash->error(__d('community', 'User could not be updated. Please, try again.')); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$groups = $this->Users->Groups->getTreeList(); |
68
|
|
|
|
69
|
|
|
$this |
70
|
|
|
->set(compact('user', 'groups')) |
71
|
|
|
->set('page_title', __d('community', 'Profiles: Add new user')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Change user password action. |
76
|
|
|
* |
77
|
|
|
* @param null|int $id User id. |
78
|
|
|
* @return \Cake\Http\Response|null |
79
|
|
|
*/ |
80
|
|
|
public function changePassword($id = null) |
81
|
|
|
{ |
82
|
|
|
$user = $this->Users->get($id, ['contain' => []]); |
83
|
|
|
$user->set('password', null); |
84
|
|
|
|
85
|
|
View Code Duplication |
if ($this->request->is(['patch', 'post', 'put'])) { |
|
|
|
|
86
|
|
|
$user = $this->Users->patchEntity($user, $this->request->getData()); |
87
|
|
|
if ($result = $this->Users->save($user)) { |
88
|
|
|
$this->Flash->success(__d('community', 'Password has been updated.')); |
89
|
|
|
return $this->App->redirect(['apply' => ['action' => 'edit', $result->id]]); |
|
|
|
|
90
|
|
|
} else { |
91
|
|
|
$this->Flash->error(__d('community', 'Password could not be updated. Please, try again.')); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this |
96
|
|
|
->set(compact('user')) |
97
|
|
|
->set('page_title', __d('community', 'Profiles: {0} - change password', $user->name)); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Edit action. |
102
|
|
|
* |
103
|
|
|
* @param null|int $id User id. |
104
|
|
|
* @return \Cake\Http\Response|null |
105
|
|
|
* |
106
|
|
|
* @throws RecordNotFoundException |
107
|
|
|
* @throws InvalidPrimaryKeyException |
108
|
|
|
* @throws RolledbackTransactionException |
109
|
|
|
*/ |
110
|
|
|
public function edit($id = null) |
111
|
|
|
{ |
112
|
|
|
/** @var User $user */ |
113
|
|
|
$user = $this->Users->get($id); |
114
|
|
|
$groups = $this->Users->Groups->getTreeList(); |
115
|
|
|
|
116
|
|
View Code Duplication |
if ($this->request->is(['patch', 'post', 'put'])) { |
|
|
|
|
117
|
|
|
$user = $this->Users->patchEntity($user, $this->request->getData()); |
118
|
|
|
if ($result = $this->Users->save($user)) { |
119
|
|
|
$this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf( |
120
|
|
|
'<strong>«%s»</strong>', |
121
|
|
|
$result->get('login') |
122
|
|
|
))); |
123
|
|
|
return $this->App->redirect([ |
124
|
|
|
'apply' => ['action' => 'edit', $result->id] |
|
|
|
|
125
|
|
|
]); |
126
|
|
|
} else { |
127
|
|
|
$this->Flash->error(__d('community', 'User could not be updated. Please, try again.')); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this |
132
|
|
|
->set(compact('user', 'groups')) |
133
|
|
|
->set('page_title', __d('community', 'Profiles: Edit user - {0}', $user->name)); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Index action. |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
* |
141
|
|
|
* @throws \RuntimeException |
142
|
|
|
*/ |
143
|
|
|
public function index() |
144
|
|
|
{ |
145
|
|
|
$query = $this->Users |
146
|
|
|
->find('search', $this->Users->filterParams($this->request->getQueryParams())) |
147
|
|
|
->contain('Groups'); |
148
|
|
|
|
149
|
|
|
$this |
150
|
|
|
->set('users', $this->paginate($query)) |
|
|
|
|
151
|
|
|
->set('page_title', __d('community', 'Profiles: User list')); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Initialization hook method. Implement this method to avoid having to overwrite the constructor and call parent. |
156
|
|
|
* |
157
|
|
|
* @return void |
158
|
|
|
* |
159
|
|
|
* @throws \Cake\Core\Exception\Exception |
160
|
|
|
*/ |
161
|
|
|
public function initialize() |
162
|
|
|
{ |
163
|
|
|
parent::initialize(); |
164
|
|
|
$this->Security->setConfig('unlockedFields', ['password', 'password_confirm']); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Login action. |
169
|
|
|
* |
170
|
|
|
* @return \Cake\Http\Response|null |
171
|
|
|
*/ |
172
|
|
|
public function login() |
173
|
|
|
{ |
174
|
|
|
$this->viewBuilder()->setLayout('login'); |
175
|
|
|
if ($this->request->is('post')) { |
176
|
|
|
$user = $this->Auth->identify(); |
177
|
|
|
if ($user !== false) { |
178
|
|
|
$this->Auth->setUser($user); |
|
|
|
|
179
|
|
|
return $this->redirect($this->Auth->redirectUrl()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->Flash->error(__d('community', 'Login or password is incorrect')); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->set('page_title', __d('community', 'Authorize profile')); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Logout action. |
190
|
|
|
* |
191
|
|
|
* @return \Cake\Http\Response|null |
192
|
|
|
*/ |
193
|
|
|
public function logout() |
194
|
|
|
{ |
195
|
|
|
return $this->redirect($this->Auth->logout()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Process action. |
200
|
|
|
* |
201
|
|
|
* @return \Cake\Http\Response|null |
202
|
|
|
*/ |
203
|
|
|
public function process() |
204
|
|
|
{ |
205
|
|
|
list($action, $ids) = $this->Process->getRequestVars($this->name); |
206
|
|
|
return $this->Process->make($this->Users, $action, $ids); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: