memberRequest = $memberRequest; $this->federatedUserService = $federatedUserService; $this->remoteService = $remoteService; $this->circleService = $circleService; $this->memberService = $memberService; $this->configService = $configService; } protected function configure() { parent::configure(); $this->setName('circles:members:list') ->setDescription('listing Members from a Team') ->addArgument('circle_id', InputArgument::REQUIRED, 'ID of the team') ->addOption('instance', '', InputOption::VALUE_REQUIRED, 'Instance of the team', '') ->addOption('inherited', '', InputOption::VALUE_NONE, 'Display all inherited members') ->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '') ->addOption('initiator-type', '', InputOption::VALUE_REQUIRED, 'set initiator type', '0') ->addOption('display-name', '', InputOption::VALUE_NONE, 'display the displayName') ->addOption('tree', '', InputOption::VALUE_OPTIONAL, 'display members as a tree', false); } /** * @param InputInterface $input * @param OutputInterface $output * * @return int * @throws CircleNotFoundException * @throws FederatedUserException * @throws FederatedUserNotFoundException * @throws InitiatorNotFoundException * @throws InvalidIdException * @throws InvalidItemException * @throws MemberNotFoundException * @throws OwnerNotFoundException * @throws RemoteInstanceException * @throws RemoteNotFoundException * @throws RemoteResourceNotFoundException * @throws RequestNetworkException * @throws SignatoryException * @throws UnknownRemoteException * @throws UserTypeNotFoundException * @throws FederatedItemException * @throws SingleCircleNotFoundException * @throws RequestBuilderException */ protected function execute(InputInterface $input, OutputInterface $output): int { $this->input = $input; $circleId = $input->getArgument('circle_id'); $instance = $input->getOption('instance'); $initiator = $input->getOption('initiator'); $initiatorType = Member::parseTypeString($input->getOption('initiator-type')); $inherited = $input->getOption('inherited'); $tree = null; if ($input->getOption('tree') !== false) { $this->treeType = (is_null($input->getOption('tree'))) ? 'all' : $input->getOption('tree'); $this->federatedUserService->commandLineInitiator($initiator, $initiatorType, $circleId, true); $circle = $this->circleService->getCircle($circleId); $output->writeln('Name: ' . $circle->getName()); $owner = $circle->getOwner(); $output->writeln('Owner: ' . $owner->getUserId() . '@' . $owner->getInstance()); $type = implode(', ', Circle::getCircleFlags($circle, Circle::FLAGS_LONG)); $output->writeln('Config: ' . $type); $output->writeln(' '); $tree = new TreeNode(null, new SimpleDataStore(['circle' => $circle])); $inherited = false; } if ($inherited) { $this->federatedUserService->commandLineInitiator($initiator, $initiatorType, $circleId, true); $circle = $this->circleService->getCircle($circleId); $members = $circle->getInheritedMembers(true); } else { $members = $this->getMembers($circleId, $instance, $initiator, $initiatorType, $tree); } if (!is_null($tree)) { $this->drawTree( $tree, [$this, 'displayLeaf'], [ 'height' => 3, 'node-spacing' => 1, 'item-spacing' => 0, ] ); return 0; } if (strtolower($input->getOption('output')) === 'json') { $output->writeln(json_encode($members, JSON_PRETTY_PRINT)); return 0; } $output = new ConsoleOutput(); $output = $output->section(); $table = new Table($output); $table->setHeaders( [ 'Circle Id', 'Circle Name', 'Member Id', 'Single Id', 'Type', 'Source', 'Username', 'Level', 'Invited By' ] ); $rows = []; foreach ($members as $member) { if ($member->getCircleId() === $circleId) { $level = $member->getLevel(); } else { $level = $member->getInheritanceFrom()->getLevel(); } $rows[] = [ $member->getCircleId(), $member->getCircle()->getDisplayName(), $member->getId(), $member->getSingleId(), Member::$TYPE[$member->getUserType()], $member->hasBasedOn() ? Circle::$DEF_SOURCE[$member->getBasedOn()->getSource()] : '', $this->configService->displayFederatedUser( $member, $this->input->getOption('display-name') ), ($level > 0) ? Member::$DEF_LEVEL[$level] : '(' . strtolower($member->getStatus()) . ')', ($member->hasInvitedBy()) ? $this->configService->displayFederatedUser( $member->getInvitedBy(), $this->input->getOption('display-name') ) : 'Unknown' ]; } $table->setRows($rows); $table->render(); return 0; } /** * @param string $circleId * @param string $instance * @param string $initiator * @param int $initiatorType * @param TreeNode|null $tree * @param array $knownIds * * @return list * @throws CircleNotFoundException * @throws FederatedItemException * @throws FederatedUserException * @throws FederatedUserNotFoundException * @throws InitiatorNotFoundException * @throws InvalidIdException * @throws InvalidItemException * @throws MemberNotFoundException * @throws OwnerNotFoundException * @throws RemoteInstanceException * @throws RemoteNotFoundException * @throws RemoteResourceNotFoundException * @throws RequestBuilderException * @throws RequestNetworkException * @throws SignatoryException * @throws SingleCircleNotFoundException * @throws UnknownRemoteException * @throws UserTypeNotFoundException */ private function getMembers( string $circleId, string $instance, string $initiator, int $initiatorType, ?TreeNode $tree, array $knownIds = [], ): array { if (in_array($circleId, $knownIds)) { return []; } $knownIds[] = $circleId; if (!$this->configService->isLocalInstance($instance)) { $data = []; if ($initiator) { $data['initiator'] = $this->federatedUserService->getFederatedUser( $initiator, Member::TYPE_USER ); } try { $members = $this->remoteService->getMembersFromInstance($circleId, $instance, $data); } catch (RemoteInstanceException $e) { return []; } } else { $this->federatedUserService->commandLineInitiator($initiator, $initiatorType, $circleId, true); $members = $this->memberService->getMembers($circleId, true); } if (!is_null($tree)) { foreach ($members as $member) { if ($member->getUserType() === Member::TYPE_CIRCLE) { if (!$this->configService->isLocalInstance($member->getInstance())) { $data = []; if ($initiator) { $data['initiator'] = $this->federatedUserService->getFederatedUser( $initiator, Member::TYPE_USER ); } $circle = null; try { $circle = $this->remoteService->getCircleFromInstance( $member->getSingleId(), $member->getInstance(), $data ); } catch (CircleNotFoundException|RemoteInstanceException $e) { } } else { $this->federatedUserService->commandLineInitiator( $initiator, $initiatorType, $member->getSingleId(), true ); $circle = $this->circleService->getCircle($member->getSingleId()); } $node = new TreeNode( $tree, new SimpleDataStore( [ 'circle' => $circle, 'member' => $member, 'cycling' => in_array($member->getSingleId(), $knownIds), ] ) ); $this->getMembers( $member->getSingleId(), $member->getInstance(), $initiator, $initiatorType, $node, $knownIds ); } else { if ($this->treeType !== 'circles-only') { new TreeNode( $tree, new SimpleDataStore( [ 'member' => $member, 'cycling' => in_array($member->getSingleId(), $knownIds) ] ) ); } } } } return $members; } /** * @param SimpleDataStore $data * @param int $lineNumber * * @return string * @throws OwnerNotFoundException */ public function displayLeaf(SimpleDataStore $data, int $lineNumber): string { if ($lineNumber === 3) { return ($data->gBool('cycling')) ? '(loop detected)' : ''; } try { $line = ''; $circle = null; if ($data->hasKey('circle')) { try { $circle = $data->gObj('circle', Circle::class); /** @var Circle $circle */ } catch (Exception $e) { } } if ($data->hasKey('member')) { /** @var Member $member */ $member = $data->gObj('member', Member::class); if ($lineNumber === 1) { $line .= '' . $member->getSingleId() . ''; if (!$this->configService->isLocalInstance($member->getInstance())) { $line .= '@' . $member->getInstance(); } $line .= ' (' . Member::$DEF_LEVEL[$member->getLevel()] . ')'; $line .= ' MemberId: ' . $member->getId(); $line .= ' Name: ' . $this->configService->displayFederatedUser( $member, $this->input->getOption('display-name') ); if ($member->hasBasedOn()) { $line .= ' Source: ' . Circle::$DEF_SOURCE[$member->getBasedOn()->getSource()]; } else { $line .= ' Type: ' . Member::$TYPE[$member->getUserType()]; } } if ($lineNumber === 2) { if (is_null($circle)) { if ($member->getUserType() === Member::TYPE_CIRCLE) { $line .= '(out of bounds)'; } return $line; } $owner = $circle->getOwner(); $line .= 'Owner: ' . $owner->getUserId() . '@' . $owner->getInstance(); $type = implode(', ', Circle::getCircleFlags($circle, Circle::FLAGS_LONG)); $line .= ($type === '') ? '' : ' Config: ' . $type; } } else { if ($lineNumber === 1 && !is_null($circle)) { $line .= '' . $circle->getSingleId() . ''; if (!$this->configService->isLocalInstance($circle->getInstance())) { $line .= '@' . $circle->getInstance(); } } } return $line; } catch (InvalidItemException|ItemNotFoundException|UnknownTypeException $e) { } return ''; } }