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