|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package API |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\api\controllers; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\controllers\backend\Controller; |
|
13
|
|
|
use gplcart\modules\api\models\User as UserModel; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Handles incoming requests and outputs data related to API users |
|
17
|
|
|
*/ |
|
18
|
|
|
class User extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* User model class instance |
|
23
|
|
|
* @var \gplcart\modules\api\models\User $user_model |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $user_model; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Pager limit |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $data_limit; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The current updating user |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $data_user = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param UserModel $user |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(UserModel $user) |
|
43
|
|
|
{ |
|
44
|
|
|
parent::__construct(); |
|
45
|
|
|
|
|
46
|
|
|
$this->user_model = $user; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Route callback |
|
51
|
|
|
* Displays the API users overview page |
|
52
|
|
|
*/ |
|
53
|
|
|
public function listUser() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->actionListUser(); |
|
56
|
|
|
$this->setTitleListUser(); |
|
57
|
|
|
$this->setBreadcrumbListUser(); |
|
58
|
|
|
$this->setFilterListUser(); |
|
59
|
|
|
$this->setPagerListUser(); |
|
60
|
|
|
|
|
61
|
|
|
$this->setData('users', $this->getListUser()); |
|
62
|
|
|
$this->outputListUser(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Applies an action to the selected users |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function actionListUser() |
|
69
|
|
|
{ |
|
70
|
|
|
list($selected, $action) = $this->getPostedAction(); |
|
71
|
|
|
|
|
72
|
|
|
$deleted = 0; |
|
73
|
|
|
|
|
74
|
|
|
foreach ($selected as $id) { |
|
75
|
|
|
if ($action === 'delete' && $this->access('module_api_user_delete')) { |
|
76
|
|
|
$deleted += (int) $this->user_model->delete($id); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($deleted > 0) { |
|
81
|
|
|
$message = $this->text('Deleted %num item(s)', array('%num' => $deleted)); |
|
82
|
|
|
$this->setMessage($message, 'success'); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Sets filter parameters |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function setFilterListUser() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->setFilter(array('created', 'modified', 'name', 'api_user_id', 'user_id', 'status')); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Sets pager |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function setPagerListUser() |
|
99
|
|
|
{ |
|
100
|
|
|
$options = $this->query_filter; |
|
101
|
|
|
$options['count'] = true; |
|
102
|
|
|
|
|
103
|
|
|
$pager = array( |
|
104
|
|
|
'query' => $this->query_filter, |
|
105
|
|
|
'total' => (int) $this->user_model->getList($options) |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
return $this->data_limit = $this->setPager($pager); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns an array of API users |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getListUser() |
|
116
|
|
|
{ |
|
117
|
|
|
$options = $this->query_filter; |
|
118
|
|
|
$options['limit'] = $this->data_limit; |
|
119
|
|
|
|
|
120
|
|
|
return $this->user_model->getList($options); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Sets title on the credential overview page |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function setTitleListUser() |
|
127
|
|
|
{ |
|
128
|
|
|
$this->setTitle($this->text('API users')); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Sets breadcrumbs on the user overview page |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function setBreadcrumbListUser() |
|
135
|
|
|
{ |
|
136
|
|
|
$breadcrumb = array( |
|
137
|
|
|
'url' => $this->url('admin'), |
|
138
|
|
|
'text' => $this->text('Dashboard') |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
$this->setBreadcrumb($breadcrumb); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Render and output the user overview page |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function outputListUser() |
|
148
|
|
|
{ |
|
149
|
|
|
$this->output('api|user/list'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Page callback |
|
154
|
|
|
* Displays the edit user page |
|
155
|
|
|
* @param null|int $api_user_id |
|
156
|
|
|
*/ |
|
157
|
|
|
public function editUser($api_user_id = null) |
|
158
|
|
|
{ |
|
159
|
|
|
$this->setUser($api_user_id); |
|
160
|
|
|
$this->setTitleEditUser(); |
|
161
|
|
|
$this->setBreadcrumbEditUser(); |
|
162
|
|
|
|
|
163
|
|
|
$this->setData('user', $this->data_user); |
|
164
|
|
|
|
|
165
|
|
|
$this->submitEditUser(); |
|
166
|
|
|
$this->setDataEditUser(); |
|
167
|
|
|
$this->outputEditUser(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Prepare template variables |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function setDataEditUser() |
|
174
|
|
|
{ |
|
175
|
|
|
$data = $this->getData('user.data.ip'); |
|
176
|
|
|
|
|
177
|
|
|
if (is_array($data)) { |
|
178
|
|
|
$this->setData('user.data.ip', implode(PHP_EOL, $data)); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Sets an API user |
|
184
|
|
|
* @param $api_user_id |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function setUser($api_user_id) |
|
187
|
|
|
{ |
|
188
|
|
|
if (is_numeric($api_user_id)) { |
|
189
|
|
|
$this->data_user = $this->user_model->get($api_user_id); |
|
190
|
|
|
if (empty($this->data_user)) { |
|
191
|
|
|
$this->outputHttpStatus(403); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Sets titles on the edit user page |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function setTitleEditUser() |
|
200
|
|
|
{ |
|
201
|
|
|
if (isset($this->data_user['api_user_id'])) { |
|
202
|
|
|
$text = $this->text('Edit %name', array('%name' => $this->data_user['api_user_id'])); |
|
203
|
|
|
} else { |
|
204
|
|
|
$text = $this->text('Add user'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$this->setTitle($text); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Sets breadcrumbs on the user edit page |
|
212
|
|
|
*/ |
|
213
|
|
|
protected function setBreadcrumbEditUser() |
|
214
|
|
|
{ |
|
215
|
|
|
$breadcrumbs = array(); |
|
216
|
|
|
|
|
217
|
|
|
$breadcrumbs[] = array( |
|
218
|
|
|
'url' => $this->url('admin'), |
|
219
|
|
|
'text' => $this->text('Dashboard') |
|
220
|
|
|
); |
|
221
|
|
|
|
|
222
|
|
|
$breadcrumbs[] = array( |
|
223
|
|
|
'url' => $this->url('admin/user/api'), |
|
224
|
|
|
'text' => $this->text('Users') |
|
225
|
|
|
); |
|
226
|
|
|
|
|
227
|
|
|
$this->setBreadcrumbs($breadcrumbs); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Handles a submitted user |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function submitEditUser() |
|
234
|
|
|
{ |
|
235
|
|
|
if ($this->isPosted('delete') && isset($this->data_user['api_user_id'])) { |
|
236
|
|
|
$this->deleteUser(); |
|
237
|
|
|
} else if ($this->isPosted('save') && $this->validateEditUser()) { |
|
238
|
|
|
if (isset($this->data_user['api_user_id'])) { |
|
239
|
|
|
$this->updateUser(); |
|
240
|
|
|
} else { |
|
241
|
|
|
$this->addUser(); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Validates a submitted user data |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function validateEditUser() |
|
250
|
|
|
{ |
|
251
|
|
|
$this->setSubmitted('user'); |
|
252
|
|
|
$this->setSubmittedBool('status'); |
|
253
|
|
|
$this->setSubmittedArray('data.ip'); |
|
254
|
|
|
|
|
255
|
|
|
if ($this->getSubmitted('secret', '') === '') { |
|
256
|
|
|
$this->setSubmitted('secret', gplcart_string_random(16)); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
$this->validateElement('secret', 'length', array(8, 255)); |
|
260
|
|
|
$this->validateElement('user_id', 'regexp', '/^[\d]{1,10}$/'); |
|
261
|
|
|
|
|
262
|
|
|
if ($this->isError()) { |
|
263
|
|
|
return !$this->hasErrors(); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
$user_id = $this->getSubmitted('user_id'); |
|
267
|
|
|
|
|
268
|
|
|
if (!$this->user->get($user_id)) { |
|
269
|
|
|
$this->setError('user_id', $this->text('Invalid user')); |
|
270
|
|
|
return !$this->hasErrors(); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
$existing = $this->user_model->getList(array('user_id' => $user_id)); |
|
274
|
|
|
|
|
275
|
|
|
if (isset($this->data_user['api_user_id'])) { |
|
276
|
|
|
unset($existing[$this->data_user['api_user_id']]); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
if (!empty($existing)) { |
|
280
|
|
|
$this->setError('user_id', $this->text('API user already created for the system user ID')); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return !$this->hasErrors(); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Updates a submitted user |
|
288
|
|
|
*/ |
|
289
|
|
|
protected function updateUser() |
|
290
|
|
|
{ |
|
291
|
|
|
$this->controlAccess('module_api_user_edit'); |
|
292
|
|
|
|
|
293
|
|
|
if ($this->user_model->update($this->data_user['api_user_id'], $this->getSubmitted())) { |
|
294
|
|
|
$this->redirect('admin/user/api', $this->text('User has been updated'), 'success'); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
$this->redirect('', $this->text('User has not been updated'), 'warning'); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Adds a new user |
|
302
|
|
|
*/ |
|
303
|
|
|
protected function addUser() |
|
304
|
|
|
{ |
|
305
|
|
|
$this->controlAccess('module_api_user_add'); |
|
306
|
|
|
|
|
307
|
|
|
if ($this->user_model->add($this->getSubmitted())) { |
|
308
|
|
|
$this->redirect('admin/user/api', $this->text('User has been added'), 'success'); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
$this->redirect('', $this->text('User has not been added'), 'warning'); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Delete a submitted user |
|
316
|
|
|
*/ |
|
317
|
|
|
protected function deleteUser() |
|
318
|
|
|
{ |
|
319
|
|
|
$this->controlAccess('module_api_user_delete'); |
|
320
|
|
|
|
|
321
|
|
|
if ($this->user_model->delete($this->data_user['api_user_id'])) { |
|
322
|
|
|
$this->redirect('admin/user/api', $this->text('User has been deleted'), 'success'); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
$this->redirect('', $this->text('User has not been deleted'), 'warning'); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Render and output the user edit page |
|
330
|
|
|
*/ |
|
331
|
|
|
protected function outputEditUser() |
|
332
|
|
|
{ |
|
333
|
|
|
$this->output('api|user/edit'); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
} |
|
337
|
|
|
|