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