|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2022 Daniel Kesselberg <mail@danielkesselberg.de> |
| 7 | + * |
| 8 | + * @author Daniel Kesselberg <mail@danielkesselberg.de> |
| 9 | + * |
| 10 | + * @license AGPL-3.0-or-later |
| 11 | + * |
| 12 | + * This code is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 14 | + * as published by the Free Software Foundation. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Affero General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License, version 3, |
| 22 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OCA\TwoFactorTOTP\Command; |
| 27 | + |
| 28 | +use OCA\TwoFactorTOTP\Db\TotpSecretMapper; |
| 29 | +use OCP\DB\Exception; |
| 30 | +use OCP\IDBConnection; |
| 31 | +use OCP\IUserManager; |
| 32 | +use Symfony\Component\Console\Command\Command; |
| 33 | +use Symfony\Component\Console\Input\InputInterface; |
| 34 | +use Symfony\Component\Console\Output\OutputInterface; |
| 35 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 36 | + |
| 37 | +class CleanUp extends Command { |
| 38 | + /** @var IDBConnection */ |
| 39 | + private $db; |
| 40 | + |
| 41 | + /** @var IUserManager */ |
| 42 | + private $userManager; |
| 43 | + |
| 44 | + /** @var TotpSecretMapper */ |
| 45 | + private $totpSecretMapper; |
| 46 | + |
| 47 | + public function __construct( |
| 48 | + IDBConnection $db, |
| 49 | + IUserManager $userManager, |
| 50 | + TotpSecretMapper $totpSecretMapper |
| 51 | + ) { |
| 52 | + parent::__construct(); |
| 53 | + |
| 54 | + $this->db = $db; |
| 55 | + $this->userManager = $userManager; |
| 56 | + $this->totpSecretMapper = $totpSecretMapper; |
| 57 | + } |
| 58 | + |
| 59 | + protected function configure(): void { |
| 60 | + $this |
| 61 | + ->setName('twofactor_totp:cleanup') |
| 62 | + ->setDescription('Remove orphaned totp secrets'); |
| 63 | + } |
| 64 | + |
| 65 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 66 | + $io = new SymfonyStyle($input, $output); |
| 67 | + $io->title('Remove totp secrets for deleted users'); |
| 68 | + |
| 69 | + foreach ($this->findUserIds() as $userId) { |
| 70 | + if ($this->userManager->userExists($userId) === false) { |
| 71 | + try { |
| 72 | + $io->text('Delete secret for uid "' . $userId . '"'); |
| 73 | + $this->totpSecretMapper->deleteSecretByUserId($userId); |
| 74 | + } catch (Exception $e) { |
| 75 | + $io->caution('Error deleting secret: ' . $e->getMessage()); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $io->success('Orphaned totp secrets removed.'); |
| 81 | + |
| 82 | + $io->text('Thank you for using Two-Factor TOTP!'); |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @throws Exception |
| 88 | + */ |
| 89 | + private function findUserIds(): array { |
| 90 | + $userIds = []; |
| 91 | + |
| 92 | + $qb = $this->db->getQueryBuilder() |
| 93 | + ->selectDistinct('user_id') |
| 94 | + ->from($this->totpSecretMapper->getTableName()); |
| 95 | + |
| 96 | + $result = $qb->executeQuery(); |
| 97 | + |
| 98 | + while ($row = $result->fetch()) { |
| 99 | + $userIds[] = $row['user_id']; |
| 100 | + } |
| 101 | + |
| 102 | + $result->closeCursor(); |
| 103 | + |
| 104 | + return $userIds; |
| 105 | + } |
| 106 | +} |
0 commit comments