Skip to content

Commit c755897

Browse files
committed
refactor: fix psalm issues from encryption commands
Signed-off-by: Carl Schwan <carl.schwan@nextcloud.com>
1 parent d36fc65 commit c755897

File tree

11 files changed

+164
-234
lines changed

11 files changed

+164
-234
lines changed

apps/encryption/lib/Command/DisableMasterKey.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace OCA\Encryption\Command;
88

99
use OCA\Encryption\Util;
10-
use OCP\IConfig;
10+
use OCP\AppFramework\Services\IAppConfig;
1111
use Symfony\Component\Console\Command\Command;
1212
use Symfony\Component\Console\Helper\QuestionHelper;
1313
use Symfony\Component\Console\Input\InputInterface;
@@ -17,7 +17,7 @@
1717
class DisableMasterKey extends Command {
1818
public function __construct(
1919
protected Util $util,
20-
protected IConfig $config,
20+
protected IAppConfig $config,
2121
protected QuestionHelper $questionHelper,
2222
) {
2323
parent::__construct();
@@ -45,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4545
. 'Do you really want to switch to per-user keys? (y/n) ', false);
4646

4747
if ($this->questionHelper->ask($input, $output, $question)) {
48-
$this->config->setAppValue('encryption', 'useMasterKey', '0');
48+
$this->config->setAppValueBool('useMasterKey', false);
4949
$output->writeln('Master key successfully disabled.');
5050
return self::SUCCESS;
5151
}

apps/encryption/lib/Command/DropLegacyFileKey.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
use OC\Encryption\Exceptions\DecryptionFailedException;
1313
use OC\Files\FileInfo;
14+
use OC\Files\SetupManager;
1415
use OC\Files\View;
1516
use OCA\Encryption\KeyManager;
1617
use OCP\Encryption\Exceptions\GenericEncryptionException;
18+
use OCP\IUser;
1719
use OCP\IUserManager;
1820
use Symfony\Component\Console\Command\Command;
1921
use Symfony\Component\Console\Input\InputInterface;
@@ -23,8 +25,9 @@ class DropLegacyFileKey extends Command {
2325
private View $rootView;
2426

2527
public function __construct(
26-
private IUserManager $userManager,
27-
private KeyManager $keyManager,
28+
private readonly IUserManager $userManager,
29+
private readonly KeyManager $keyManager,
30+
private readonly SetupManager $setupManager,
2831
) {
2932
parent::__construct();
3033

@@ -42,18 +45,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4245

4346
$output->writeln('<info>Scanning all files for legacy filekey</info>');
4447

45-
foreach ($this->userManager->getBackends() as $backend) {
46-
$limit = 500;
47-
$offset = 0;
48-
do {
49-
$users = $backend->getUsers('', $limit, $offset);
50-
foreach ($users as $user) {
51-
$output->writeln('Scanning all files for ' . $user);
52-
$this->setupUserFS($user);
53-
$result = $result && $this->scanFolder($output, '/' . $user);
54-
}
55-
$offset += $limit;
56-
} while (count($users) >= $limit);
48+
foreach ($this->userManager->getSeenUsers() as $user) {
49+
$output->writeln('Scanning all files for ' . $user->getUID());
50+
$this->setupUserFileSystem($user);
51+
$result = $result && $this->scanFolder($output, '/' . $user->getUID());
5752
}
5853

5954
if ($result) {
@@ -143,8 +138,8 @@ private function migrateSinglefile(string $path, FileInfo $fileInfo, OutputInter
143138
/**
144139
* setup user file system
145140
*/
146-
protected function setupUserFS(string $uid): void {
147-
\OC_Util::tearDownFS();
148-
\OC_Util::setupFS($uid);
141+
protected function setupUserFileSystem(IUser $user): void {
142+
$this->setupManager->tearDown();
143+
$this->setupManager->setupForUser($user);
149144
}
150145
}

apps/encryption/lib/Command/EnableMasterKey.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace OCA\Encryption\Command;
99

1010
use OCA\Encryption\Util;
11-
use OCP\IConfig;
11+
use OCP\AppFramework\Services\IAppConfig;
1212
use Symfony\Component\Console\Command\Command;
1313
use Symfony\Component\Console\Helper\QuestionHelper;
1414
use Symfony\Component\Console\Input\InputInterface;
@@ -18,7 +18,7 @@
1818
class EnableMasterKey extends Command {
1919
public function __construct(
2020
protected Util $util,
21-
protected IConfig $config,
21+
protected IAppConfig $config,
2222
protected QuestionHelper $questionHelper,
2323
) {
2424
parent::__construct();
@@ -43,7 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4343
. 'There is also no way to disable it again. Do you want to continue? (y/n) ', false);
4444

4545
if ($this->questionHelper->ask($input, $output, $question)) {
46-
$this->config->setAppValue('encryption', 'useMasterKey', '1');
46+
$this->config->setAppValueBool('useMasterKey', true);
4747
$output->writeln('Master key successfully enabled.');
4848
return self::SUCCESS;
4949
}

apps/encryption/lib/Command/FixEncryptedVersion.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
namespace OCA\Encryption\Command;
1010

11+
use OC\Files\SetupManager;
1112
use OC\Files\Storage\Wrapper\Encryption;
1213
use OC\Files\View;
1314
use OC\ServerNotAvailableException;
1415
use OCA\Encryption\Util;
1516
use OCP\Encryption\Exceptions\InvalidHeaderException;
16-
use OCP\Files\IRootFolder;
1717
use OCP\HintException;
1818
use OCP\IConfig;
1919
use OCP\IUser;
@@ -29,12 +29,12 @@ class FixEncryptedVersion extends Command {
2929
private bool $supportLegacy = false;
3030

3131
public function __construct(
32-
private IConfig $config,
33-
private LoggerInterface $logger,
34-
private IRootFolder $rootFolder,
35-
private IUserManager $userManager,
36-
private Util $util,
37-
private View $view,
32+
private readonly IConfig $config,
33+
private readonly LoggerInterface $logger,
34+
private readonly IUserManager $userManager,
35+
private readonly Util $util,
36+
private readonly View $view,
37+
private readonly SetupManager $setupManager,
3838
) {
3939
parent::__construct();
4040
}
@@ -91,45 +91,43 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9191
return self::FAILURE;
9292
}
9393

94-
if ($this->userManager->get($user) === null) {
94+
$user = $this->userManager->get($user);
95+
if ($user === null) {
9596
$output->writeln("<error>User id $user does not exist. Please provide a valid user id</error>");
9697
return self::FAILURE;
9798
}
9899

99-
return $this->runForUser($user, $pathOption, $output);
100+
return $this->runForUser($user, $pathOption, $output) ? self::SUCCESS : self::FAILURE;
100101
}
101102

102-
$result = 0;
103-
$this->userManager->callForSeenUsers(function (IUser $user) use ($pathOption, $output, &$result) {
103+
foreach ($this->userManager->getSeenUsers() as $user) {
104104
$output->writeln('Processing files for ' . $user->getUID());
105-
$result = $this->runForUser($user->getUID(), $pathOption, $output);
106-
return $result === 0;
107-
});
108-
return $result;
105+
if (!$this->runForUser($user, $pathOption, $output)) {
106+
return self::FAILURE;
107+
}
108+
}
109+
return self::SUCCESS;
109110
}
110111

111-
private function runForUser(string $user, string $pathOption, OutputInterface $output): int {
112-
$pathToWalk = "/$user/files";
112+
private function runForUser(IUser $user, string $pathOption, OutputInterface $output): bool {
113+
$pathToWalk = '/' . $user->getUID() . '/files';
113114
if ($pathOption !== '') {
114115
$pathToWalk = "$pathToWalk/$pathOption";
115116
}
116117
return $this->walkPathOfUser($user, $pathToWalk, $output);
117118
}
118119

119-
/**
120-
* @return int 0 for success, 1 for error
121-
*/
122-
private function walkPathOfUser(string $user, string $path, OutputInterface $output): int {
123-
$this->setupUserFs($user);
120+
private function walkPathOfUser(IUser $user, string $path, OutputInterface $output): bool {
121+
$this->setupUserFileSystem($user);
124122
if (!$this->view->file_exists($path)) {
125123
$output->writeln("<error>Path \"$path\" does not exist. Please provide a valid path.</error>");
126-
return self::FAILURE;
124+
return false;
127125
}
128126

129127
if ($this->view->is_file($path)) {
130128
$output->writeln("Verifying the content of file \"$path\"");
131129
$this->verifyFileContent($path, $output);
132-
return self::SUCCESS;
130+
return true;
133131
}
134132
$directories = [];
135133
$directories[] = $path;
@@ -145,7 +143,7 @@ private function walkPathOfUser(string $user, string $path, OutputInterface $out
145143
}
146144
}
147145
}
148-
return self::SUCCESS;
146+
return true;
149147
}
150148

151149
/**
@@ -309,8 +307,8 @@ private function correctEncryptedVersion(string $path, OutputInterface $output,
309307
/**
310308
* Setup user file system
311309
*/
312-
private function setupUserFs(string $uid): void {
313-
\OC_Util::tearDownFS();
314-
\OC_Util::setupFS($uid);
310+
private function setupUserFileSystem(IUser $user): void {
311+
$this->setupManager->tearDown();
312+
$this->setupManager->setupForUser($user);
315313
}
316314
}

apps/encryption/lib/Command/FixKeyLocation.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OC\Encryption\Manager;
1212
use OC\Encryption\Util;
13+
use OC\Files\SetupManager;
1314
use OC\Files\Storage\Wrapper\Encryption;
1415
use OC\Files\View;
1516
use OCP\Encryption\IManager;
@@ -33,10 +34,11 @@ class FixKeyLocation extends Command {
3334
private Manager $encryptionManager;
3435

3536
public function __construct(
36-
private IUserManager $userManager,
37-
private IUserMountCache $userMountCache,
38-
private Util $encryptionUtil,
39-
private IRootFolder $rootFolder,
37+
private readonly IUserManager $userManager,
38+
private readonly IUserMountCache $userMountCache,
39+
private readonly Util $encryptionUtil,
40+
private readonly IRootFolder $rootFolder,
41+
private readonly SetupManager $setupManager,
4042
IManager $encryptionManager,
4143
) {
4244
$this->keyRootDirectory = rtrim($this->encryptionUtil->getKeyStorageRoot(), '/');
@@ -69,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6971
return self::FAILURE;
7072
}
7173

72-
\OC_Util::setupFS($user->getUID());
74+
$this->setupManager->setupForUser($user);
7375

7476
$mounts = $this->getSystemMountsForUser($user);
7577
foreach ($mounts as $mount) {
@@ -179,11 +181,12 @@ private function getSystemMountsForUser(IUser $user): array {
179181
*
180182
* @return \Generator<File>
181183
*/
182-
private function getAllEncryptedFiles(Folder $folder) {
184+
private function getAllEncryptedFiles(Folder $folder): \Generator {
183185
foreach ($folder->getDirectoryListing() as $child) {
184186
if ($child instanceof Folder) {
185187
yield from $this->getAllEncryptedFiles($child);
186188
} else {
189+
/** @var File $child */
187190
if (substr($child->getName(), -4) !== '.bak' && $child->isEncrypted()) {
188191
yield $child;
189192
}

apps/encryption/lib/Command/ScanLegacyFormat.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
*/
99
namespace OCA\Encryption\Command;
1010

11+
use OC\Files\SetupManager;
1112
use OC\Files\View;
1213
use OCA\Encryption\Util;
1314
use OCP\IConfig;
15+
use OCP\IUser;
1416
use OCP\IUserManager;
1517
use Symfony\Component\Console\Command\Command;
1618
use Symfony\Component\Console\Helper\QuestionHelper;
@@ -21,10 +23,11 @@ class ScanLegacyFormat extends Command {
2123
private View $rootView;
2224

2325
public function __construct(
24-
protected Util $util,
25-
protected IConfig $config,
26-
protected QuestionHelper $questionHelper,
27-
private IUserManager $userManager,
26+
protected readonly Util $util,
27+
protected readonly IConfig $config,
28+
protected readonly QuestionHelper $questionHelper,
29+
private readonly IUserManager $userManager,
30+
private readonly SetupManager $setupManager,
2831
) {
2932
parent::__construct();
3033

@@ -42,18 +45,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4245

4346
$output->writeln('Scanning all files for legacy encryption');
4447

45-
foreach ($this->userManager->getBackends() as $backend) {
46-
$limit = 500;
47-
$offset = 0;
48-
do {
49-
$users = $backend->getUsers('', $limit, $offset);
50-
foreach ($users as $user) {
51-
$output->writeln('Scanning all files for ' . $user);
52-
$this->setupUserFS($user);
53-
$result = $result && $this->scanFolder($output, '/' . $user);
54-
}
55-
$offset += $limit;
56-
} while (count($users) >= $limit);
48+
foreach ($this->userManager->getSeenUsers() as $user) {
49+
$output->writeln('Scanning all files for ' . $user->getUID());
50+
$this->setupUserFileSystem($user);
51+
$result = $result && $this->scanFolder($output, '/' . $user->getUID());
5752
}
5853

5954
if ($result) {
@@ -93,8 +88,8 @@ private function scanFolder(OutputInterface $output, string $folder): bool {
9388
/**
9489
* setup user file system
9590
*/
96-
protected function setupUserFS(string $uid): void {
97-
\OC_Util::tearDownFS();
98-
\OC_Util::setupFS($uid);
91+
protected function setupUserFileSystem(IUser $user): void {
92+
$this->setupManager->tearDown();
93+
$this->setupManager->setupForUser($user);
9994
}
10095
}

0 commit comments

Comments
 (0)