export server profiles

This commit is contained in:
Roland Gruber 2020-04-19 20:40:40 +02:00
parent 9936c834db
commit 24a6e14251
5 changed files with 180 additions and 8 deletions

View File

@ -649,6 +649,58 @@ class LAMConfig {
$this->reload(); $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 * Reloads preferences from config file
* *

View File

@ -1,6 +1,7 @@
<?php <?php
namespace LAM\PERSISTENCE; namespace LAM\PERSISTENCE;
use LAMCfgMain; use LAMCfgMain;
use LAMConfig;
use LAMException; use LAMException;
/* /*
@ -46,15 +47,16 @@ class ConfigDataExporter {
$jsonData = array(); $jsonData = array();
$jsonData['mainConfig'] = $this->_getMainConfigData($mainCfg); $jsonData['mainConfig'] = $this->_getMainConfigData($mainCfg);
$jsonData['certificates'] = $this->_getCertificates($mainCfg); $jsonData['certificates'] = $this->_getCertificates($mainCfg);
$jsonData['serverProfiles'] = $this->_getServerProfiles();
/** /**
* TODO * TODO
* *
* webauthn
* server profiles * server profiles
* server profile job database * server profile job database
* account profiles * account profiles
* PDF profiles * PDF profiles
* self service profiles * self service profiles
* webauthn
*/ */
return json_encode($jsonData); return json_encode($jsonData);
} }
@ -88,6 +90,21 @@ class ConfigDataExporter {
return $mainCfg->exportCertificates(); 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 * @throws LAMException if invalid format
*/ */
public function getPossibleImportSteps($json) { public function getPossibleImportSteps($json) {
$data = json_decode($json); $data = json_decode($json, true);
if ($data === null) { if ($data === null) {
throw new LAMException(_('Unable to read import file.')); throw new LAMException(_('Unable to read import file.'));
} }
@ -116,6 +133,13 @@ class ConfigDataImporter {
case 'certificates': case 'certificates':
$steps[] = new ImporterStep(_('SSL certificates'), 'certificates', $value); $steps[] = new ImporterStep(_('SSL certificates'), 'certificates', $value);
break; 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: default:
logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key); logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key);
} }
@ -185,6 +209,7 @@ class ImporterStep {
private $key; private $key;
private $value; private $value;
private $active = false; private $active = false;
private $subSteps = array();
/** /**
* Constructor. * Constructor.
@ -196,7 +221,7 @@ class ImporterStep {
public function __construct($label, $key, &$value) { public function __construct($label, $key, &$value) {
$this->label = $label; $this->label = $label;
$this->key = $key; $this->key = $key;
$this->value = $value; $this->value = &$value;
} }
/** /**
@ -244,4 +269,22 @@ class ImporterStep {
return $this->value; 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;
}
} }

View File

@ -221,7 +221,21 @@ printHeaderContents(_("Import and export configuration"), '../..');
elseif (isset($_POST['importConfig'])) { elseif (isset($_POST['importConfig'])) {
$content->add(new htmlOutputText(_('Import steps')), 12); $content->add(new htmlOutputText(_('Import steps')), 12);
foreach ($importSteps as $importStep) { 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 = new htmlGroup();
$buttonGroup->addElement(new htmlButton('importConfigConfirm', _('Import'))); $buttonGroup->addElement(new htmlButton('importConfigConfirm', _('Import')));

View File

@ -3,7 +3,7 @@ use PHPUnit\Framework\TestCase;
/* /*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) 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 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 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. * LAMConfig test case.
@ -858,6 +858,63 @@ class LAMConfigTest extends TestCase {
$this->assertEquals($sizeTypeSettings, sizeof($this->lAMConfig->get_typeSettings())); $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 * Saves the config
*/ */

View File

@ -37,16 +37,22 @@ class ConfigDataExporterTest extends TestCase {
'confMainKey2' => 4, 'confMainKey2' => 4,
'confMainKey3' => '', 'confMainKey3' => '',
); );
$profileData = array(
'profile1' => array('ServerURL' => 'myserver'),
'profile2' => array('ServerURL' => 'myserver2'),
);
$expectedJson = json_encode(array( $expectedJson = json_encode(array(
'mainConfig' => $mainData, 'mainConfig' => $mainData,
'certificates' => 'certs' 'certificates' => 'certs',
'serverProfiles' => $profileData,
)); ));
$exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter') $exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter')
->setMethods(array('_getMainConfigData', '_getCertificates')) ->setMethods(array('_getMainConfigData', '_getCertificates', '_getServerProfiles'))
->getMock(); ->getMock();
$exporter->method('_getMainConfigData')->willReturn($mainData); $exporter->method('_getMainConfigData')->willReturn($mainData);
$exporter->method('_getCertificates')->willReturn('certs'); $exporter->method('_getCertificates')->willReturn('certs');
$exporter->method('_getServerProfiles')->willReturn($profileData);
$json = $exporter->exportAsJson(); $json = $exporter->exportAsJson();