|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 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\IUserManager; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Input\InputOption; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +class PreMigrate extends Base { |
| 20 | + public function __construct( |
| 21 | + private PrimaryObjectStoreConfig $objectStoreConfig, |
| 22 | + private IUserManager $userManager, |
| 23 | + private IConfig $config, |
| 24 | + ) { |
| 25 | + parent::__construct(); |
| 26 | + } |
| 27 | + |
| 28 | + protected function configure(): void { |
| 29 | + parent::configure(); |
| 30 | + $this |
| 31 | + ->setName('files:object:multi:pre-migrate') |
| 32 | + ->setDescription('Assign a configured object store to users who don\'t have one assigned yet.') |
| 33 | + ->addOption('object-store', 'o', InputOption::VALUE_REQUIRED, 'The name of the configured object store') |
| 34 | + ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'The userId of the user to assign the object store') |
| 35 | + ->addOption('all', 'a', InputOption::VALUE_NONE, 'Assign the object store to all users'); |
| 36 | + } |
| 37 | + |
| 38 | + public function execute(InputInterface $input, OutputInterface $output): int { |
| 39 | + $objectStore = $input->getOption('object-store'); |
| 40 | + if (!$objectStore) { |
| 41 | + $output->writeln('Please specify the object store'); |
| 42 | + return 1; |
| 43 | + } |
| 44 | + |
| 45 | + $configs = $this->objectStoreConfig->getObjectStoreConfigs(); |
| 46 | + if (!isset($configs[$objectStore])) { |
| 47 | + $output->writeln('<error>Unknown object store configuration: ' . $objectStore . '</error>'); |
| 48 | + return 1; |
| 49 | + } |
| 50 | + |
| 51 | + if ($input->getOption('all')) { |
| 52 | + $users = $this->userManager->getSeenUsers(); |
| 53 | + } elseif ($userId = $input->getOption('user')) { |
| 54 | + $user = $this->userManager->get($userId); |
| 55 | + if (!$user) { |
| 56 | + $output->writeln('<error>User ' . $userId . ' not found</error>'); |
| 57 | + return 1; |
| 58 | + } |
| 59 | + $users = new \ArrayIterator([$user]); |
| 60 | + } else { |
| 61 | + $output->writeln('<comment>Please specify a user id with --user or --all for all users</comment>'); |
| 62 | + return 1; |
| 63 | + } |
| 64 | + |
| 65 | + $count = 0; |
| 66 | + foreach ($users as $user) { |
| 67 | + if (!$this->config->getUserValue($user->getUID(), 'homeobjectstore', 'objectstore', null)) { |
| 68 | + $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'objectstore', $objectStore); |
| 69 | + $count++; |
| 70 | + } |
| 71 | + } |
| 72 | + $output->writeln('Assigned object store <info>' . $objectStore . '</info> to <info>' . $count . '</info> users'); |
| 73 | + |
| 74 | + return 0; |
| 75 | + } |
| 76 | +} |
0 commit comments