diff options
author | Louis Chemineau <louis@chmn.me> | 2023-10-04 18:12:35 +0200 |
---|---|---|
committer | Louis Chemineau <louis@chmn.me> | 2023-10-05 13:31:33 +0200 |
commit | 4427fef384e7e4131faa031a245a998b4101eb2c (patch) | |
tree | d105adee6091caa3f74aa608f0466b225a3b6795 | |
parent | 30c7c47ce0d5627fd654d1a854a393a9f6af530e (diff) | |
download | nextcloud-server-oc-wnd-migrate.tar.gz nextcloud-server-oc-wnd-migrate.zip |
Add logic to migrate users' credentialsoc-wnd-migrate
Signed-off-by: Louis Chemineau <louis@chmn.me>
-rw-r--r-- | apps/files_external/lib/Command/MigrateOc.php | 66 |
1 files changed, 54 insertions, 12 deletions
diff --git a/apps/files_external/lib/Command/MigrateOc.php b/apps/files_external/lib/Command/MigrateOc.php index ec35523ca9f..5b9a3cc1298 100644 --- a/apps/files_external/lib/Command/MigrateOc.php +++ b/apps/files_external/lib/Command/MigrateOc.php @@ -104,17 +104,8 @@ class MigrateOc extends Base { $output->writeln("Successfully migrated"); } - $passwords = $this->getV2StoragePasswords(); - - if (count($passwords)) { - $output->writeln("Found <info>" . count($passwords) . "</info> stored passwords that need re-encoding"); - foreach ($passwords as $id => $password) { - $decoded = $this->decodePassword($password); - if (!$dryRun) { - $this->setStorageConfig($id, $this->encryptPassword($decoded)); - } - } - } + $this->migrateV2StoragePasswords($dryRun, $output); + $this->migrateUserCredentials($dryRun, $output); return 0; } @@ -167,7 +158,21 @@ class MigrateOc extends Base { return $configs; } - private function setStorageConfig(int $id, string $value) { + private function migrateV2StoragePasswords(bool $dryRun, OutputInterface $output): void { + $passwords = $this->getV2StoragePasswords(); + + if (count($passwords)) { + $output->writeln("Found <info>" . count($passwords) . "</info> stored passwords that need re-encoding"); + foreach ($passwords as $id => $password) { + $decoded = $this->decodePassword($password); + if (!$dryRun) { + $this->setStorageConfig($id, $this->encryptPassword($decoded)); + } + } + } + } + + private function setStorageConfig(int $id, string $value): void { $query = $this->connection->getQueryBuilder(); $query->update('external_config') ->set('value', $query->createNamedParameter($value)) @@ -175,6 +180,43 @@ class MigrateOc extends Base { $query->executeStatement(); } + /** + * @return array<array<string, string>> + */ + private function getUserCredentials(): array { + $query = $this->connection->getQueryBuilder(); + $query->select('user', 'identifier', 'credentials') + ->from('credentials'); + + return $query->executeQuery()->fetchAll(); + } + + private function migrateUserCredentials(bool $dryRun, OutputInterface $output): void { + $passwords = $this->getUserCredentials(); + + if (count($passwords)) { + $output->writeln("Found <info>" . count($passwords) . "</info> stored user credentials that need re-encoding"); + foreach ($passwords as $passwordRow) { + $decoded = $this->decodePassword($passwordRow["credentials"]); + if (!$dryRun) { + $this->setStorageCredentials($passwordRow, $this->encryptPassword($decoded)); + } + } + } + } + + private function setStorageCredentials(array $row, string $encryptedPassword): void { + $query = $this->connection->getQueryBuilder(); + + $query->insert('storages_credentials') + ->values([ + 'user' => $query->createNamedParameter($row['user']), + 'identifier' => $query->createNamedParameter($row['identifier']), + 'credentials' => $query->createNamedParameter($encryptedPassword), + ]) + ->executeStatement(); + } + private function setStorageId(string $old, string $new): bool { $query = $this->connection->getQueryBuilder(); $query->update('storages') |