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