From 24a6e142516817de4b246bb639a413e0ca9e01aa Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sun, 19 Apr 2020 20:40:40 +0200 Subject: [PATCH] export server profiles --- lam/lib/config.inc | 52 +++++++++++++++++++ lam/lib/persistence.inc | 49 ++++++++++++++++-- lam/templates/config/confImportExport.php | 16 +++++- lam/tests/lib/LAMConfigTest.php | 61 ++++++++++++++++++++++- lam/tests/lib/persistenceTest.php | 10 +++- 5 files changed, 180 insertions(+), 8 deletions(-) diff --git a/lam/lib/config.inc b/lam/lib/config.inc index 06a6eca7..6419f1d0 100644 --- a/lam/lib/config.inc +++ b/lam/lib/config.inc @@ -649,6 +649,58 @@ class LAMConfig { $this->reload(); } + /** + * Returns the server profile data. + * + * @return array data + */ + public function exportData() { + $data = array(); + $settingsToIgnore = array('modules', 'types', 'tools', 'jobs'); + foreach ($this->settings as $setting) { + if (in_array($setting, $settingsToIgnore)) { + continue; + } + $data[$setting] = $this->$setting; + } + $data['typeSettings'] = $this->typeSettings; + $data['moduleSettings'] = $this->moduleSettings; + $data['toolSettings'] = $this->toolSettings; + $data['jobSettings'] = $this->jobSettings; + return $data; + } + + /** + * Imports server profile data. + * + * @param array $data config data + * @throws LAMException import error + */ + public function importData($data) { + $settingsToIgnore = array('modules', 'types', 'tools', 'jobs'); + foreach ($data as $dataKey => $dataValue) { + if (in_array($dataKey, $settingsToIgnore)) { + continue; + } + if (!in_array($dataKey, $this->settings)) { + logNewMessage(LOG_WARNING, 'Ignored setting during import: ' . $dataKey); + continue; + } + if (!(($dataValue === null) || is_array($dataValue) || is_string($dataValue) || is_int($dataValue) || is_bool($dataValue))) { + throw new LAMException('Invalid import data type for ' . htmlspecialchars($dataKey) . ': ' . gettype($dataValue)); + } + $this->$dataKey = $dataValue; + } + $typeSettingsData = !empty($data['typeSettings']) && is_array($data['typeSettings']) ? $data['typeSettings'] : array(); + $this->typeSettings = $typeSettingsData; + $moduleSettingsData = !empty($data['moduleSettings']) && is_array($data['moduleSettings']) ? $data['moduleSettings'] : array(); + $this->moduleSettings = $moduleSettingsData; + $toolSettingsData = !empty($data['toolSettings']) && is_array($data['toolSettings']) ? $data['toolSettings'] : array(); + $this->toolSettings = $toolSettingsData; + $jobSettingsData = !empty($data['jobSettings']) && is_array($data['jobSettings']) ? $data['jobSettings'] : array(); + $this->jobSettings = $jobSettingsData; + } + /** * Reloads preferences from config file * diff --git a/lam/lib/persistence.inc b/lam/lib/persistence.inc index 46401611..498b0d2c 100644 --- a/lam/lib/persistence.inc +++ b/lam/lib/persistence.inc @@ -1,6 +1,7 @@ _getMainConfigData($mainCfg); $jsonData['certificates'] = $this->_getCertificates($mainCfg); + $jsonData['serverProfiles'] = $this->_getServerProfiles(); /** * TODO * - * webauthn * server profiles * server profile job database * account profiles * PDF profiles * self service profiles + * webauthn */ return json_encode($jsonData); } @@ -88,6 +90,21 @@ class ConfigDataExporter { return $mainCfg->exportCertificates(); } + /** + * Returns the content of the server profiles. + * + * @return array $data + */ + public function _getServerProfiles() { + $data = array(); + $profileNames = getConfigProfiles(); + foreach ($profileNames as $profileName) { + $serverProfile = new LAMConfig($profileName); + $data[$profileName] = $serverProfile->exportData(); + } + return $data; + } + } /** @@ -103,7 +120,7 @@ class ConfigDataImporter { * @throws LAMException if invalid format */ public function getPossibleImportSteps($json) { - $data = json_decode($json); + $data = json_decode($json, true); if ($data === null) { throw new LAMException(_('Unable to read import file.')); } @@ -116,6 +133,13 @@ class ConfigDataImporter { case 'certificates': $steps[] = new ImporterStep(_('SSL certificates'), 'certificates', $value); break; + case 'serverProfiles': + $mainStep = new ImporterStep(_('Server profiles'), 'serverProfiles', $value); + foreach ($value as $profileName => $profileData) { + $mainStep->addSubStep(new ImporterStep($profileName, 'serverProfile_' . $profileName, $profileData)); + } + $steps[] = $mainStep; + break; default: logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key); } @@ -185,6 +209,7 @@ class ImporterStep { private $key; private $value; private $active = false; + private $subSteps = array(); /** * Constructor. @@ -196,7 +221,7 @@ class ImporterStep { public function __construct($label, $key, &$value) { $this->label = $label; $this->key = $key; - $this->value = $value; + $this->value = &$value; } /** @@ -244,4 +269,22 @@ class ImporterStep { return $this->value; } + /** + * Adds a sub-step. + * + * @param ImporterStep $subStep sub-step + */ + public function addSubStep($subStep) { + $this->subSteps[] = $subStep; + } + + /** + * Returns the sub-steps. + * + * @return ImporterStep[] sub-steps + */ + public function getSubSteps() { + return $this->subSteps; + } + } diff --git a/lam/templates/config/confImportExport.php b/lam/templates/config/confImportExport.php index 7c54e8b4..e6548749 100644 --- a/lam/templates/config/confImportExport.php +++ b/lam/templates/config/confImportExport.php @@ -221,7 +221,21 @@ printHeaderContents(_("Import and export configuration"), '../..'); elseif (isset($_POST['importConfig'])) { $content->add(new htmlOutputText(_('Import steps')), 12); foreach ($importSteps as $importStep) { - $content->add(new htmlResponsiveInputCheckbox('step_' . $importStep->getKey(), true, $importStep->getLabel()), 12); + $stepKey = 'step_' . $importStep->getKey(); + $stepCheckbox = new htmlResponsiveInputCheckbox($stepKey, true, $importStep->getLabel()); + $stepCheckbox->setLabelAfterCheckbox(); + $subStepIds = array(); + $content->add($stepCheckbox, 12); + $content->addVerticalSpacer('0.3rem'); + foreach ($importStep->getSubSteps() as $subStep) { + $subStepKey = 'step_' . $subStep->getKey(); + $subStepIds[] = $subStepKey; + $subStepCheckbox = new htmlResponsiveInputCheckbox($subStepKey, true, ' - ' . $subStep->getLabel()); + $subStepCheckbox->setLabelAfterCheckbox(); + $content->add($subStepCheckbox, 12); + } + $stepCheckbox->setTableRowsToShow($subStepIds); + $content->addVerticalSpacer('1rem'); } $buttonGroup = new htmlGroup(); $buttonGroup->addElement(new htmlButton('importConfigConfirm', _('Import'))); diff --git a/lam/tests/lib/LAMConfigTest.php b/lam/tests/lib/LAMConfigTest.php index 972c5403..b6f9212c 100644 --- a/lam/tests/lib/LAMConfigTest.php +++ b/lam/tests/lib/LAMConfigTest.php @@ -3,7 +3,7 @@ use PHPUnit\Framework\TestCase; /* This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) - Copyright (C) 2016 - 2019 Roland Gruber + Copyright (C) 2016 - 2020 Roland Gruber This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,7 +21,7 @@ use PHPUnit\Framework\TestCase; */ -include_once 'lam/tests/utils/configuration.inc'; +include_once __DIR__ . '/../utils/configuration.inc'; /** * LAMConfig test case. @@ -858,6 +858,63 @@ class LAMConfigTest extends TestCase { $this->assertEquals($sizeTypeSettings, sizeof($this->lAMConfig->get_typeSettings())); } + /** + * Tests the export. + */ + public function testExportData() { + $this->lAMConfig->set_defaultLanguage('lang'); + $this->lAMConfig->set_ServerURL('myserver'); + $this->lAMConfig->set_typeSettings(array('typetest' => '1')); + $this->lAMConfig->set_moduleSettings(array('modtest' => '1')); + $this->lAMConfig->setToolSettings(array('tooltest' => '1')); + $this->lAMConfig->setJobSettings(array('jobtest' => '1')); + + $data = $this->lAMConfig->exportData(); + + $this->assertEquals('lang', $data['defaultLanguage']); + $this->assertEquals('myserver', $data['ServerURL']); + $this->assertEquals(array('typetest' => '1'), $data['typeSettings']); + $this->assertEquals(array('modtest' => '1'), $data['moduleSettings']); + $this->assertEquals(array('tooltest' => '1'), $data['toolSettings']); + $this->assertEquals(array('jobtest' => '1'), $data['jobSettings']); + } + + /** + * Tests the import. + */ + public function testImportData() { + $importData = array(); + $importData['ServerURL'] = 'testserver'; + $importData['defaultLanguage'] = 'de_DE.utf8'; + $importData['typeSettings'] = array('typetest' => 'value'); + $importData['toolSettings'] = array('tooltest' => 'value'); + $importData['moduleSettings'] = array('modtest' => 'value'); + $importData['jobSettings'] = array('jobtest' => 'value'); + $importData['IGNORE_ME'] = 'ignore'; + + $this->lAMConfig->importData($importData); + + $this->assertEquals('testserver', $this->lAMConfig->get_ServerURL()); + $this->assertEquals('de_DE.utf8', $this->lAMConfig->get_defaultLanguage()); + $this->assertEquals(array('typetest' => 'value'), $this->lAMConfig->get_typeSettings()); + $this->assertEquals(array('tooltest' => 'value'), $this->lAMConfig->getToolSettings()); + $this->assertEquals(array('modtest' => 'value'), $this->lAMConfig->get_moduleSettings()); + $this->assertEquals(array('jobtest' => 'value'), $this->lAMConfig->getJobSettings()); + } + + /** + * Tests the import with invalid data. + */ + public function testImportData_invalid() { + $importData = array(); + $importData['ServerURL'] = 'testserver'; + $importData['typeSettings'] = array('typetest' => 'value'); + $importData['defaultLanguage'] = new LAMLanguage('de_de', 'UTF-8', 'DE'); + + $this->expectException(LAMException::class); + $this->lAMConfig->importData($importData); + } + /** * Saves the config */ diff --git a/lam/tests/lib/persistenceTest.php b/lam/tests/lib/persistenceTest.php index 622b19b2..a39b81ba 100644 --- a/lam/tests/lib/persistenceTest.php +++ b/lam/tests/lib/persistenceTest.php @@ -37,16 +37,22 @@ class ConfigDataExporterTest extends TestCase { 'confMainKey2' => 4, 'confMainKey3' => '', ); + $profileData = array( + 'profile1' => array('ServerURL' => 'myserver'), + 'profile2' => array('ServerURL' => 'myserver2'), + ); $expectedJson = json_encode(array( 'mainConfig' => $mainData, - 'certificates' => 'certs' + 'certificates' => 'certs', + 'serverProfiles' => $profileData, )); $exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter') - ->setMethods(array('_getMainConfigData', '_getCertificates')) + ->setMethods(array('_getMainConfigData', '_getCertificates', '_getServerProfiles')) ->getMock(); $exporter->method('_getMainConfigData')->willReturn($mainData); $exporter->method('_getCertificates')->willReturn('certs'); + $exporter->method('_getServerProfiles')->willReturn($profileData); $json = $exporter->exportAsJson();