From 1b009e2f12dfaac28cfabf90d445c614b5bc3647 Mon Sep 17 00:00:00 2001 From: Faraz Samapoor Date: Thu, 3 Aug 2023 14:50:33 +0330 Subject: [PATCH] Uses early returns. To improve code readability. Signed-off-by: Faraz Samapoor --- .../lib/Command/DisableMasterKey.php | 32 ++++++++++--------- .../lib/Command/EnableMasterKey.php | 24 +++++++------- .../lib/Command/FixEncryptedVersion.php | 24 +++++++------- .../encryption/lib/Command/FixKeyLocation.php | 28 +++++++++------- 4 files changed, 59 insertions(+), 49 deletions(-) diff --git a/apps/encryption/lib/Command/DisableMasterKey.php b/apps/encryption/lib/Command/DisableMasterKey.php index 5fc8878c6b5e3..ce40f123f6abf 100644 --- a/apps/encryption/lib/Command/DisableMasterKey.php +++ b/apps/encryption/lib/Command/DisableMasterKey.php @@ -52,21 +52,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int if (!$isMasterKeyEnabled) { $output->writeln('Master key already disabled'); - } else { - $question = new ConfirmationQuestion( - 'Warning: Only perform this operation for a fresh installations with no existing encrypted data! ' - . 'There is no way to enable the master key again. ' - . 'We strongly recommend to keep the master key, it provides significant performance improvements ' - . 'and is easier to handle for both, users and administrators. ' - . 'Do you really want to switch to per-user keys? (y/n) ', false); - if ($this->questionHelper->ask($input, $output, $question)) { - $this->config->setAppValue('encryption', 'useMasterKey', '0'); - $output->writeln('Master key successfully disabled.'); - } else { - $output->writeln('aborted.'); - return self::FAILURE; - } + return self::SUCCESS; } - return self::SUCCESS; + + $question = new ConfirmationQuestion( + 'Warning: Only perform this operation for a fresh installations with no existing encrypted data! ' + . 'There is no way to enable the master key again. ' + . 'We strongly recommend to keep the master key, it provides significant performance improvements ' + . 'and is easier to handle for both, users and administrators. ' + . 'Do you really want to switch to per-user keys? (y/n) ', false); + + if ($this->questionHelper->ask($input, $output, $question)) { + $this->config->setAppValue('encryption', 'useMasterKey', '0'); + $output->writeln('Master key successfully disabled.'); + return self::SUCCESS; + } + + $output->writeln('aborted.'); + return self::FAILURE; } } diff --git a/apps/encryption/lib/Command/EnableMasterKey.php b/apps/encryption/lib/Command/EnableMasterKey.php index 44c3a7a72833f..b36a2731ec7f0 100644 --- a/apps/encryption/lib/Command/EnableMasterKey.php +++ b/apps/encryption/lib/Command/EnableMasterKey.php @@ -51,18 +51,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($isAlreadyEnabled) { $output->writeln('Master key already enabled'); - } else { - $question = new ConfirmationQuestion( - 'Warning: Only available for fresh installations with no existing encrypted data! ' + return self::SUCCESS; + } + + $question = new ConfirmationQuestion( + 'Warning: Only available for fresh installations with no existing encrypted data! ' . 'There is also no way to disable it again. Do you want to continue? (y/n) ', false); - if ($this->questionHelper->ask($input, $output, $question)) { - $this->config->setAppValue('encryption', 'useMasterKey', '1'); - $output->writeln('Master key successfully enabled.'); - } else { - $output->writeln('aborted.'); - return self::FAILURE; - } + + if ($this->questionHelper->ask($input, $output, $question)) { + $this->config->setAppValue('encryption', 'useMasterKey', '1'); + $output->writeln('Master key successfully enabled.'); + return self::SUCCESS; } - return self::SUCCESS; + + $output->writeln('aborted.'); + return self::FAILURE; } } diff --git a/apps/encryption/lib/Command/FixEncryptedVersion.php b/apps/encryption/lib/Command/FixEncryptedVersion.php index 2a032fb9c0752..8cdbef4e34641 100644 --- a/apps/encryption/lib/Command/FixEncryptedVersion.php +++ b/apps/encryption/lib/Command/FixEncryptedVersion.php @@ -93,6 +93,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $all = $input->getOption('all'); $pathOption = \trim(($input->getOption('path') ?? ''), '/'); + if (!$user && !$all) { + $output->writeln("Either a user id or --all needs to be provided"); + return self::FAILURE; + } + if ($user) { if ($all) { $output->writeln("Specifying a user id and --all are mutually exclusive"); @@ -105,18 +110,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int } return $this->runForUser($user, $pathOption, $output); - } elseif ($all) { - $result = 0; - $this->userManager->callForSeenUsers(function (IUser $user) use ($pathOption, $output, &$result) { - $output->writeln("Processing files for " . $user->getUID()); - $result = $this->runForUser($user->getUID(), $pathOption, $output); - return $result === 0; - }); - return $result; - } else { - $output->writeln("Either a user id or --all needs to be provided"); - return self::FAILURE; } + + $result = 0; + $this->userManager->callForSeenUsers(function (IUser $user) use ($pathOption, $output, &$result) { + $output->writeln("Processing files for " . $user->getUID()); + $result = $this->runForUser($user->getUID(), $pathOption, $output); + return $result === 0; + }); + return $result; } private function runForUser(string $user, string $pathOption, OutputInterface $output): int { diff --git a/apps/encryption/lib/Command/FixKeyLocation.php b/apps/encryption/lib/Command/FixKeyLocation.php index 51b9e688c0255..36cc6ce103191 100644 --- a/apps/encryption/lib/Command/FixKeyLocation.php +++ b/apps/encryption/lib/Command/FixKeyLocation.php @@ -87,19 +87,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int $files = $this->getAllFiles($mountRootFolder); foreach ($files as $file) { - if ($this->isKeyStoredForUser($user, $file)) { - if ($dryRun) { - $output->writeln("" . $file->getPath() . " needs migration"); - } else { - $output->write("Migrating key for " . $file->getPath() . " "); - if ($this->copyKeyAndValidate($user, $file)) { - $output->writeln(""); - } else { - $output->writeln("❌"); - $output->writeln(" Failed to validate key for " . $file->getPath() . ", key will not be migrated"); - } - } + if (!$this->isKeyStoredForUser($user, $file)) { + continue; } + + if ($dryRun) { + $output->writeln("" . $file->getPath() . " needs migration"); + continue; + } + + $output->write("Migrating key for " . $file->getPath() . " "); + if ($this->copyKeyAndValidate($user, $file)) { + $output->writeln(""); + continue; + } + + $output->writeln("❌"); + $output->writeln(" Failed to validate key for " . $file->getPath() . ", key will not be migrated"); } }