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