1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package CLI |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh <[email protected]> |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\cli\controllers\commands; |
11
|
|
|
|
12
|
|
|
use gplcart\core\models\User as UserModel; |
13
|
|
|
use gplcart\modules\cli\controllers\Command; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles commands related to users |
17
|
|
|
*/ |
18
|
|
|
class User extends Command |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* User model instance |
23
|
|
|
* @var \gplcart\core\models\User $user |
24
|
|
|
*/ |
25
|
|
|
protected $user; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param UserModel $user |
29
|
|
|
*/ |
30
|
|
|
public function __construct(UserModel $user) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
|
34
|
|
|
$this->user = $user; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Callback for "user-on" command |
39
|
|
|
*/ |
40
|
|
|
public function cmdOnUser() |
41
|
|
|
{ |
42
|
|
|
$this->setStatusUser(true); |
43
|
|
|
$this->output(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Callback for "user-off" command |
48
|
|
|
*/ |
49
|
|
|
public function cmdOffUser() |
50
|
|
|
{ |
51
|
|
|
$this->setStatusUser(false); |
52
|
|
|
$this->output(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Callback for "user-get" command |
57
|
|
|
*/ |
58
|
|
|
public function cmdGetUser() |
59
|
|
|
{ |
60
|
|
|
$result = $this->getListUser(); |
61
|
|
|
$this->outputFormat($result); |
62
|
|
|
$this->outputFormatTableUser($result); |
63
|
|
|
$this->output(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Callback for "user-delete" command |
68
|
|
|
*/ |
69
|
|
|
public function cmdDeleteUser() |
70
|
|
|
{ |
71
|
|
|
$id = $this->getParam(0); |
72
|
|
|
|
73
|
|
|
if (!isset($id) || !is_numeric($id)) { |
74
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$options = null; |
78
|
|
|
|
79
|
|
|
if ($this->getParam('role')) { |
80
|
|
|
$options = array('role_id' => $id); |
81
|
|
|
} else if ($this->getParam('store')) { |
82
|
|
|
$options = array('store_id' => $id); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (isset($options)) { |
86
|
|
|
|
87
|
|
|
$deleted = $count = 0; |
88
|
|
|
foreach ($this->user->getList($options) as $item) { |
89
|
|
|
$count++; |
90
|
|
|
$deleted += (int) $this->user->delete($item['user_id']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$result = $count && $count == $deleted; |
94
|
|
|
|
95
|
|
|
} else { |
96
|
|
|
$result = $this->user->delete($id); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (empty($result)) { |
100
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->output(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Callback for "user-add" command |
108
|
|
|
*/ |
109
|
|
|
public function cmdAddUser() |
110
|
|
|
{ |
111
|
|
|
if ($this->getParam()) { |
112
|
|
|
$this->submitAddUser(); |
113
|
|
|
} else { |
114
|
|
|
$this->wizardAddUser(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->output(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Callback for "user-update" command |
122
|
|
|
*/ |
123
|
|
|
public function cmdUpdateUser() |
124
|
|
|
{ |
125
|
|
|
$params = $this->getParam(); |
126
|
|
|
|
127
|
|
|
if (empty($params[0]) || count($params) < 2) { |
128
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (!is_numeric($params[0])) { |
132
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->setSubmitted(null, $params); |
136
|
|
|
$this->setSubmitted('update', $params[0]); |
137
|
|
|
$this->setSubmittedJson('data'); |
138
|
|
|
|
139
|
|
|
$this->validateComponent('user', array('admin' => true)); |
140
|
|
|
|
141
|
|
|
$this->updateUser($params[0]); |
142
|
|
|
$this->output(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns an array of users |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
protected function getListUser() |
150
|
|
|
{ |
151
|
|
|
$id = $this->getParam(0); |
152
|
|
|
|
153
|
|
|
if (!isset($id)) { |
154
|
|
|
return $this->user->getList(array('limit' => $this->getLimit())); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!is_numeric($id)) { |
158
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($this->getParam('store')) { |
162
|
|
|
return $this->user->getList(array('store_id' => $id, 'limit' => $this->getLimit())); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ($this->getParam('role')) { |
166
|
|
|
return $this->user->getList(array('role_id' => $id, 'limit' => $this->getLimit())); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$result = $this->user->get($id); |
170
|
|
|
|
171
|
|
|
if (empty($result)) { |
172
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return array($result); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Output table format |
180
|
|
|
* @param array $items |
181
|
|
|
*/ |
182
|
|
|
protected function outputFormatTableUser(array $items) |
183
|
|
|
{ |
184
|
|
|
$header = array( |
185
|
|
|
$this->text('ID'), |
186
|
|
|
$this->text('Name'), |
187
|
|
|
$this->text('E-mail'), |
188
|
|
|
$this->text('Role'), |
189
|
|
|
$this->text('Store'), |
190
|
|
|
$this->text('Enabled'), |
191
|
|
|
$this->text('Created'), |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$rows = array(); |
195
|
|
|
|
196
|
|
|
foreach ($items as $item) { |
197
|
|
|
$rows[] = array( |
198
|
|
|
$item['user_id'], |
199
|
|
|
$item['name'], |
200
|
|
|
$item['email'], |
201
|
|
|
$item['role_id'], |
202
|
|
|
$item['store_id'], |
203
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes'), |
204
|
|
|
$this->date($item['created']) |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$this->outputFormatTable($rows, $header); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Updates a user |
213
|
|
|
* @param string $user_id |
214
|
|
|
*/ |
215
|
|
|
protected function updateUser($user_id) |
216
|
|
|
{ |
217
|
|
|
if (!$this->isError() && !$this->user->update($user_id, $this->getSubmitted())) { |
218
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Add a new user at once |
224
|
|
|
*/ |
225
|
|
|
protected function submitAddUser() |
226
|
|
|
{ |
227
|
|
|
$this->setSubmitted(null, $this->getParam()); |
228
|
|
|
$this->setSubmittedJson('data'); |
229
|
|
|
|
230
|
|
|
$this->validateComponent('user'); |
231
|
|
|
$this->addUser(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Add a new user |
236
|
|
|
*/ |
237
|
|
|
protected function addUser() |
238
|
|
|
{ |
239
|
|
|
if (!$this->isError()) { |
240
|
|
|
$id = $this->user->add($this->getSubmitted()); |
241
|
|
|
if (empty($id)) { |
242
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
243
|
|
|
} |
244
|
|
|
$this->line($id); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Add a new user step by step |
250
|
|
|
*/ |
251
|
|
|
protected function wizardAddUser() |
252
|
|
|
{ |
253
|
|
|
// Required |
254
|
|
|
$this->validatePrompt('email', $this->text('E-mail'), 'user'); |
255
|
|
|
$this->validatePrompt('password', $this->text('Password'), 'user'); |
256
|
|
|
$this->validatePrompt('name', $this->text('Name'), 'user'); |
257
|
|
|
|
258
|
|
|
// Optional |
259
|
|
|
$this->validatePrompt('role_id', $this->text('Role ID'), 'user', 0); |
260
|
|
|
$this->validatePrompt('store_id', $this->text('Store ID'), 'user', $this->config->get('store', 1)); |
261
|
|
|
$this->validatePrompt('timezone', $this->text('Timezone'), 'user', $this->config->get('timezone', 'Europe/London')); |
262
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'user', 0); |
263
|
|
|
$this->validatePrompt('data', $this->text('Data'), 'user'); |
264
|
|
|
$this->setSubmittedJson('data'); |
265
|
|
|
|
266
|
|
|
$this->validateComponent('user'); |
267
|
|
|
$this->addUser(); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Sets a user status |
272
|
|
|
* @param $status |
273
|
|
|
*/ |
274
|
|
|
public function setStatusUser($status) |
275
|
|
|
{ |
276
|
|
|
$options = $id = $result = null; |
277
|
|
|
|
278
|
|
|
if ($this->getParam('all')) { |
279
|
|
|
$options = array(); |
280
|
|
|
} else { |
281
|
|
|
|
282
|
|
|
$id = $this->getParam(0); |
283
|
|
|
|
284
|
|
|
if (!is_numeric($id)) { // Allow 0 for roleless users |
285
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if ($this->getParam('role')) { |
289
|
|
|
$options = array('role_id' => $id); |
290
|
|
|
} else if ($this->getParam('store')) { |
291
|
|
|
$options = array('store_id' => $id); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
if (isset($options)) { |
296
|
|
|
|
297
|
|
|
$updated = $count = 0; |
298
|
|
|
foreach ($this->user->getList($options) as $item) { |
299
|
|
|
$count++; |
300
|
|
|
$updated += (int) $this->user->update($item['user_id'], array('status' => (bool) $status)); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$result = $count && $count == $updated; |
304
|
|
|
|
305
|
|
|
} else if (isset($id)) { |
306
|
|
|
$result = $this->user->update($id, array('status' => (bool) $status)); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
if (empty($result)) { |
310
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
} |
315
|
|
|
|