1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Circles - Bring cloud-users closer together. |
8
|
|
|
* |
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
10
|
|
|
* later. See the COPYING file. |
11
|
|
|
* |
12
|
|
|
* @author Maxence Lange <[email protected]> |
13
|
|
|
* @copyright 2017 |
14
|
|
|
* @license GNU AGPL version 3 or any later version |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
19
|
|
|
* License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Affero General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
namespace OCA\Circles\Command; |
33
|
|
|
|
34
|
|
|
use ArtificialOwl\MySmallPhpTools\Exceptions\InvalidItemException; |
35
|
|
|
use ArtificialOwl\MySmallPhpTools\Exceptions\ItemNotFoundException; |
36
|
|
|
use ArtificialOwl\MySmallPhpTools\Exceptions\RequestNetworkException; |
37
|
|
|
use ArtificialOwl\MySmallPhpTools\Exceptions\SignatoryException; |
38
|
|
|
use ArtificialOwl\MySmallPhpTools\Exceptions\UnknownTypeException; |
39
|
|
|
use ArtificialOwl\MySmallPhpTools\Model\Nextcloud\nc22\NC22TreeNode; |
40
|
|
|
use ArtificialOwl\MySmallPhpTools\Model\SimpleDataStore; |
41
|
|
|
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22ConsoleTree; |
42
|
|
|
use OC\Core\Command\Base; |
43
|
|
|
use OCA\Circles\Db\MemberRequest; |
44
|
|
|
use OCA\Circles\Exceptions\CircleNotFoundException; |
45
|
|
|
use OCA\Circles\Exceptions\FederatedItemException; |
46
|
|
|
use OCA\Circles\Exceptions\FederatedUserException; |
47
|
|
|
use OCA\Circles\Exceptions\FederatedUserNotFoundException; |
48
|
|
|
use OCA\Circles\Exceptions\InitiatorNotFoundException; |
49
|
|
|
use OCA\Circles\Exceptions\InvalidIdException; |
50
|
|
|
use OCA\Circles\Exceptions\MemberNotFoundException; |
51
|
|
|
use OCA\Circles\Exceptions\OwnerNotFoundException; |
52
|
|
|
use OCA\Circles\Exceptions\RemoteInstanceException; |
53
|
|
|
use OCA\Circles\Exceptions\RemoteNotFoundException; |
54
|
|
|
use OCA\Circles\Exceptions\RemoteResourceNotFoundException; |
55
|
|
|
use OCA\Circles\Exceptions\RequestBuilderException; |
56
|
|
|
use OCA\Circles\Exceptions\SingleCircleNotFoundException; |
57
|
|
|
use OCA\Circles\Exceptions\UnknownRemoteException; |
58
|
|
|
use OCA\Circles\Exceptions\UserTypeNotFoundException; |
59
|
|
|
use OCA\Circles\Model\Circle; |
60
|
|
|
use OCA\Circles\Model\Member; |
61
|
|
|
use OCA\Circles\Service\CircleService; |
62
|
|
|
use OCA\Circles\Service\ConfigService; |
63
|
|
|
use OCA\Circles\Service\FederatedUserService; |
64
|
|
|
use OCA\Circles\Service\MemberService; |
65
|
|
|
use OCA\Circles\Service\RemoteService; |
66
|
|
|
use Symfony\Component\Console\Helper\Table; |
67
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
68
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
69
|
|
|
use Symfony\Component\Console\Input\InputOption; |
70
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
71
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Class MembersList |
76
|
|
|
* |
77
|
|
|
* @package OCA\Circles\Command |
78
|
|
|
*/ |
79
|
|
|
class MembersList extends Base { |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
use TNC22ConsoleTree; |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** @var MemberRequest */ |
86
|
|
|
private $memberRequest; |
87
|
|
|
|
88
|
|
|
/** @var FederatedUserService */ |
89
|
|
|
private $federatedUserService; |
90
|
|
|
|
91
|
|
|
/** @var RemoteService */ |
92
|
|
|
private $remoteService; |
93
|
|
|
|
94
|
|
|
/** @var CircleService */ |
95
|
|
|
private $circleService; |
96
|
|
|
|
97
|
|
|
/** @var MemberService */ |
98
|
|
|
private $memberService; |
99
|
|
|
|
100
|
|
|
/** @var ConfigService */ |
101
|
|
|
private $configService; |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** @var InputInterface */ |
105
|
|
|
private $input; |
106
|
|
|
|
107
|
|
|
/** @var string */ |
108
|
|
|
private $treeType = ''; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* MembersList constructor. |
112
|
|
|
* |
113
|
|
|
* @param MemberRequest $memberRequest |
114
|
|
|
* @param FederatedUserService $federatedUserService |
115
|
|
|
* @param RemoteService $remoteService |
116
|
|
|
* @param CircleService $circleService |
117
|
|
|
* @param MemberService $memberService |
118
|
|
|
* @param ConfigService $configService |
119
|
|
|
*/ |
120
|
|
|
public function __construct( |
121
|
|
|
MemberRequest $memberRequest, FederatedUserService $federatedUserService, |
122
|
|
|
RemoteService $remoteService, CircleService $circleService, MemberService $memberService, |
123
|
|
|
ConfigService $configService |
124
|
|
|
) { |
125
|
|
|
parent::__construct(); |
126
|
|
|
|
127
|
|
|
$this->memberRequest = $memberRequest; |
128
|
|
|
$this->federatedUserService = $federatedUserService; |
129
|
|
|
$this->remoteService = $remoteService; |
130
|
|
|
$this->circleService = $circleService; |
131
|
|
|
$this->memberService = $memberService; |
132
|
|
|
$this->configService = $configService; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
protected function configure() { |
137
|
|
|
parent::configure(); |
138
|
|
|
$this->setName('circles:members:list') |
139
|
|
|
->setDescription('listing Members from a Circle') |
140
|
|
|
->addArgument('circle_id', InputArgument::REQUIRED, 'ID of the circle') |
141
|
|
|
->addOption('instance', '', InputOption::VALUE_REQUIRED, 'Instance of the circle', '') |
142
|
|
|
->addOption('inherited', '', InputOption::VALUE_NONE, 'Display all inherited members') |
143
|
|
|
->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '') |
144
|
|
|
->addOption('initiator-type', '', InputOption::VALUE_REQUIRED, 'set initiator type', '0') |
145
|
|
|
->addOption('display-name', '', InputOption::VALUE_NONE, 'display the displayName') |
146
|
|
|
->addOption('tree', '', InputOption::VALUE_OPTIONAL, 'display members as a tree', false); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param InputInterface $input |
152
|
|
|
* @param OutputInterface $output |
153
|
|
|
* |
154
|
|
|
* @return int |
155
|
|
|
* @throws CircleNotFoundException |
156
|
|
|
* @throws FederatedUserException |
157
|
|
|
* @throws FederatedUserNotFoundException |
158
|
|
|
* @throws InitiatorNotFoundException |
159
|
|
|
* @throws InvalidIdException |
160
|
|
|
* @throws InvalidItemException |
161
|
|
|
* @throws MemberNotFoundException |
162
|
|
|
* @throws OwnerNotFoundException |
163
|
|
|
* @throws RemoteInstanceException |
164
|
|
|
* @throws RemoteNotFoundException |
165
|
|
|
* @throws RemoteResourceNotFoundException |
166
|
|
|
* @throws RequestNetworkException |
167
|
|
|
* @throws SignatoryException |
168
|
|
|
* @throws UnknownRemoteException |
169
|
|
|
* @throws UserTypeNotFoundException |
170
|
|
|
* @throws FederatedItemException |
171
|
|
|
* @throws SingleCircleNotFoundException |
172
|
|
|
* @throws RequestBuilderException |
173
|
|
|
*/ |
174
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int { |
175
|
|
|
$this->input = $input; |
176
|
|
|
$circleId = $input->getArgument('circle_id'); |
177
|
|
|
$instance = $input->getOption('instance'); |
178
|
|
|
$initiator = $input->getOption('initiator'); |
179
|
|
|
$initiatorType = Member::parseTypeString($input->getOption('initiator-type')); |
180
|
|
|
$inherited = $input->getOption('inherited'); |
181
|
|
|
|
182
|
|
|
$tree = null; |
183
|
|
|
if ($input->getOption('tree') !== false) { |
184
|
|
|
$this->treeType = (is_null($input->getOption('tree'))) ? 'all' : $input->getOption('tree'); |
185
|
|
|
|
186
|
|
|
$this->federatedUserService->commandLineInitiator($initiator, $initiatorType, $circleId, true); |
187
|
|
|
$circle = $this->circleService->getCircle($circleId); |
188
|
|
|
|
189
|
|
|
$output->writeln('<info>Name</info>: ' . $circle->getName()); |
190
|
|
|
$owner = $circle->getOwner(); |
191
|
|
|
$output->writeln('<info>Owner</info>: ' . $owner->getUserId() . '@' . $owner->getInstance()); |
192
|
|
|
$type = implode(", ", Circle::getCircleFlags($circle, Circle::FLAGS_LONG)); |
193
|
|
|
$output->writeln('<info>Config</info>: ' . $type); |
194
|
|
|
$output->writeln(' '); |
195
|
|
|
|
196
|
|
|
$tree = new NC22TreeNode(null, new SimpleDataStore(['circle' => $circle])); |
197
|
|
|
$inherited = false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if ($inherited) { |
201
|
|
|
$this->federatedUserService->commandLineInitiator($initiator, $initiatorType, $circleId, true); |
202
|
|
|
$circle = $this->circleService->getCircle($circleId); |
203
|
|
|
$members = $circle->getInheritedMembers(true); |
204
|
|
|
} else { |
205
|
|
|
$members = $this->getMembers($circleId, $instance, $initiator, $initiatorType, $tree); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if (!is_null($tree)) { |
209
|
|
|
$this->drawTree( |
210
|
|
|
$tree, [$this, 'displayLeaf'], |
211
|
|
|
[ |
212
|
|
|
'height' => 3, |
213
|
|
|
'node-spacing' => 1, |
214
|
|
|
'item-spacing' => 0, |
215
|
|
|
] |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
return 0; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if (strtolower($input->getOption('output')) === 'json') { |
222
|
|
|
$output->writeln(json_encode($members, JSON_PRETTY_PRINT)); |
223
|
|
|
|
224
|
|
|
return 0; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$output = new ConsoleOutput(); |
228
|
|
|
$output = $output->section(); |
229
|
|
|
|
230
|
|
|
$table = new Table($output); |
231
|
|
|
$table->setHeaders( |
232
|
|
|
[ |
233
|
|
|
'Circle Id', 'Circle Name', 'Member Id', 'Single Id', 'Type', 'Source', |
234
|
|
|
'Username', 'Instance', 'Level', 'Invited By' |
235
|
|
|
] |
236
|
|
|
); |
237
|
|
|
$table->render(); |
238
|
|
|
|
239
|
|
|
foreach ($members as $member) { |
240
|
|
|
if ($member->getCircleId() === $circleId) { |
241
|
|
|
$level = $member->getLevel(); |
242
|
|
|
} else { |
243
|
|
|
$level = $member->getInheritanceFrom()->getLevel(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$table->appendRow( |
247
|
|
|
[ |
248
|
|
|
$member->getCircleId(), |
249
|
|
|
$member->getCircle()->getDisplayName(), |
250
|
|
|
$member->getId(), |
251
|
|
|
$member->getSingleId(), |
252
|
|
|
Member::$TYPE[$member->getUserType()], |
253
|
|
|
$member->hasBasedOn() ? Circle::$DEF_SOURCE[$member->getBasedOn()->getSource()] : '', |
254
|
|
|
$this->configService->displayFederatedUser($member, $this->input->getOption('display-name')), |
255
|
|
|
$this->configService->displayInstance($member->getInstance()), |
256
|
|
|
($level > 0) ? Member::$DEF_LEVEL[$level] : |
257
|
|
|
'(' . strtolower($member->getStatus()) . ')', |
258
|
|
|
($member->hasInvitedBy()) ? $this->configService->displayFederatedUser( |
259
|
|
|
$member->getInvitedBy(), $this->input->getOption('display-name') |
260
|
|
|
) : 'Unknown' |
261
|
|
|
] |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return 0; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param string $circleId |
271
|
|
|
* @param string $instance |
272
|
|
|
* @param string $initiator |
273
|
|
|
* @param int $initiatorType |
274
|
|
|
* @param NC22TreeNode|null $tree |
275
|
|
|
* @param array $knownIds |
276
|
|
|
* |
277
|
|
|
* @return array |
278
|
|
|
* @throws CircleNotFoundException |
279
|
|
|
* @throws FederatedItemException |
280
|
|
|
* @throws FederatedUserException |
281
|
|
|
* @throws FederatedUserNotFoundException |
282
|
|
|
* @throws InitiatorNotFoundException |
283
|
|
|
* @throws InvalidIdException |
284
|
|
|
* @throws InvalidItemException |
285
|
|
|
* @throws MemberNotFoundException |
286
|
|
|
* @throws OwnerNotFoundException |
287
|
|
|
* @throws RemoteInstanceException |
288
|
|
|
* @throws RemoteNotFoundException |
289
|
|
|
* @throws RemoteResourceNotFoundException |
290
|
|
|
* @throws RequestBuilderException |
291
|
|
|
* @throws RequestNetworkException |
292
|
|
|
* @throws SignatoryException |
293
|
|
|
* @throws SingleCircleNotFoundException |
294
|
|
|
* @throws UnknownRemoteException |
295
|
|
|
* @throws UserTypeNotFoundException |
296
|
|
|
*/ |
297
|
|
|
private function getMembers( |
298
|
|
|
string $circleId, |
299
|
|
|
string $instance, |
300
|
|
|
string $initiator, |
301
|
|
|
int $initiatorType, |
302
|
|
|
?NC22TreeNode $tree, |
303
|
|
|
array $knownIds = [] |
304
|
|
|
): array { |
305
|
|
|
if (in_array($circleId, $knownIds)) { |
306
|
|
|
return []; |
307
|
|
|
} |
308
|
|
|
$knownIds[] = $circleId; |
309
|
|
|
|
310
|
|
|
if (!$this->configService->isLocalInstance($instance)) { |
311
|
|
|
$data = []; |
312
|
|
|
if ($initiator) { |
313
|
|
|
$data['initiator'] = $this->federatedUserService->getFederatedUser( |
314
|
|
|
$initiator, |
315
|
|
|
Member::TYPE_USER |
316
|
|
|
); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
try { |
320
|
|
|
$members = $this->remoteService->getMembersFromInstance($circleId, $instance, $data); |
321
|
|
|
} catch (RemoteInstanceException $e) { |
322
|
|
|
return []; |
323
|
|
|
} |
324
|
|
|
} else { |
325
|
|
|
$this->federatedUserService->commandLineInitiator($initiator, $initiatorType, $circleId, true); |
326
|
|
|
$members = $this->memberService->getMembers($circleId); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
if (!is_null($tree)) { |
330
|
|
|
foreach ($members as $member) { |
331
|
|
|
if ($member->getUserType() === Member::TYPE_CIRCLE) { |
332
|
|
|
if (!$this->configService->isLocalInstance($member->getInstance())) { |
333
|
|
|
$data = []; |
334
|
|
|
if ($initiator) { |
335
|
|
|
$data['initiator'] = $this->federatedUserService->getFederatedUser( |
336
|
|
|
$initiator, |
337
|
|
|
Member::TYPE_USER |
338
|
|
|
); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
$circle = null; |
342
|
|
|
try { |
343
|
|
|
$circle = $this->remoteService->getCircleFromInstance( |
344
|
|
|
$member->getSingleId(), $member->getInstance(), $data |
345
|
|
|
); |
346
|
|
|
} catch (CircleNotFoundException | RemoteInstanceException $e) { |
|
|
|
|
347
|
|
|
} |
348
|
|
|
} else { |
349
|
|
|
$this->federatedUserService->commandLineInitiator( |
350
|
|
|
$initiator, |
351
|
|
|
$initiatorType, |
352
|
|
|
$member->getSingleId(), |
353
|
|
|
true |
354
|
|
|
); |
355
|
|
|
$circle = $this->circleService->getCircle($member->getSingleId(), 0); |
356
|
|
|
} |
357
|
|
|
$node = new NC22TreeNode( |
358
|
|
|
$tree, new SimpleDataStore( |
359
|
|
|
[ |
360
|
|
|
'circle' => $circle, |
361
|
|
|
'member' => $member, |
362
|
|
|
'cycling' => in_array($member->getSingleId(), $knownIds), |
363
|
|
|
] |
364
|
|
|
) |
365
|
|
|
); |
366
|
|
|
|
367
|
|
|
$this->getMembers( |
368
|
|
|
$member->getSingleId(), |
369
|
|
|
$member->getInstance(), |
370
|
|
|
$initiator, |
371
|
|
|
$initiatorType, |
372
|
|
|
$node, |
373
|
|
|
$knownIds |
374
|
|
|
); |
375
|
|
|
} else { |
376
|
|
|
if ($this->treeType !== 'circles-only') { |
377
|
|
|
new NC22TreeNode( |
378
|
|
|
$tree, new SimpleDataStore( |
379
|
|
|
[ |
380
|
|
|
'member' => $member, |
381
|
|
|
'cycling' => in_array($member->getSingleId(), $knownIds) |
382
|
|
|
] |
383
|
|
|
) |
384
|
|
|
); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
return $members; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @param SimpleDataStore $data |
396
|
|
|
* @param int $lineNumber |
397
|
|
|
* |
398
|
|
|
* @return string |
399
|
|
|
* @throws OwnerNotFoundException |
400
|
|
|
*/ |
401
|
|
|
public function displayLeaf(SimpleDataStore $data, int $lineNumber): string { |
402
|
|
|
if ($lineNumber === 3) { |
403
|
|
|
return ($data->gBool('cycling')) ? '<comment>(loop detected)</comment>' : ''; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
try { |
407
|
|
|
$line = ''; |
408
|
|
|
$circle = null; |
409
|
|
|
if ($data->hasKey('circle')) { |
410
|
|
|
/** @var Circle $circle */ |
411
|
|
|
$circle = $data->gObj('circle', Circle::class); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
if ($data->hasKey('member')) { |
415
|
|
|
/** @var Member $member */ |
416
|
|
|
$member = $data->gObj('member', Member::class); |
417
|
|
|
|
418
|
|
|
if ($lineNumber === 1) { |
419
|
|
|
$line .= '<info>' . $member->getSingleId() . '</info>'; |
420
|
|
|
if (!$this->configService->isLocalInstance($member->getInstance())) { |
421
|
|
|
$line .= '@' . $member->getInstance(); |
422
|
|
|
} |
423
|
|
|
$line .= ' (' . Member::$DEF_LEVEL[$member->getLevel()] . ')'; |
424
|
|
|
|
425
|
|
|
$line .= ' <info>MemberId</info>: ' . $member->getId(); |
426
|
|
|
$name = ($this->input->getOption('display-name')) ? |
427
|
|
|
$member->getDisplayName() : $member->getUserId(); |
428
|
|
|
$line .= ' <info>Name</info>: ' . $name; |
429
|
|
|
$source = ($member->hasBasedOn()) ? $member->getBasedOn()->getSource() : ''; |
430
|
|
|
$line .= ' <info>Source</info>: ' . Circle::$DEF_SOURCE[$source]; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
if ($lineNumber === 2) { |
434
|
|
|
if (is_null($circle)) { |
435
|
|
|
if ($member->getUserType() === Member::TYPE_CIRCLE) { |
436
|
|
|
$line .= '<comment>(out of bounds)</comment>'; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
return $line; |
440
|
|
|
} |
441
|
|
|
$owner = $circle->getOwner(); |
442
|
|
|
$line .= '<info>Owner</info>: ' . $owner->getUserId() . '@' . $owner->getInstance(); |
443
|
|
|
$type = implode(", ", Circle::getCircleFlags($circle, Circle::FLAGS_LONG)); |
444
|
|
|
$line .= ($type === '') ? '' : ' <info>Config</info>: ' . $type; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
} else { |
448
|
|
|
if ($lineNumber === 1 && !is_null($circle)) { |
449
|
|
|
$line .= '<info>' . $circle->getSingleId() . '</info>'; |
450
|
|
|
if (!$this->configService->isLocalInstance($circle->getInstance())) { |
451
|
|
|
$line .= '@' . $circle->getInstance(); |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
return $line; |
457
|
|
|
|
458
|
|
|
} catch (InvalidItemException | ItemNotFoundException | UnknownTypeException $e) { |
|
|
|
|
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
return ''; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
|