|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 Robin Appelman <[email protected]> |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Files\Command\Object\Multi; |
| 10 | + |
| 11 | +use OC\Core\Command\Base; |
| 12 | +use OC\Files\ObjectStore\PrimaryObjectStoreConfig; |
| 13 | +use OCP\IConfig; |
| 14 | +use OCP\IUser; |
| 15 | +use OCP\IUserManager; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Input\InputOption; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | + |
| 20 | +class Users extends Base { |
| 21 | + public function __construct( |
| 22 | + private IUserManager $userManager, |
| 23 | + private PrimaryObjectStoreConfig $objectStoreConfig, |
| 24 | + private IConfig $config, |
| 25 | + ) { |
| 26 | + parent::__construct(); |
| 27 | + } |
| 28 | + |
| 29 | + protected function configure(): void { |
| 30 | + parent::configure(); |
| 31 | + $this |
| 32 | + ->setName('files:object:multi:users') |
| 33 | + ->setDescription('Get the mapping between users and object store buckets') |
| 34 | + ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, 'Only list users using the specified bucket') |
| 35 | + ->addOption('object-store', 'o', InputOption::VALUE_REQUIRED, 'Only list users using the specified object store configuration') |
| 36 | + ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Only show the mapping for the specified user, ignores all other options'); |
| 37 | + } |
| 38 | + |
| 39 | + public function execute(InputInterface $input, OutputInterface $output): int { |
| 40 | + if ($userId = $input->getOption('user')) { |
| 41 | + $user = $this->userManager->get($userId); |
| 42 | + if (!$user) { |
| 43 | + $output->writeln("<error>User $userId not found</error>"); |
| 44 | + return 1; |
| 45 | + } |
| 46 | + $users = new \ArrayIterator([$user]); |
| 47 | + } else { |
| 48 | + $bucket = (string) $input->getOption('bucket'); |
| 49 | + $objectStore = (string) $input->getOption('object-store'); |
| 50 | + if ($bucket !== '' && $objectStore === '') { |
| 51 | + $users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket)); |
| 52 | + } elseif ($bucket === '' && $objectStore !== '') { |
| 53 | + $users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore)); |
| 54 | + } elseif ($bucket) { |
| 55 | + $users = $this->getUsers(array_intersect( |
| 56 | + $this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket), |
| 57 | + $this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore) |
| 58 | + )); |
| 59 | + } else { |
| 60 | + $users = $this->userManager->getSeenUsers(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + $this->writeStreamingTableInOutputFormat($input, $output, $this->infoForUsers($users), 100); |
| 65 | + return 0; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param string[] $userIds |
| 70 | + * @return \Iterator<IUser> |
| 71 | + */ |
| 72 | + private function getUsers(array $userIds): \Iterator { |
| 73 | + foreach ($userIds as $userId) { |
| 74 | + $user = $this->userManager->get($userId); |
| 75 | + if ($user) { |
| 76 | + yield $user; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param \Iterator<IUser> $users |
| 83 | + * @return \Iterator<array> |
| 84 | + */ |
| 85 | + private function infoForUsers(\Iterator $users): \Iterator { |
| 86 | + foreach ($users as $user) { |
| 87 | + yield $this->infoForUser($user); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private function infoForUser(IUser $user): array { |
| 92 | + return [ |
| 93 | + 'user' => $user->getUID(), |
| 94 | + 'object-store' => $this->objectStoreConfig->getObjectStoreForUser($user), |
| 95 | + 'bucket' => $this->objectStoreConfig->getSetBucketForUser($user) ?? 'unset', |
| 96 | + ]; |
| 97 | + } |
| 98 | +} |
0 commit comments