1
|
|
|
<?php |
2
|
|
|
namespace App\Controller\Admin; |
3
|
|
|
|
4
|
|
|
use App\Controller\AppController; |
5
|
|
|
|
6
|
|
|
class UsersController extends AppController |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Search users form. |
11
|
|
|
* |
12
|
|
|
* @return void |
13
|
|
|
*/ |
14
|
|
|
public function index() |
15
|
|
|
{ |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Search users. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function search() |
24
|
|
|
{ |
25
|
|
|
//Keyword to search. (For pagination) |
26
|
|
View Code Duplication |
if (!empty($this->request->getData('search'))) { |
|
|
|
|
27
|
|
|
$keyword = $this->request->getData('search'); |
28
|
|
|
$this->request->session()->write('Search.Admin.Users.Keyword', $keyword); |
29
|
|
|
} else { |
30
|
|
|
if ($this->request->session()->read('Search.Admin.Users.Keyword')) { |
31
|
|
|
$keyword = $this->request->session()->read('Search.Admin.Users.Keyword'); |
32
|
|
|
} else { |
33
|
|
|
$keyword = ''; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
//Search type. (For pagination) |
38
|
|
View Code Duplication |
if (!empty($this->request->getData('type'))) { |
|
|
|
|
39
|
|
|
$type = $this->request->getData('type'); |
40
|
|
|
$this->request->session()->write('Search.Admin.Users.Type', $type); |
41
|
|
|
} else { |
42
|
|
|
if ($this->request->session()->read('Search.Admin.Users.Type')) { |
43
|
|
|
$type = $this->request->session()->read('Search.Admin.Users.Type'); |
44
|
|
|
} else { |
45
|
|
|
$type = ''; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
switch ($type) { |
50
|
|
View Code Duplication |
case "username": |
|
|
|
|
51
|
|
|
$this->paginate = [ |
52
|
|
|
'limit' => 15, |
53
|
|
|
'conditions' => [ |
54
|
|
|
'Users.username LIKE' => "%$keyword%" |
55
|
|
|
], |
56
|
|
|
'order' => [ |
57
|
|
|
'Users.username' => 'asc' |
58
|
|
|
] |
59
|
|
|
]; |
60
|
|
|
break; |
61
|
|
|
|
62
|
|
View Code Duplication |
case "ip": |
|
|
|
|
63
|
|
|
$this->paginate = [ |
64
|
|
|
'limit' => 15, |
65
|
|
|
'conditions' => [ |
66
|
|
|
'Users.last_login_ip LIKE' => "%$keyword%" |
67
|
|
|
], |
68
|
|
|
'order' => [ |
69
|
|
|
'Users.last_login_ip' => 'asc' |
70
|
|
|
] |
71
|
|
|
]; |
72
|
|
|
break; |
73
|
|
|
|
74
|
|
View Code Duplication |
case "mail": |
|
|
|
|
75
|
|
|
$this->paginate = [ |
76
|
|
|
'limit' => 15, |
77
|
|
|
'conditions' => [ |
78
|
|
|
'Users.email LIKE' => "%$keyword%" |
79
|
|
|
], |
80
|
|
|
'order' => [ |
81
|
|
|
'Users.email' => 'asc' |
82
|
|
|
] |
83
|
|
|
]; |
84
|
|
|
break; |
85
|
|
|
|
86
|
|
|
case "group": |
87
|
|
|
$this->paginate = [ |
88
|
|
|
'limit' => 15, |
89
|
|
|
'conditions' => [ |
90
|
|
|
'Groups.name LIKE' => "%$keyword%" |
91
|
|
|
], |
92
|
|
|
'contain' => ['Groups'] |
93
|
|
|
]; |
94
|
|
|
break; |
95
|
|
|
|
96
|
|
View Code Duplication |
default: |
|
|
|
|
97
|
|
|
$this->paginate = [ |
98
|
|
|
'limit' => 15, |
99
|
|
|
'conditions' => [ |
100
|
|
|
'Users.username LIKE' => "%$keyword%" |
101
|
|
|
], |
102
|
|
|
'order' => [ |
103
|
|
|
'Users.username' => 'asc' |
104
|
|
|
] |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$users = $this->paginate($this->Users->find()); |
|
|
|
|
109
|
|
|
$this->set(compact('users', 'keyword', 'type')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Edit an user. |
114
|
|
|
* |
115
|
|
|
* @return \Cake\Network\Response|void |
116
|
|
|
*/ |
117
|
|
|
public function edit() |
118
|
|
|
{ |
119
|
|
|
$user = $this->Users |
|
|
|
|
120
|
|
|
->find() |
121
|
|
|
->where([ |
122
|
|
|
'Users.id' => $this->request->id |
123
|
|
|
]) |
124
|
|
|
->first(); |
125
|
|
|
|
126
|
|
|
//Check if the user is found. |
127
|
|
|
if (empty($user)) { |
128
|
|
|
$this->Flash->error(__d('admin', 'This user doesn\'t exist or has been deleted.')); |
129
|
|
|
|
130
|
|
|
return $this->redirect(['action' => 'index']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($this->request->is('put')) { |
134
|
|
|
$this->Users->patchEntity( |
|
|
|
|
135
|
|
|
$user, |
136
|
|
|
$this->request->getParsedBody(), |
137
|
|
|
[ |
138
|
|
|
'validate' => 'update', |
139
|
|
|
'accessibleFields' => ['group_id' => true] |
140
|
|
|
] |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
if ($this->Users->save($user)) { |
|
|
|
|
144
|
|
|
$this->Flash->success(__d('admin', 'This user has been updated successfully !')); |
145
|
|
|
|
146
|
|
|
return $this->redirect(['action' => 'index']); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->loadModel('Groups'); |
151
|
|
|
|
152
|
|
|
$groups = $this->Groups->find('list'); |
|
|
|
|
153
|
|
|
|
154
|
|
|
$this->set(compact('user', 'groups')); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Delete an user and all his articles, comments and likes. |
159
|
|
|
* |
160
|
|
|
* @return \Cake\Network\Response |
161
|
|
|
*/ |
162
|
|
|
public function delete() |
163
|
|
|
{ |
164
|
|
|
$user = $this->Users |
|
|
|
|
165
|
|
|
->find() |
166
|
|
|
->where([ |
167
|
|
|
'Users.id' => $this->request->id |
168
|
|
|
]) |
169
|
|
|
->first(); |
170
|
|
|
|
171
|
|
|
//Check if the user is found. |
172
|
|
|
if (empty($user) || $user->is_deleted == true) { |
173
|
|
|
$this->Flash->error(__d('admin', 'This user doesn\'t exist or has been deleted.')); |
174
|
|
|
|
175
|
|
|
return $this->redirect(['action' => 'index']); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$user->is_deleted = true; |
179
|
|
|
|
180
|
|
|
if ($this->Users->save($user)) { |
|
|
|
|
181
|
|
|
$this->Flash->success(__d('admin', 'This user has been deleted successfully !')); |
182
|
|
|
|
183
|
|
|
return $this->redirect(['action' => 'index']); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->Flash->error(__d('admin', 'Unable to delete this user.')); |
187
|
|
|
|
188
|
|
|
return $this->redirect(['action' => 'index']); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Delete an avatar. |
193
|
|
|
* |
194
|
|
|
* @return \Cake\Network\Response |
195
|
|
|
*/ |
196
|
|
|
public function deleteAvatar() |
197
|
|
|
{ |
198
|
|
|
$user = $this->Users |
|
|
|
|
199
|
|
|
->find() |
200
|
|
|
->where([ |
201
|
|
|
'Users.id' => $this->request->id |
202
|
|
|
]) |
203
|
|
|
->first(); |
204
|
|
|
|
205
|
|
|
//Check if the user is found. |
206
|
|
|
if (empty($user)) { |
207
|
|
|
$this->Flash->error(__d('admin', 'This user doesn\'t exist or has been deleted.')); |
208
|
|
|
|
209
|
|
|
return $this->redirect(['action' => 'index']); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$user->avatar = '../img/avatar.png'; |
213
|
|
|
|
214
|
|
|
if ($this->Users->save($user)) { |
|
|
|
|
215
|
|
|
$this->Flash->success(__d('admin', 'His avatar has been deleted successfully !')); |
216
|
|
|
|
217
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->Flash->error(__d('admin', 'Unable to delete his avatar.')); |
221
|
|
|
|
222
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
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.