Completed
Push — master ( 3ddca8...414a73 )
by Maxence
02:57
created

MembersList::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 daita\MySmallPhpTools\Exceptions\InvalidItemException;
35
use daita\MySmallPhpTools\Exceptions\ItemNotFoundException;
36
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
37
use daita\MySmallPhpTools\Exceptions\SignatoryException;
38
use daita\MySmallPhpTools\Exceptions\UnknownTypeException;
39
use daita\MySmallPhpTools\Model\Nextcloud\nc22\NC22TreeNode;
40
use daita\MySmallPhpTools\Model\SimpleDataStore;
41
use daita\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('display-name', '', InputOption::VALUE_NONE, 'display the displayName')
145
			 ->addOption('tree', '', InputOption::VALUE_OPTIONAL, 'display members as a tree', false);
146
	}
147
148
149
	/**
150
	 * @param InputInterface $input
151
	 * @param OutputInterface $output
152
	 *
153
	 * @return int
154
	 * @throws CircleNotFoundException
155
	 * @throws FederatedUserException
156
	 * @throws FederatedUserNotFoundException
157
	 * @throws InitiatorNotFoundException
158
	 * @throws InvalidIdException
159
	 * @throws InvalidItemException
160
	 * @throws MemberNotFoundException
161
	 * @throws OwnerNotFoundException
162
	 * @throws RemoteInstanceException
163
	 * @throws RemoteNotFoundException
164
	 * @throws RemoteResourceNotFoundException
165
	 * @throws RequestNetworkException
166
	 * @throws SignatoryException
167
	 * @throws UnknownRemoteException
168
	 * @throws UserTypeNotFoundException
169
	 * @throws FederatedItemException
170
	 * @throws SingleCircleNotFoundException
171
	 * @throws RequestBuilderException
172
	 */
173
	protected function execute(InputInterface $input, OutputInterface $output): int {
174
		$this->input = $input;
175
		$circleId = $input->getArgument('circle_id');
176
		$instance = $input->getOption('instance');
177
		$initiator = $input->getOption('initiator');
178
		$inherited = $input->getOption('inherited');
179
180
		$tree = null;
181
		if ($input->getOption('tree') !== false) {
182
			$this->treeType = (is_null($input->getOption('tree'))) ? 'all' : $input->getOption('tree');
183
184
			$this->federatedUserService->commandLineInitiator($initiator, $circleId, true);
185
			$circle = $this->circleService->getCircle($circleId);
186
187
			$output->writeln('<info>Name</info>: ' . $circle->getName());
188
			$owner = $circle->getOwner();
189
			$output->writeln('<info>Owner</info>: ' . $owner->getUserId() . '@' . $owner->getInstance());
190
			$type = implode(", ", Circle::getCircleFlags($circle, Circle::FLAGS_LONG));
191
			$output->writeln('<info>Config</info>: ' . $type);
192
			$output->writeln(' ');
193
194
			$tree = new NC22TreeNode(null, new SimpleDataStore(['circle' => $circle]));
195
			$inherited = false;
196
		}
197
198
		if ($inherited) {
199
			$this->federatedUserService->commandLineInitiator($initiator, $circleId, true);
200
			$circle = $this->circleService->getCircle($circleId);
201
			$members = $circle->getInheritedMembers(true);
202
		} else {
203
			$members = $this->getMembers($circleId, $instance, $initiator, $tree);
204
		}
205
206
		if (!is_null($tree)) {
207
			$this->drawTree(
208
				$tree, [$this, 'displayLeaf'],
209
				[
210
					'height'       => 3,
211
					'node-spacing' => 1,
212
					'item-spacing' => 0,
213
				]
214
			);
215
216
			return 0;
217
		}
218
219
		if (strtolower($input->getOption('output')) === 'json') {
220
			$output->writeln(json_encode($members, JSON_PRETTY_PRINT));
221
222
			return 0;
223
		}
224
225
		$output = new ConsoleOutput();
226
		$output = $output->section();
227
228
		$table = new Table($output);
229
		$table->setHeaders(
230
			[
231
				'Circle Id', 'Circle Name', 'Member Id', 'Single Id', 'Type', 'Source', 'Username',
232
				'Instance', 'Level'
233
			]
234
		);
235
		$table->render();
236
237
		foreach ($members as $member) {
238
			if ($member->getCircleId() === $circleId) {
239
				$level = $member->getLevel();
240
			} else {
241
				$level = $member->getInheritanceFrom()->getLevel();
242
			}
243
244
			$table->appendRow(
245
				[
246
					$member->getCircleId(),
247
					$member->getCircle()->getDisplayName(),
248
					$member->getId(),
249
					$member->getSingleId(),
250
					Member::$TYPE[$member->getUserType()],
251
					$member->hasBasedOn() ? Circle::$DEF_SOURCE[$member->getBasedOn()->getSource()] : '',
252
					($this->input->getOption('display-name')) ?
253
						$member->getBasedOn()->getDisplayName() : $member->getUserId(),
254
					$this->configService->displayInstance($member->getInstance()),
255
					($level > 0) ? Member::$DEF_LEVEL[$level] :
256
						'(' . strtolower($member->getStatus()) . ')'
257
				]
258
			);
259
		}
260
261
		return 0;
262
	}
