Completed
Push — master ( d9aa67...99a3d1 )
by Maxence
02:21
created

MembersList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 6
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 ($instance !== '' && !$this->configService->isLocalInstance($instance)) {
305
			$data = [];
306
			if ($initiator) {
307
				$data['initiator'] = $this->federatedUserService->getFederatedUser($initiator);
308
			}
309
310
			try {
311
				$members = $this->remoteService->getMembersFromInstance($circleId, $instance, $data);
312
			} catch (RemoteInstanceException $e) {
313
				return [];
314
			}
315
		} else {
316
			$this->federatedUserService->commandLineInitiator($initiator, $circleId, true);
317
			$members = $this->memberService->getMembers($circleId);
318
		}
319
320
		if (!is_null($tree)) {
321
			foreach ($members as $member) {
322
				if ($member->getUserType() === Member::TYPE_CIRCLE) {
323
					if (!$this->configService->isLocalInstance($member->getInstance())) {
324
						$data = [];
325
						if ($initiator) {
326
							$data['initiator'] = $this->federatedUserService->getFederatedUser($initiator);
327
						}
328
329
						$circle = null;
330
						try {
331
							$circle = $this->remoteService->getCircleFromInstance(
332
								$member->getSingleId(), $member->getInstance(), $data
333
							);
334
						} 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...
335
						}
336
					} else {
337
						$this->federatedUserService->commandLineInitiator(
338
							$initiator, $member->getSingleId(), true
339
						);
340
						$circle = $this->circleService->getCircle($member->getSingleId(), 0);
341
					}
342
					$node = new NC22TreeNode(
343
						$tree, new SimpleDataStore(
344
								 [
345
									 'circle'  => $circle,
346
									 'member'  => $member,
347
									 'cycling' => in_array($member->getSingleId(), $knownIds),
348
								 ]
349
							 )
350
					);
351
352
					$this->getMembers(
353
						$member->getSingleId(), $member->getInstance(), $initiator, $node, $knownIds
354
					);
355
				} else {
356
					if ($this->treeType !== 'circles-only') {
357
						new NC22TreeNode(
358
							$tree, new SimpleDataStore(
359
									 [
360
										 'member'  => $member,
361
										 'cycling' => in_array($member->getSingleId(), $knownIds)
362
									 ]
363
								 )
364
						);
365
					}
366
				}
367
			}
368
		}
369
370
		return $members;
371
	}
372
373
374
	/**
375
	 * @param SimpleDataStore $data
376
	 * @param int $lineNumber
377
	 *
378
	 * @return string
379
	 * @throws OwnerNotFoundException
380
	 */
381
	public function displayLeaf(SimpleDataStore $data, int $lineNumber): string {
382
		if ($lineNumber === 3) {
383
			return ($data->gBool('cycling')) ? '<comment>(loop detected)</comment>' : '';
384
		}
385
386
		try {
387
			$line = '';
388
			$circle = null;
389
			if ($data->hasKey('circle')) {
390
				/** @var Circle $circle */
391
				$circle = $data->gObj('circle', Circle::class);
392
			}
393
394
			if ($data->hasKey('member')) {
395
				/** @var Member $member */
396
				$member = $data->gObj('member', Member::class);
397
398
				if ($lineNumber === 1) {
399
					$line .= '<info>' . $member->getSingleId() . '</info>';
400
					if (!$this->configService->isLocalInstance($member->getInstance())) {
401
						$line .= '@' . $member->getInstance();
402
					}
403
					$line .= ' (' . Member::$DEF_LEVEL[$member->getLevel()] . ')';
404
405
					$line .= ' <info>MemberId</info>: ' . $member->getId();
406
					$name = ($this->input->getOption('display-name')) ?
407
						$member->getBasedOn()->getDisplayName() : $member->getUserId();
408
					$line .= ' <info>Name</info>: ' . $name;
409
					$source = ($member->hasBasedOn()) ? $member->getBasedOn()->getSource() : '';
410
					$line .= ' <info>Source</info>: ' . Circle::$DEF_SOURCE[$source];
411
				}
412
413
				if ($lineNumber === 2) {
414
					if (is_null($circle)) {
415
						if ($member->getUserType() === Member::TYPE_CIRCLE) {
416
							$line .= '<comment>(out of bounds)</comment>';
417
						}
418
419
						return $line;
420
					}
421
					$owner = $circle->getOwner();
422
					$line .= '<info>Owner</info>: ' . $owner->getUserId() . '@' . $owner->getInstance();
423
					$type = implode(", ", Circle::getCircleFlags($circle, Circle::FLAGS_LONG));
424
					$line .= ($type === '') ? '' : ' <info>Config</info>: ' . $type;
425
				}
426
427
			} else {
428
				if ($lineNumber === 1 && !is_null($circle)) {
429
					$line .= '<info>' . $circle->getSingleId() . '</info>';
430
					if (!$this->configService->isLocalInstance($circle->getInstance())) {
431
						$line .= '@' . $circle->getInstance();
432
					}
433
				}
434
			}
435
436
			return $line;
437
438
		} 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...
439
		}
440
441
		return '';
442
	}
443
444
}
445
446