|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\UserMigration\Command; |
| 11 | + |
| 12 | +use OC\Core\Command\Base; |
| 13 | +use OCA\UserMigration\AppInfo\Application; |
| 14 | +use OCA\UserMigration\Service\UserMigrationService; |
| 15 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 16 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 17 | +use OCP\IConfig; |
| 18 | +use OCP\IDBConnection; |
| 19 | +use OCP\IUserManager; |
| 20 | +use Symfony\Component\Console\Helper\QuestionHelper; |
| 21 | +use Symfony\Component\Console\Input\InputInterface; |
| 22 | +use Symfony\Component\Console\Input\InputOption; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 25 | + |
| 26 | +class Manage extends Base { |
| 27 | + public function __construct( |
| 28 | + private IDBConnection $connection, |
| 29 | + private IUserManager $userManager, |
| 30 | + private UserMigrationService $migrationService, |
| 31 | + private IConfig $config, |
| 32 | + private ITimeFactory $timeFactory, |
| 33 | + ) { |
| 34 | + parent::__construct(); |
| 35 | + } |
| 36 | + |
| 37 | + protected function configure(): void { |
| 38 | + parent::configure(); |
| 39 | + $this |
| 40 | + ->setName('user_migration:manage') |
| 41 | + ->setDescription('List users exported by the admin, delete them by batch') |
| 42 | + ->addOption( |
| 43 | + 'limit', |
| 44 | + 'l', |
| 45 | + InputOption::VALUE_REQUIRED, |
| 46 | + 'Limit the number of listed users', |
| 47 | + 100, |
| 48 | + ) |
| 49 | + ->addOption( |
| 50 | + 'since', |
| 51 | + null, |
| 52 | + InputOption::VALUE_REQUIRED, |
| 53 | + 'Filter by minimum export date', |
| 54 | + ) |
| 55 | + ->addOption( |
| 56 | + 'delete', |
| 57 | + null, |
| 58 | + InputOption::VALUE_NONE, |
| 59 | + 'Delete the exported users', |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 64 | + if ((string)$input->getOption('since') !== '') { |
| 65 | + $since = new \DateTime($input->getOption('since')); |
| 66 | + $output->writeln('<info>Since ' . $since->format(\DateTimeInterface::ATOM) . '</info>'); |
| 67 | + } else { |
| 68 | + $since = null; |
| 69 | + } |
| 70 | + $values = iterator_to_array($this->queryUsers((int)$input->getOption('limit'), $since)); |
| 71 | + $this->writeTableInOutputFormat($input, $output, $values); |
| 72 | + if ($input->getOption('delete')) { |
| 73 | + /** @var QuestionHelper $helper */ |
| 74 | + $helper = $this->getHelper('question'); |
| 75 | + $question = new ConfirmationQuestion('Please confirm to delete the above listed users [y/n]', !$input->isInteractive()); |
| 76 | + |
| 77 | + if (!$helper->ask($input, $output, $question)) { |
| 78 | + $output->writeln('<info>Deletion canceled</info>'); |
| 79 | + return self::SUCCESS; |
| 80 | + } |
| 81 | + $errors = $this->deleteUsers(array_column($values, 'userid'), $output); |
| 82 | + if ($errors > 0) { |
| 83 | + return self::FAILURE; |
| 84 | + } |
| 85 | + } |
| 86 | + return self::SUCCESS; |
| 87 | + } |
| 88 | + |
| 89 | + private function queryUsers(int $limit, ?\DateTime $since): \Generator { |
| 90 | + $qb = $this->connection->getQueryBuilder(); |
| 91 | + $qb->select('userid', 'configvalue') |
| 92 | + ->from('preferences') |
| 93 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter(Application::APP_ID))) |
| 94 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lastExport'))); |
| 95 | + |
| 96 | + if ($since !== null) { |
| 97 | + $qb->andWhere($qb->expr()->gte('configvalue', $qb->createNamedParameter($since->getTimestamp(), IQueryBuilder::PARAM_INT))); |
| 98 | + } |
| 99 | + |
| 100 | + $qb->orderBy('configvalue') |
| 101 | + ->setMaxResults($limit); |
| 102 | + |
| 103 | + $result = $qb->executeQuery(); |
| 104 | + |
| 105 | + while ($row = $result->fetch()) { |
| 106 | + yield [ |
| 107 | + 'userid' => $row['userid'], |
| 108 | + 'Last export' => date(\DateTimeInterface::ATOM, (int)$row['configvalue']) |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + $result->closeCursor(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param iterable<string> $uids |
| 117 | + */ |
| 118 | + private function deleteUsers(iterable $uids, OutputInterface $output): int { |
| 119 | + $errors = 0; |
| 120 | + foreach ($uids as $uid) { |
| 121 | + $user = $this->userManager->get($uid); |
| 122 | + if (is_null($user)) { |
| 123 | + $output->writeln('<error>User ' . $uid . ' does not exist</error>'); |
| 124 | + $errors++; |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + if ($user->delete()) { |
| 129 | + $output->writeln('<info>User "' . $uid . '" was deleted</info>'); |
| 130 | + } else { |
| 131 | + $output->writeln('<error>User "' . $uid . '" could not be deleted. Please check the logs.</error>'); |
| 132 | + $errors++; |
| 133 | + } |
| 134 | + } |
| 135 | + return $errors; |
| 136 | + } |
| 137 | +} |
0 commit comments