added certificates to export
This commit is contained in:
		
							parent
							
								
									00c5a014b4
								
							
						
					
					
						commit
						e8d421ae04
					
				| 
						 | 
				
			
			@ -2698,6 +2698,7 @@ class LAMCfgMain {
 | 
			
		|||
	 * Imports configuration data.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param array $data config data
 | 
			
		||||
	 * @throws LAMException import error
 | 
			
		||||
	 */
 | 
			
		||||
	public function importData($data) {
 | 
			
		||||
		foreach ($data as $dataKey => $dataValue) {
 | 
			
		||||
| 
						 | 
				
			
			@ -2712,6 +2713,51 @@ class LAMCfgMain {
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Returns the content of the server certificates file
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return null|string certificates
 | 
			
		||||
	 */
 | 
			
		||||
	public function exportCertificates() {
 | 
			
		||||
		$fileName = $this->getSSLCaCertPath();
 | 
			
		||||
		if ($fileName === null) {
 | 
			
		||||
			return null;
 | 
			
		||||
		}
 | 
			
		||||
		$content = null;
 | 
			
		||||
		$handle = @fopen($fileName, "r");
 | 
			
		||||
		if ($handle) {
 | 
			
		||||
			$content = fread($handle, 10000000);
 | 
			
		||||
			fclose($handle);
 | 
			
		||||
		}
 | 
			
		||||
		return $content;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Imports the server certificates.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param null|string $certsContent certificates
 | 
			
		||||
	 * @throws LAMException write to file failed
 | 
			
		||||
	 */
 | 
			
		||||
	public function importCertificates($certsContent) {
 | 
			
		||||
		$fileName = $this->getSSLCaCertPath();
 | 
			
		||||
		if (empty($certsContent)) {
 | 
			
		||||
			if ($fileName !== null) {
 | 
			
		||||
				unlink($fileName);
 | 
			
		||||
			}
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
		$fileName = $this->getInternalSSLCaCertFileName();
 | 
			
		||||
		$handle = @fopen($fileName, "wb");
 | 
			
		||||
		if ($handle) {
 | 
			
		||||
			fputs($handle, $certsContent);
 | 
			
		||||
			fclose($handle);
 | 
			
		||||
			@chmod($fileName, 0600);
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			throw new LAMException(printf(_('Unable to write file %s.'), $fileName));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	* Reloads preferences from config file config.cfg
 | 
			
		||||
	*
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -42,21 +42,52 @@ class ConfigDataExporter {
 | 
			
		|||
	 * Exports LAM's configuration data in JSON format.
 | 
			
		||||
	 */
 | 
			
		||||
	public function exportAsJson() {
 | 
			
		||||
		$mainCfg = $this->_getMainConfiguration();
 | 
			
		||||
		$jsonData = array();
 | 
			
		||||
		$jsonData['mainConfig'] = $this->_getMainConfigData();
 | 
			
		||||
		$jsonData['mainConfig'] = $this->_getMainConfigData($mainCfg);
 | 
			
		||||
		$jsonData['certificates'] = $this->_getCertificates($mainCfg);
 | 
			
		||||
		/**
 | 
			
		||||
		 * TODO
 | 
			
		||||
		 *
 | 
			
		||||
		 * webauthn
 | 
			
		||||
		 * server profiles
 | 
			
		||||
		 * server profile job database
 | 
			
		||||
		 * account profiles
 | 
			
		||||
		 * PDF profiles
 | 
			
		||||
		 * self service profiles
 | 
			
		||||
		 */
 | 
			
		||||
		return json_encode($jsonData);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Returns the main configuration.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return LAMCfgMain main config
 | 
			
		||||
	 */
 | 
			
		||||
	public function _getMainConfiguration() {
 | 
			
		||||
		return new LAMCfgMain();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Internal function to read master configuration.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param LAMCfgMain $mainCfg main config
 | 
			
		||||
	 * @return array data
 | 
			
		||||
	 */
 | 
			
		||||
	public function _getMainConfigData() {
 | 
			
		||||
		$mainCfg = new LAMCfgMain();
 | 
			
		||||
	public function _getMainConfigData($mainCfg) {
 | 
			
		||||
		return $mainCfg->exportData();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Returns the certificate file content.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param LAMCfgMain $mainCfg main config
 | 
			
		||||
	 * @return array data
 | 
			
		||||
	 */
 | 
			
		||||
	public function _getCertificates($mainCfg) {
 | 
			
		||||
		return $mainCfg->exportCertificates();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -82,6 +113,9 @@ class ConfigDataImporter {
 | 
			
		|||
				case 'mainConfig':
 | 
			
		||||
					$steps[] = new ImporterStep(_('General settings'), 'mainConfig', $value);
 | 
			
		||||
					break;
 | 
			
		||||
				case 'certificates':
 | 
			
		||||
					$steps[] = new ImporterStep(_('SSL certificates'), 'certificates', $value);
 | 
			
		||||
					break;
 | 
			
		||||
				default:
 | 
			
		||||
					logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key);
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			@ -96,7 +130,7 @@ class ConfigDataImporter {
 | 
			
		|||
	 * Runs the actual import.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param ImporterStep[] $steps import steps
 | 
			
		||||
	 * @throws LAMException if error occured
 | 
			
		||||
	 * @throws LAMException if error occurred
 | 
			
		||||
	 */
 | 
			
		||||
	public function runImport($steps) {
 | 
			
		||||
		foreach ($steps as $step) {
 | 
			
		||||
| 
						 | 
				
			
			@ -106,9 +140,10 @@ class ConfigDataImporter {
 | 
			
		|||
			$key = $step->getKey();
 | 
			
		||||
			switch ($key) {
 | 
			
		||||
				case 'mainConfig':
 | 
			
		||||
					$cfgMain = new LAMCfgMain();
 | 
			
		||||
					$cfgMain->importData($step->getValue());
 | 
			
		||||
					$cfgMain->save();
 | 
			
		||||
					$this->importMainConfig($step->getValue());
 | 
			
		||||
					break;
 | 
			
		||||
				case 'certificates':
 | 
			
		||||
					$this->importCertificates($step->getValue());
 | 
			
		||||
					break;
 | 
			
		||||
				default:
 | 
			
		||||
					logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key);
 | 
			
		||||
| 
						 | 
				
			
			@ -116,6 +151,29 @@ class ConfigDataImporter {
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Imports the main configuration.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param array $data main config data
 | 
			
		||||
	 * @throws LAMException error during import
 | 
			
		||||
	 */
 | 
			
		||||
	private function importMainConfig($data) {
 | 
			
		||||
		$cfgMain = new LAMCfgMain();
 | 
			
		||||
		$cfgMain->importData($data);
 | 
			
		||||
		$cfgMain->save();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Imports the SSL certificates.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param null|string $data file content
 | 
			
		||||
	 * @throws LAMException error during import
 | 
			
		||||
	 */
 | 
			
		||||
	private function importCertificates($data) {
 | 
			
		||||
		$cfgMain = new LAMCfgMain();
 | 
			
		||||
		$cfgMain->importCertificates($data);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
<?php
 | 
			
		||||
namespace LAM\CONFIG;
 | 
			
		||||
use htmlButton;
 | 
			
		||||
use htmlGroup;
 | 
			
		||||
use htmlInputFileUpload;
 | 
			
		||||
use htmlLink;
 | 
			
		||||
use htmlOutputText;
 | 
			
		||||
| 
						 | 
				
			
			@ -222,8 +223,10 @@ printHeaderContents(_("Import and export configuration"), '../..');
 | 
			
		|||
            foreach ($importSteps as $importStep) {
 | 
			
		||||
                $content->add(new htmlResponsiveInputCheckbox('step_' . $importStep->getKey(), true, $importStep->getLabel()), 12);
 | 
			
		||||
            }
 | 
			
		||||
	        $content->add(new htmlButton('importConfigConfirm', _('Import')), 12);
 | 
			
		||||
	        $content->add(new htmlButton('importCancel', _('Cancel')), 12);
 | 
			
		||||
            $buttonGroup = new htmlGroup();
 | 
			
		||||
	        $buttonGroup->addElement(new htmlButton('importConfigConfirm', _('Import')));
 | 
			
		||||
	        $buttonGroup->addElement(new htmlButton('importCancel', _('Cancel')));
 | 
			
		||||
	        $content->add($buttonGroup, 12);
 | 
			
		||||
        }
 | 
			
		||||
        elseif (isset($_POST['importConfigConfirm'])) {
 | 
			
		||||
			$handle = fopen($_SESSION['configImportFile'], "r");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -37,12 +37,16 @@ class ConfigDataExporterTest extends TestCase {
 | 
			
		|||
			'confMainKey2' => 4,
 | 
			
		||||
			'confMainKey3' => '',
 | 
			
		||||
		);
 | 
			
		||||
		$expectedJson = json_encode(array('mainConfig' => $mainData));
 | 
			
		||||
		$expectedJson = json_encode(array(
 | 
			
		||||
			'mainConfig' => $mainData,
 | 
			
		||||
			'certificates' => 'certs'
 | 
			
		||||
		));
 | 
			
		||||
 | 
			
		||||
		$exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter')
 | 
			
		||||
			->setMethods(array('_getMainConfigData'))
 | 
			
		||||
			->setMethods(array('_getMainConfigData', '_getCertificates'))
 | 
			
		||||
			->getMock();
 | 
			
		||||
		$exporter->method('_getMainConfigData')->willReturn($mainData);
 | 
			
		||||
		$exporter->method('_getCertificates')->willReturn('certs');
 | 
			
		||||
 | 
			
		||||
		$json = $exporter->exportAsJson();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue