Skip to content

Commit 881123f

Browse files
Faraz Samapoorfsamapoor
authored andcommitted
Uses early returns.
To improve code readability. Signed-off-by: Faraz Samapoor <fsa@adlas.at>
1 parent 73b42a9 commit 881123f

File tree

4 files changed

+59
-49
lines changed

4 files changed

+59
-49
lines changed

apps/encryption/lib/Command/DisableMasterKey.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5252

5353
if (!$isMasterKeyEnabled) {
5454
$output->writeln('Master key already disabled');
55-
} else {
56-
$question = new ConfirmationQuestion(
57-
'Warning: Only perform this operation for a fresh installations with no existing encrypted data! '
58-
. 'There is no way to enable the master key again. '
59-
. 'We strongly recommend to keep the master key, it provides significant performance improvements '
60-
. 'and is easier to handle for both, users and administrators. '
61-
. 'Do you really want to switch to per-user keys? (y/n) ', false);
62-
if ($this->questionHelper->ask($input, $output, $question)) {
63-
$this->config->setAppValue('encryption', 'useMasterKey', '0');
64-
$output->writeln('Master key successfully disabled.');
65-
} else {
66-
$output->writeln('aborted.');
67-
return self::FAILURE;
68-
}
55+
return self::SUCCESS;
6956
}
70-
return self::SUCCESS;
57+
58+
$question = new ConfirmationQuestion(
59+
'Warning: Only perform this operation for a fresh installations with no existing encrypted data! '
60+
. 'There is no way to enable the master key again. '
61+
. 'We strongly recommend to keep the master key, it provides significant performance improvements '
62+
. 'and is easier to handle for both, users and administrators. '
63+
. 'Do you really want to switch to per-user keys? (y/n) ', false);
64+
65+
if ($this->questionHelper->ask($input, $output, $question)) {
66+
$this->config->setAppValue('encryption', 'useMasterKey', '0');
67+
$output->writeln('Master key successfully disabled.');
68+
return self::SUCCESS;
69+
}
70+
71+
$output->writeln('aborted.');
72+
return self::FAILURE;
7173
}
7274
}

apps/encryption/lib/Command/EnableMasterKey.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5151

5252
if ($isAlreadyEnabled) {
5353
$output->writeln('Master key already enabled');
54-
} else {
55-
$question = new ConfirmationQuestion(
56-
'Warning: Only available for fresh installations with no existing encrypted data! '
54+
return self::SUCCESS;
55+
}
56+
57+
$question = new ConfirmationQuestion(
58+
'Warning: Only available for fresh installations with no existing encrypted data! '
5759
. 'There is also no way to disable it again. Do you want to continue? (y/n) ', false);
58-
if ($this->questionHelper->ask($input, $output, $question)) {
59-
$this->config->setAppValue('encryption', 'useMasterKey', '1');
60-
$output->writeln('Master key successfully enabled.');
61-
} else {
62-
$output->writeln('aborted.');
63-
return self::FAILURE;
64-
}
60+
61+
if ($this->questionHelper->ask($input, $output, $question)) {
62+
$this->config->setAppValue('encryption', 'useMasterKey', '1');
63+
$output->writeln('Master key successfully enabled.');
64+
return self::SUCCESS;
6565
}
66-
return self::SUCCESS;
66+
67+
$output->writeln('aborted.');
68+
return self::FAILURE;
6769
}
6870
}

apps/encryption/lib/Command/FixEncryptedVersion.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9393
$all = $input->getOption('all');
9494
$pathOption = \trim(($input->getOption('path') ?? ''), '/');
9595

96+
if (!$user && !$all) {
97+
$output->writeln("Either a user id or --all needs to be provided");
98+
return self::FAILURE;
99+
}
100+
96101
if ($user) {
97102
if ($all) {
98103
$output->writeln("Specifying a user id and --all are mutually exclusive");
@@ -105,18 +110,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
105110
}
106111

107112
return $this->runForUser($user, $pathOption, $output);
108-
} elseif ($all) {
109-
$result = 0;
110-
$this->userManager->callForSeenUsers(function (IUser $user) use ($pathOption, $output, &$result) {
111-
$output->writeln("Processing files for " . $user->getUID());
112-
$result = $this->runForUser($user->getUID(), $pathOption, $output);
113-
return $result === 0;
114-
});
115-
return $result;
116-
} else {
117-
$output->writeln("Either a user id or --all needs to be provided");
118-
return self::FAILURE;
119113
}
114+
115+
$result = 0;
116+
$this->userManager->callForSeenUsers(function (IUser $user) use ($pathOption, $output, &$result) {
117+
$output->writeln("Processing files for " . $user->getUID());
118+
$result = $this->runForUser($user->getUID(), $pathOption, $output);
119+
return $result === 0;
120+
});
121+
return $result;
120122
}
121123

122124
private function runForUser(string $user, string $pathOption, OutputInterface $output): int {

apps/encryption/lib/Command/FixKeyLocation.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8787

8888
$files = $this->getAllFiles($mountRootFolder);
8989
foreach ($files as $file) {
90-
if ($this->isKeyStoredForUser($user, $file)) {
91-
if ($dryRun) {
92-
$output->writeln("<info>" . $file->getPath() . "</info> needs migration");
93-
} else {
94-
$output->write("Migrating key for <info>" . $file->getPath() . "</info> ");
95-
if ($this->copyKeyAndValidate($user, $file)) {
96-
$output->writeln("<info>✓</info>");
97-
} else {
98-
$output->writeln("<fg=red>❌</>");
99-
$output->writeln(" Failed to validate key for <error>" . $file->getPath() . "</error>, key will not be migrated");
100-
}
101-
}
90+
if (!$this->isKeyStoredForUser($user, $file)) {
91+
continue;
10292
}
93+
94+
if ($dryRun) {
95+
$output->writeln("<info>" . $file->getPath() . "</info> needs migration");
96+
continue;
97+
}
98+
99+
$output->write("Migrating key for <info>" . $file->getPath() . "</info> ");
100+
if ($this->copyKeyAndValidate($user, $file)) {
101+
$output->writeln("<info>✓</info>");
102+
continue;
103+
}
104+
105+
$output->writeln("<fg=red>❌</>");
106+
$output->writeln(" Failed to validate key for <error>" . $file->getPath() . "</error>, key will not be migrated");
103107
}
104108
}
105109

0 commit comments

Comments
 (0)