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