chg: [settings] fixes

- use a JSON file for the config
- stop using cake4 dump/load for the process
- move settings back to the root level
- Research Flyer Carapace level 1
pull/79/head
iglocska 2021-10-21 10:58:07 +02:00
parent 7ba043682b
commit d8b2de7460
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
4 changed files with 70 additions and 17 deletions

View File

@ -87,7 +87,12 @@ try {
*/
if (file_exists(CONFIG . 'app_local.php')) {
Configure::load('app_local', 'default');
Configure::load('cerebrate', 'default', true);
//Configure::load('cerebrate', 'default', true);
$settings = file_get_contents(CONFIG . 'config.json');
$settings = json_decode($settings, true);
foreach ($settings as $path => $setting) {
Configure::write($path, $setting);
}
}
/*

View File

@ -136,7 +136,7 @@ class AppController extends Controller
if (!empty($user) && !empty($user->user_settings_by_name_with_fallback['ui.bsTheme']['value'])) {
$this->set('bsTheme', $user->user_settings_by_name_with_fallback['ui.bsTheme']['value']);
} else {
$this->set('bsTheme', Configure::read('Cerebrate')['ui.bsTheme']);
$this->set('bsTheme', Configure::read('ui.bsTheme'));
}
if ($this->modelClass == 'Tags.Tags') {

View File

@ -18,6 +18,24 @@ class CerebrateSettingsProvider extends BaseSettingsProvider
parent::__construct();
}
public function retrieveSettingPathsBasedOnBlueprint(): array
{
$blueprint = $this->generateSettingsConfiguration();
$paths = [];
foreach ($blueprint as $l1) {
foreach ($l1 as $l2) {
foreach ($l2 as $l3) {
foreach ($l3 as $k => $v) {
if ($k[0] !== '_') {
$paths[] = $k;
}
}
}
}
}
return $paths;
}
protected function generateSettingsConfiguration()
{
return [
@ -26,7 +44,7 @@ class CerebrateSettingsProvider extends BaseSettingsProvider
'Essentials' => [
'_description' => __('Ensentials settings required for the application to run normally.'),
'_icon' => 'user-cog',
'app.baseurl' => [
'App.baseurl' => [
'name' => __('Base URL'),
'type' => 'string',
'description' => __('The base url of the application (in the format https://www.mymispinstance.com or https://myserver.com/misp). Several features depend on this setting being correctly set to function.'),
@ -34,7 +52,7 @@ class CerebrateSettingsProvider extends BaseSettingsProvider
'severity' => 'critical',
'test' => 'testBaseURL',
],
'app.uuid' => [
'App.uuid' => [
'name' => 'UUID',
'type' => 'string',
'description' => __('The Cerebrate instance UUID. This UUID is used to identify this instance.'),
@ -85,26 +103,26 @@ class CerebrateSettingsProvider extends BaseSettingsProvider
],
'Network' => [
'Proxy' => [
'proxy.host' => [
'Proxy.host' => [
'name' => __('Host'),
'type' => 'string',
'description' => __('The hostname of an HTTP proxy for outgoing sync requests. Leave empty to not use a proxy.'),
'test' => 'testHostname',
],
'proxy.port' => [
'Proxy.port' => [
'name' => __('Port'),
'type' => 'integer',
'description' => __('The TCP port for the HTTP proxy.'),
'test' => 'testForRangeXY',
],
'proxy.user' => [
'Proxy.user' => [
'name' => __('User'),
'type' => 'string',
'description' => __('The authentication username for the HTTP proxy.'),
'default' => 'admin',
'dependsOn' => 'proxy.host',
],
'proxy.password' => [
'Proxy.password' => [
'name' => __('Password'),
'type' => 'string',
'description' => __('The authentication password for the HTTP proxy.'),
@ -181,11 +199,16 @@ class CerebrateSettingsProvider extends BaseSettingsProvider
'dependsOn' => 'keycloak.enabled'
],
'keycloak.default_role_name' => [
'name' => 'Authoritative',
'type' => 'boolean',
'name' => 'Default role',
'type' => 'select',
'severity' => 'info',
'description' => __('Override local role and organisation settings based on the settings in KeyCloak'),
'default' => false,
'description' => __('Select the default role name to be used when creating users'),
'options' => function ($settingsProviders) {
$roleTable = TableRegistry::getTableLocator()->get('Roles');
$allRoleNames = $roleTable->find()->toArray();
$allRoleNames = array_column($allRoleNames, 'name');
return array_combine($allRoleNames, $allRoleNames);
},
'dependsOn' => 'keycloak.enabled'
],
'keycloak.mapping.org_uuid' => [
@ -242,7 +265,7 @@ class CerebrateSettingsProvider extends BaseSettingsProvider
'Security' => [
'Development' => [
'Debugging' => [
'security.debug' => [
'debug' => [
'name' => __('Debug Level'),
'type' => 'select',
'description' => __('The debug level of the instance'),

View File

@ -4,6 +4,7 @@ namespace App\Model\Table;
use App\Model\Table\AppTable;
use Cake\ORM\Table;
use Cake\Core\Configure;
use Cake\Error\Debugger;
require_once(APP . 'Model' . DS . 'Table' . DS . 'SettingProviders' . DS . 'CerebrateSettingsProvider.php');
use App\Settings\SettingsProvider\CerebrateSettingsProvider;
@ -12,6 +13,13 @@ class SettingsTable extends AppTable
{
private static $FILENAME = 'cerebrate';
private static $CONFIG_KEY = 'Cerebrate';
private static $DUMPABLE = [
'Cerebrate',
'proxy',
'ui',
'keycloak',
'app'
];
public function initialize(array $config): void
{
@ -96,15 +104,32 @@ class SettingsTable extends AppTable
private function readSettings()
{
return Configure::read()[$this::$CONFIG_KEY];
$settingPaths = $this->SettingsProvider->retrieveSettingPathsBasedOnBlueprint();
$settings = [];
foreach ($settingPaths as $path) {
if (Configure::check($path)) {
$settings[$path] = Configure::read($path);
}
}
return $settings;
}
private function loadSettings(): void
{
$settings = file_get_contents(CONFIG . 'config.json');
$settings = json_decode($settings, true);
foreach ($settings as $path => $setting) {
Configure::write($path, $setting);
}
}
private function saveSettingOnDisk($name, $value)
{
$settings = $this->readSettings();
$settings[$name] = $value;
Configure::write($this::$CONFIG_KEY, $settings);
Configure::dump($this::$FILENAME, 'default', [$this::$CONFIG_KEY]);
$settings = json_encode($settings, JSON_PRETTY_PRINT);
file_put_contents(CONFIG . 'config.json', $settings);
$this->loadSettings();
return true;
}
}