From 5bbf5f09b90230973e77aed8847d84c737c91d5d Mon Sep 17 00:00:00 2001 From: Hamza Date: Wed, 12 Nov 2025 15:53:27 +0100 Subject: [PATCH] fix(encryption): limit oprhaned keys scan to one user Signed-off-by: Hamza --- .../lib/Command/CleanOrphanedKeys.php | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/apps/encryption/lib/Command/CleanOrphanedKeys.php b/apps/encryption/lib/Command/CleanOrphanedKeys.php index 74196c202429d..4825f0c65225a 100644 --- a/apps/encryption/lib/Command/CleanOrphanedKeys.php +++ b/apps/encryption/lib/Command/CleanOrphanedKeys.php @@ -22,6 +22,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\QuestionHelper; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; @@ -45,10 +46,28 @@ public function __construct( protected function configure(): void { $this ->setName('encryption:clean-orphaned-keys') - ->setDescription('Scan the keys storage for orphaned keys and remove them'); + ->setDescription('Scan the keys storage for orphaned keys and remove them') + ->addArgument( + 'user', + InputArgument::OPTIONAL, + 'The id of the user for whom the scan should be performed. If not provided, all users will be scanned.' + ); } protected function execute(InputInterface $input, OutputInterface $output): int { + $users = []; + $userId = $input->getArgument('user'); + if ($userId !== null) { + $user = $this->userManager->get($userId); + if (!$user) { + $output->writeln("User $userId not found"); + return self::FAILURE; + } + $users[] = $user; + } else { + $users = $this->userManager->getSeenUsers(); + } + $orphanedKeys = []; $headline = 'Scanning all keys for file parity'; $output->writeln($headline); @@ -57,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $progress = new ProgressBar($output); $progress->setFormat(" %message% \n [%bar%]"); - foreach ($this->userManager->getSeenUsers() as $user) { + foreach ($users as $user) { $uid = $user->getUID(); $progress->setMessage('Scanning all keys for: ' . $uid); $progress->advance(); @@ -65,8 +84,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $root = $this->encryptionUtil->getKeyStorageRoot() . '/' . $uid . '/files_encryption/keys'; $userOrphanedKeys = $this->scanFolder($output, $root, $uid); $orphanedKeys = array_merge($orphanedKeys, $userOrphanedKeys); + $progress->setMessage('Scanned orphaned keys for user: ' . $uid); } - $progress->setMessage('Scanned orphaned keys for all users'); $progress->finish(); $output->writeln("\n"); foreach ($orphanedKeys as $keyPath) { @@ -79,7 +98,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($this->questionHelper->ask($input, $output, $question)) { $this->deleteAll($orphanedKeys, $output); } else { - $question = new ConfirmationQuestion('Do you want to delete specific keys? (y/n) ', false); if ($this->questionHelper->ask($input, $output, $question)) { $this->deleteSpecific($input, $output, $orphanedKeys);