store self service profiles as JSON

This commit is contained in:
Roland Gruber 2020-06-01 10:34:34 +02:00
parent 7bd799bee3
commit aa43b4721b
4 changed files with 55 additions and 6 deletions

View File

@ -4,6 +4,7 @@
"squizlabs/php_codesniffer" : "3.4.0"
},
"require": {
"ext-ldap": "*"
"ext-ldap": "*",
"ext-json": "*"
}
}

View File

@ -77,7 +77,6 @@ class ConfigDataExporter {
/**
* TODO
*
* pdf/account templates /config/templates
* self service profiles
* webauthn
* cron job runs

View File

@ -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).
*

View File

@ -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);
}
}
?>