Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions apps/encryption/lib/Command/CleanOrphanedKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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("<error>User $userId not found</error>");
return self::FAILURE;
}
$users[] = $user;
} else {
$users = $this->userManager->getSeenUsers();
}

$orphanedKeys = [];
$headline = 'Scanning all keys for file parity';
$output->writeln($headline);
Expand All @@ -57,16 +76,16 @@ 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();
$this->setupUserFileSystem($user);
$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) {
Expand All @@ -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);
Expand Down
Loading