mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-04-08 17:15:08 +02:00
PreferencesCommand: Introduce method loadIniFile()
and remove not required code
This commit is contained in:
parent
b3998856af
commit
bbbe9eef22
@ -10,9 +10,9 @@ use Icinga\Data\ConfigObject;
|
|||||||
use Icinga\Data\ResourceFactory;
|
use Icinga\Data\ResourceFactory;
|
||||||
use Icinga\Exception\NotReadableError;
|
use Icinga\Exception\NotReadableError;
|
||||||
use Icinga\Exception\NotWritableError;
|
use Icinga\Exception\NotWritableError;
|
||||||
|
use Icinga\File\Ini\IniParser;
|
||||||
use Icinga\User;
|
use Icinga\User;
|
||||||
use Icinga\User\Preferences\Store\IniStore;
|
use Icinga\User\Preferences\PreferencesStore;
|
||||||
use Icinga\User\Preferences\Store\DbStore;
|
|
||||||
use Icinga\Util\DirectoryIterator;
|
use Icinga\Util\DirectoryIterator;
|
||||||
|
|
||||||
class PreferencesCommand extends Command
|
class PreferencesCommand extends Command
|
||||||
@ -61,12 +61,15 @@ class PreferencesCommand extends Command
|
|||||||
|
|
||||||
Logger::info('Migrating INI preferences for user "%s" to database...', $userName);
|
Logger::info('Migrating INI preferences for user "%s" to database...', $userName);
|
||||||
|
|
||||||
$iniStore = new IniStore(new ConfigObject(['location' => $preferencesPath]), new User($userName));
|
$dbStore = new PreferencesStore(new ConfigObject(['connection' => $connection]), new User($userName));
|
||||||
$dbStore = new DbStore(new ConfigObject(['connection' => $connection]), new User($userName));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$dbStore->load();
|
$dbStore->load();
|
||||||
$dbStore->save(new User\Preferences($iniStore->load()));
|
$dbStore->save(
|
||||||
|
new User\Preferences(
|
||||||
|
$this->loadIniFile($preferencesPath, (new User($userName))->getUsername())
|
||||||
|
)
|
||||||
|
);
|
||||||
} catch (NotReadableError $e) {
|
} catch (NotReadableError $e) {
|
||||||
if ($e->getPrevious() !== null) {
|
if ($e->getPrevious() !== null) {
|
||||||
Logger::error('%s: %s', $e->getMessage(), $e->getPrevious()->getMessage());
|
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')) {
|
if ($this->params->has('resource') && ! $this->params->has('no-set-config-backend')) {
|
||||||
$appConfig = Config::app();
|
$appConfig = Config::app();
|
||||||
$globalConfig = $appConfig->getSection('global');
|
$globalConfig = $appConfig->getSection('global');
|
||||||
$globalConfig['config_backend'] = 'db';
|
|
||||||
$globalConfig['config_resource'] = $resource;
|
$globalConfig['config_resource'] = $resource;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -102,4 +104,28 @@ class PreferencesCommand extends Command
|
|||||||
|
|
||||||
Logger::info('Successfully migrated all local user preferences to database');
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user