263
264
265
	/**
266
	 * @param string $circleId
267
	 * @param string $instance
268
	 * @param string $initiator
269
	 * @param NC22TreeNode|null $tree
270
	 * @param array $knownIds
271
	 *
272
	 * @return array
273
	 * @throws CircleNotFoundException
274
	 * @throws FederatedItemException
275
	 * @throws FederatedUserException
276
	 * @throws FederatedUserNotFoundException
277
	 * @throws InitiatorNotFoundException
278
	 * @throws InvalidIdException
279
	 * @throws InvalidItemException
280
	 * @throws MemberNotFoundException
281
	 * @throws OwnerNotFoundException
282
	 * @throws RemoteInstanceException
283
	 * @throws RemoteNotFoundException
284
	 * @throws RemoteResourceNotFoundException
285
	 * @throws RequestNetworkException
286
	 * @throws SignatoryException
287
	 * @throws SingleCircleNotFoundException
288
	 * @throws UnknownRemoteException
289
	 * @throws UserTypeNotFoundException
290
	 * @throws RequestBuilderException
291
	 */
292
	private function getMembers(
293
		string $circleId,
294
		string $instance,
295
		string $initiator,
296
		?NC22TreeNode $tree,
297
		array $knownIds = []
298
	): array {
299
		if (in_array($circleId, $knownIds)) {
300
			return [];
301
		}
302
		$knownIds[] = $circleId;
303
304
		if (!$this->configService->isLocalInstance($instance)) {
305
			$data = [];
306
			if ($initiator) {
307
				$data['initiator'] = $this->federatedUserService->getFederatedUser(
308
					$initiator,
309
					Member::TYPE_USER
310
				);
311
			}
312
313
			try {
314
				$members = $this->remoteService->getMembersFromInstance($circleId, $instance, $data);
315
			} catch (RemoteInstanceException $e) {
316
				return [];
317
			}
318
		} else {
319
			$this->federatedUserService->commandLineInitiator($initiator, $circleId, true);
320
			$members = $this->memberService->getMembers($circleId);
321
		}
322
323
		if (!is_null($tree)) {
324
			foreach ($members as $member) {
325
				if ($member->getUserType() === Member::TYPE_CIRCLE) {
326
					if (!$this->configService->isLocalInstance($member->getInstance())) {
327
						$data = [];
328
						if ($initiator) {
329
							$data['initiator'] = $this->federatedUserService->getFederatedUser(
330
								$initiator,
331
								Member::TYPE_USER
332
							);
333
						}
334
335
						$circle = null;
336
						try {
337
							$circle = $this->remoteService->getCircleFromInstance(
338
								$member->getSingleId(), $member->getInstance(), $data
339
							);
340
						} catch (CircleNotFoundException | RemoteInstanceException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
341
						}
342
					} else {
343
						$this->federatedUserService->commandLineInitiator(
344
							$initiator, $member->getSingleId(), true
345
						);
346
						$circle = $this->circleService->getCircle($member->getSingleId(), 0);
347
					}
348
					$node = new NC22TreeNode(
349
						$tree, new SimpleDataStore(
350
								 [
351
									 'circle'  => $circle,
352
									 'member'  => $member,
353
									 'cycling' => in_array($member->getSingleId(), $knownIds),
354
								 ]
355
							 )
356
					);
357
358
					$this->getMembers(
359
						$member->getSingleId(), $member->getInstance(), $initiator, $node, $knownIds
360
					);
361
				} else {
362
					if ($this->treeType !== 'circles-only') {
363
						new NC22TreeNode(
364
							$tree, new SimpleDataStore(
365
									 [
366
										 'member'  => $member,
367
										 'cycling' => in_array($member->getSingleId(), $knownIds)
368
									 ]
369
								 )
370
						);
371
					}
372
				}
373
			}
374
		}
375
376
		return $members;
377
	}
