Skip to content

Commit

Permalink
PreferencesCommand: Introduce method loadIniFile() and remove not r…
Browse files Browse the repository at this point in the history
…equired code
  • Loading branch information
sukhwinder33445 authored and nilmerg committed May 27, 2022
1 parent 4ad1ee4 commit 53fc351
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions modules/migrate/application/clicommands/PreferencesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
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
Expand Down Expand Up @@ -61,12 +61,15 @@ public function indexAction()

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());
Expand All @@ -89,7 +92,6 @@ public function indexAction()
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 {
Expand All @@ -102,4 +104,28 @@ public function indexAction()

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;
}
}

0 comments on commit 53fc351

Please sign in to comment.