From bbbe9eef22e325c58d27e869569ad172fe019b94 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 3 May 2022 15:39:36 +0200 Subject: [PATCH] PreferencesCommand: Introduce method `loadIniFile()` and remove not required code --- .../clicommands/PreferencesCommand.php | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/modules/migrate/application/clicommands/PreferencesCommand.php b/modules/migrate/application/clicommands/PreferencesCommand.php index f31665493..11d1edb94 100644 --- a/modules/migrate/application/clicommands/PreferencesCommand.php +++ b/modules/migrate/application/clicommands/PreferencesCommand.php @@ -10,9 +10,9 @@ use Icinga\Data\ConfigObject; use Icinga\Data\ResourceFactory; use Icinga\Exception\NotReadableError; use Icinga\Exception\NotWritableError; +use Icinga\File\Ini\IniParser; use Icinga\User; -use Icinga\User\Preferences\Store\IniStore; -use Icinga\User\Preferences\Store\DbStore; +use Icinga\User\Preferences\PreferencesStore; use Icinga\Util\DirectoryIterator; class PreferencesCommand extends Command @@ -61,12 +61,15 @@ class PreferencesCommand extends Command Logger::info('Migrating INI preferences for user "%s" to database...', $userName); - $iniStore = new IniStore(new ConfigObject(['location' => $preferencesPath]), new User($userName)); - $dbStore = new DbStore(new ConfigObject(['connection' => $connection]), new User($userName)); + $dbStore = new PreferencesStore(new ConfigObject(['connection' => $connection]), new User($userName)); try { $dbStore->load(); - $dbStore->save(new User\Preferences($iniStore->load())); + $dbStore->save( + new User\Preferences( + $this->loadIniFile($preferencesPath, (new User($userName))->getUsername()) + ) + ); } catch (NotReadableError $e) { if ($e->getPrevious() !== null) { Logger::error('%s: %s', $e->getMessage(), $e->getPrevious()->getMessage()); @@ -89,7 +92,6 @@ class PreferencesCommand extends Command if ($this->params->has('resource') && ! $this->params->has('no-set-config-backend')) { $appConfig = Config::app(); $globalConfig = $appConfig->getSection('global'); - $globalConfig['config_backend'] = 'db'; $globalConfig['config_resource'] = $resource; try { @@ -102,4 +104,28 @@ class PreferencesCommand extends Command Logger::info('Successfully migrated all local user preferences to database'); } + + private function loadIniFile(string $filePath, string $username): array + { + $preferences = []; + $preferencesFile = sprintf( + '%s/%s/config.ini', + $filePath, + strtolower($username) + ); + + if (file_exists($preferencesFile)) { + if (! is_readable($preferencesFile)) { + throw new NotReadableError( + 'Preferences INI file %s for user %s is not readable', + $preferencesFile, + $username + ); + } else { + $preferences = IniParser::parseIniFile($preferencesFile)->toArray(); + } + } + + return $preferences; + } }