diff --git a/composer.json b/composer.json index f2015f0d..7c61b448 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,7 @@ "squizlabs/php_codesniffer" : "3.4.0" }, "require": { - "ext-ldap": "*" + "ext-ldap": "*", + "ext-json": "*" } } diff --git a/lam/lib/persistence.inc b/lam/lib/persistence.inc index 5ad8b67d..b5f32782 100644 --- a/lam/lib/persistence.inc +++ b/lam/lib/persistence.inc @@ -77,7 +77,6 @@ class ConfigDataExporter { /** * TODO * - * pdf/account templates /config/templates * self service profiles * webauthn * cron job runs diff --git a/lam/lib/selfService.inc b/lam/lib/selfService.inc index 7d8b63c9..c96e0c82 100644 --- a/lam/lib/selfService.inc +++ b/lam/lib/selfService.inc @@ -213,7 +213,13 @@ function loadSelfServiceProfile($name, $scope) { $file = @fopen($file, "r"); if ($file) { $data = fread($file, 10000000); - $profile = unserialize($data); + $profileData = @json_decode($data, true); + if ($profileData === null) { + $profile = unserialize($data); + } + else { + $profile = selfServiceProfile::import($profileData); + } fclose($file); } else { @@ -249,7 +255,7 @@ function saveSelfServiceProfile($name, $scope, $profile) { $file = @fopen($path, "w"); if ($file) { // write settings to file - fputs($file, serialize($profile)); + fputs($file, json_encode($profile->export())); // close file fclose($file); } @@ -521,6 +527,32 @@ class selfServiceProfile { $this->baseUrl = ''; } + /** + * Converts the export data back to a self service profile. + * + * @param array $data export data + * @return selfServiceProfile profile + */ + public static function import($data) { + $profile = new selfServiceProfile(); + $vars = get_class_vars(selfServiceProfile::class); + foreach ($data as $key => $value) { + if (in_array($key, $vars)) { + $profile->$key = $value; + } + } + return $profile; + } + + /** + * Returns a representation of the self service profile. + * + * @return array self service profile data + */ + public function export() { + return json_decode(json_encode($this), true); + } + /** * Returns the server's base URL (e.g. https://www.example.com). * diff --git a/lam/tests/lib/selfServiceTest.php b/lam/tests/lib/selfServiceTest.php index 67e3ce23..b7e65859 100644 --- a/lam/tests/lib/selfServiceTest.php +++ b/lam/tests/lib/selfServiceTest.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) 2019 Roland Gruber + Copyright (C) 2019 - 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; */ -require_once 'lam/lib/selfService.inc'; +require_once __DIR__ . '/../../lib/selfService.inc'; /** * Checks selfServiceProfile. @@ -43,6 +43,23 @@ class selfServiceProfileTest extends TestCase { $this->assertEquals('https://test.com', $profile->getBaseUrl()); } + public function testImportExport() { + $profile = new selfServiceProfile(); + $moduleSettings = array('x1' => 'y1', 'x2' => 'y2'); + $profile->moduleSettings = $moduleSettings; + $profile->baseColor = 'green'; + $profile->language = 'de_DE@UTF8'; + $profile->pageHeader = 'header'; + + $export = $profile->export(); + $importedProfile = selfServiceProfile::import($export); + + $this->assertEquals($moduleSettings, $importedProfile->moduleSettings); + $this->assertEquals('green', $importedProfile->baseColor); + $this->assertEquals('de_DE@UTF8', $importedProfile->language); + $this->assertEquals('header', $importedProfile->pageHeader); + } + } ?> \ No newline at end of file