378
379
380
	/**
381
	 * @param SimpleDataStore $data
382
	 * @param int $lineNumber
383
	 *
384
	 * @return string
385
	 * @throws OwnerNotFoundException
386
	 */
387
	public function displayLeaf(SimpleDataStore $data, int $lineNumber): string {
388
		if ($lineNumber === 3) {
389
			return ($data->gBool('cycling')) ? '<comment>(loop detected)</comment>' : '';
390
		}
391
392
		try {
393
			$line = '';
394
			$circle = null;
395
			if ($data->hasKey('circle')) {
396
				/** @var Circle $circle */
397
				$circle = $data->gObj('circle', Circle::class);
398
			}
399
400
			if ($data->hasKey('member')) {
401
				/** @var Member $member */
402
				$member = $data->gObj('member', Member::class);
403
404
				if ($lineNumber === 1) {
405
					$line .= '<info>' . $member->getSingleId() . '</info>';
406
					if (!$this->configService->isLocalInstance($member->getInstance())) {
407
						$line .= '@' . $member->getInstance();
408
					}
409
					$line .= ' (' . Member::$DEF_LEVEL[$member->getLevel()] . ')';
410
411
					$line .= ' <info>MemberId</info>: ' . $member->getId();
412
					$name = ($this->input->getOption('display-name')) ?
413
						$member->getBasedOn()->getDisplayName() : $member->getUserId();
414
					$line .= ' <info>Name</info>: ' . $name;
415
					$source = ($member->hasBasedOn()) ? $member->getBasedOn()->getSource() : '';
416
					$line .= ' <info>Source</info>: ' . Circle::$DEF_SOURCE[$source];
417
				}
418
419
				if ($lineNumber === 2) {
420
					if (is_null($circle)) {
421
						if ($member->getUserType() === Member::TYPE_CIRCLE) {
422
							$line .= '<comment>(out of bounds)</comment>';
423
						}
424
425
						return $line;
426
					}
427
					$owner = $circle->getOwner();
428
					$line .= '<info>Owner</info>: ' . $owner->getUserId() . '@' . $owner->getInstance();
429
					$type = implode(", ", Circle::getCircleFlags($circle, Circle::FLAGS_LONG));
430
					$line .= ($type === '') ? '' : ' <info>Config</info>: ' . $type;
431
				}
432
433
			} else {
434
				if ($lineNumber === 1 && !is_null($circle)) {
435
					$line .= '<info>' . $circle->getSingleId() . '</info>';
436
					if (!$this->configService->isLocalInstance($circle->getInstance())) {
437
						$line .= '@' . $circle->getInstance();
438
					}
439
				}
440
			}
441
442
			return $line;
443
444
		} catch (InvalidItemException | ItemNotFoundException | UnknownTypeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
445
		}
446
447
		return '';
448
	}
449
450
}
451
452