diff --git a/src/Model/Table/SettingsProviderTable.php b/src/Model/Table/SettingProviders/BaseSettingsProvider.php similarity index 53% rename from src/Model/Table/SettingsProviderTable.php rename to src/Model/Table/SettingProviders/BaseSettingsProvider.php index 5483352..0efcaf4 100644 --- a/src/Model/Table/SettingsProviderTable.php +++ b/src/Model/Table/SettingProviders/BaseSettingsProvider.php @@ -1,27 +1,27 @@ settingsConfiguration = $this->generateSettingsConfiguration(); - $this->setTable(false); $this->error_critical = __('Cerebrate will not operate correctly or will be unsecure until these issues are resolved.'); $this->error_warning = __('Some of the features of Cerebrate cannot be utilised until these issues are resolved.'); $this->error_info = __('There are some optional tweaks that could be done to improve the looks of your Cerebrate instance.'); - $this->settingValidator = new SettingValidator(); + if (!isset($this->settingValidator)) { + $this->settingValidator = new SettingValidator(); + } } /** @@ -46,151 +46,9 @@ class SettingsProviderTable extends AppTable * redacted [optional]: Should the setting value be redacted. FIXME: To implement * cli_only [optional]: Should this setting be modified only via the CLI. */ - private function generateSettingsConfiguration() + protected function generateSettingsConfiguration() { - return [ - 'Application' => [ - 'General' => [ - 'Essentials' => [ - '_description' => __('Ensentials settings required for the application to run normally.'), - '_icon' => 'user-cog', - '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.'), - 'default' => '', - 'severity' => 'critical', - 'test' => 'testBaseURL', - ], - 'app.uuid' => [ - 'name' => 'UUID', - 'type' => 'string', - 'description' => __('The Cerebrate instance UUID. This UUID is used to identify this instance.'), - 'default' => '', - 'severity' => 'critical', - 'test' => 'testUuid', - ], - ], - 'Miscellaneous' => [ - 'sc2.hero' => [ - 'description' => 'The true hero', - 'default' => 'Sarah Kerrigan', - 'name' => 'Hero', - 'options' => [ - 'Jim Raynor' => 'Jim Raynor', - 'Sarah Kerrigan' => 'Sarah Kerrigan', - 'Artanis' => 'Artanis', - 'Zeratul' => 'Zeratul', - ], - 'type' => 'select' - ], - 'sc2.antagonists' => [ - 'description' => 'The bad guys', - 'default' => 'Amon', - 'name' => 'Antagonists', - 'options' => function($settingsProviders) { - return [ - 'Amon' => 'Amon', - 'Sarah Kerrigan' => 'Sarah Kerrigan', - 'Narud' => 'Narud', - ]; - }, - 'severity' => 'warning', - 'type' => 'multi-select' - ], - ], - 'floating-setting' => [ - 'description' => 'floaringSetting', - // 'default' => 'A default value', - 'name' => 'Uncategorized Setting', - // 'severity' => 'critical', - 'severity' => 'warning', - // 'severity' => 'info', - 'type' => 'integer' - ], - ], - 'Network' => [ - 'Proxy' => [ - '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' => [ - 'name' => __('Port'), - 'type' => 'integer', - 'description' => __('The TCP port for the HTTP proxy.'), - 'test' => 'testForRangeXY', - ], - 'proxy.user' => [ - 'name' => __('User'), - 'type' => 'string', - 'description' => __('The authentication username for the HTTP proxy.'), - 'default' => 'admin', - 'dependsOn' => 'proxy.host', - ], - 'proxy.password' => [ - 'name' => __('Password'), - 'type' => 'string', - 'description' => __('The authentication password for the HTTP proxy.'), - 'default' => '', - 'dependsOn' => 'proxy.host', - ], - ], - ], - 'UI' => [ - 'General' => [ - 'ui.bsTheme' => [ - 'description' => 'The Bootstrap theme to use for the application', - 'default' => 'default', - 'name' => 'UI Theme', - 'options' => function($settingsProviders) { - $instanceTable = TableRegistry::getTableLocator()->get('Instance'); - $themes = $instanceTable->getAvailableThemes(); - return array_combine($themes, $themes); - }, - 'severity' => 'info', - 'type' => 'select' - ], - ], - ], - ], - 'Security' => [ - 'Development' => [ - 'Debugging' => [ - 'security.debug' => [ - 'name' => __('Debug Level'), - 'type' => 'select', - 'description' => __('The debug level of the instance'), - 'default' => 0, - 'options' => [ - 0 => __('Debug Off'), - 1 => __('Debug On'), - 2 => __('Debug On + SQL Dump'), - ], - 'test' => function($value, $setting, $validator) { - $validator->range('value', [0, 3]); - return testValidator($value, $validator); - }, - ], - ], - ] - ], - 'Features' => [ - 'Demo Settings' => [ - 'demo.switch' => [ - 'name' => __('Switch'), - 'type' => 'boolean', - 'description' => __('A switch acting as a checkbox'), - 'default' => false, - 'test' => function() { - return 'Fake error'; - }, - ], - ] - ], - ]; + return []; } /** @@ -214,7 +72,7 @@ class SettingsProviderTable extends AppTable * @param array $settings the settings * @return void */ - private function mergeSettingsIntoSettingConfiguration(array $settingConf, array $settings, string $path=''): array + protected function mergeSettingsIntoSettingConfiguration(array $settingConf, array $settings, string $path=''): array { foreach ($settingConf as $key => $value) { if ($this->isSettingMetaKey($key)) { @@ -277,12 +135,12 @@ class SettingsProviderTable extends AppTable return $notices; } - private function isLeaf($setting) + protected function isLeaf($setting) { return !empty($setting['name']) && !empty($setting['type']); } - private function evaluateLeaf($setting, $settingSection) + protected function evaluateLeaf($setting, $settingSection) { $skipValidation = false; if ($setting['type'] == 'select' || $setting['type'] == 'multi-select') { @@ -353,12 +211,6 @@ class SettingsProviderTable extends AppTable } } -function testValidator($value, $validator) -{ - $errors = $validator->validate(['value' => $value]); - return !empty($errors) ? implode(', ', $errors['value']) : true; -} - class SettingValidator { @@ -383,23 +235,5 @@ class SettingValidator public function testForEmpty($value, &$setting) { return !empty($value) ? true : __('Cannot be empty'); - } - - public function testBaseURL($value, &$setting) - { - if (empty($value)) { - return __('Cannot be empty'); - } - if (!empty($value) && !preg_match('/^http(s)?:\/\//i', $value)) { - return __('Invalid URL, please make sure that the protocol is set.'); - } - return true; - } - - public function testUuid($value, &$setting) { - if (empty($value) || !preg_match('/^\{?[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}\}?$/', $value)) { - return __('Invalid UUID.'); - } - return true; - } + } } \ No newline at end of file diff --git a/src/Model/Table/SettingProviders/CerebrateSettingsProvider.php b/src/Model/Table/SettingProviders/CerebrateSettingsProvider.php new file mode 100644 index 0000000..ac50614 --- /dev/null +++ b/src/Model/Table/SettingProviders/CerebrateSettingsProvider.php @@ -0,0 +1,196 @@ +settingValidator = new CerebrateSettingValidator(); + parent::__construct(); + } + + protected function generateSettingsConfiguration() + { + return [ + 'Application' => [ + 'General' => [ + 'Essentials' => [ + '_description' => __('Ensentials settings required for the application to run normally.'), + '_icon' => 'user-cog', + '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.'), + 'default' => '', + 'severity' => 'critical', + 'test' => 'testBaseURL', + ], + 'app.uuid' => [ + 'name' => 'UUID', + 'type' => 'string', + 'description' => __('The Cerebrate instance UUID. This UUID is used to identify this instance.'), + 'default' => '', + 'severity' => 'critical', + 'test' => 'testUuid', + ], + ], + 'Miscellaneous' => [ + 'sc2.hero' => [ + 'description' => 'The true hero', + 'default' => 'Sarah Kerrigan', + 'name' => 'Hero', + 'options' => [ + 'Jim Raynor' => 'Jim Raynor', + 'Sarah Kerrigan' => 'Sarah Kerrigan', + 'Artanis' => 'Artanis', + 'Zeratul' => 'Zeratul', + ], + 'type' => 'select' + ], + 'sc2.antagonists' => [ + 'description' => 'The bad guys', + 'default' => 'Amon', + 'name' => 'Antagonists', + 'options' => function ($settingsProviders) { + return [ + 'Amon' => 'Amon', + 'Sarah Kerrigan' => 'Sarah Kerrigan', + 'Narud' => 'Narud', + ]; + }, + 'severity' => 'warning', + 'type' => 'multi-select' + ], + ], + 'floating-setting' => [ + 'description' => 'floaringSetting', + // 'default' => 'A default value', + 'name' => 'Uncategorized Setting', + // 'severity' => 'critical', + 'severity' => 'warning', + // 'severity' => 'info', + 'type' => 'integer' + ], + ], + 'Network' => [ + 'Proxy' => [ + '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' => [ + 'name' => __('Port'), + 'type' => 'integer', + 'description' => __('The TCP port for the HTTP proxy.'), + 'test' => 'testForRangeXY', + ], + 'proxy.user' => [ + 'name' => __('User'), + 'type' => 'string', + 'description' => __('The authentication username for the HTTP proxy.'), + 'default' => 'admin', + 'dependsOn' => 'proxy.host', + ], + 'proxy.password' => [ + 'name' => __('Password'), + 'type' => 'string', + 'description' => __('The authentication password for the HTTP proxy.'), + 'default' => '', + 'dependsOn' => 'proxy.host', + ], + ], + ], + 'UI' => [ + 'General' => [ + 'ui.bsTheme' => [ + 'description' => 'The Bootstrap theme to use for the application', + 'default' => 'default', + 'name' => 'UI Theme', + 'options' => function ($settingsProviders) { + $instanceTable = TableRegistry::getTableLocator()->get('Instance'); + $themes = $instanceTable->getAvailableThemes(); + return array_combine($themes, $themes); + }, + 'severity' => 'info', + 'type' => 'select' + ], + ], + ], + ], + 'Security' => [ + 'Development' => [ + 'Debugging' => [ + 'security.debug' => [ + 'name' => __('Debug Level'), + 'type' => 'select', + 'description' => __('The debug level of the instance'), + 'default' => 0, + 'options' => [ + 0 => __('Debug Off'), + 1 => __('Debug On'), + 2 => __('Debug On + SQL Dump'), + ], + 'test' => function ($value, $setting, $validator) { + $validator->range('value', [0, 3]); + return testValidator($value, $validator); + }, + ], + ], + ] + ], + 'Features' => [ + 'Demo Settings' => [ + 'demo.switch' => [ + 'name' => __('Switch'), + 'type' => 'boolean', + 'description' => __('A switch acting as a checkbox'), + 'default' => false, + 'test' => function () { + return 'Fake error'; + }, + ], + ] + ], + ]; + } +} + +function testValidator($value, $validator) +{ + $errors = $validator->validate(['value' => $value]); + return !empty($errors) ? implode(', ', $errors['value']) : true; +} + +class CerebrateSettingValidator extends SettingValidator +{ + public function testUuid($value, &$setting) + { + if (empty($value) || !preg_match('/^\{?[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}\}?$/', $value)) { + return __('Invalid UUID.'); + } + return true; + } + + + public function testBaseURL($value, &$setting) + { + if (empty($value)) { + return __('Cannot be empty'); + } + if (!empty($value) && !preg_match('/^http(s)?:\/\//i', $value)) { + return __('Invalid URL, please make sure that the protocol is set.'); + } + return true; + } +} \ No newline at end of file diff --git a/src/Model/Table/SettingsTable.php b/src/Model/Table/SettingsTable.php index 0235cdc..0459ce7 100644 --- a/src/Model/Table/SettingsTable.php +++ b/src/Model/Table/SettingsTable.php @@ -3,9 +3,10 @@ namespace App\Model\Table; use App\Model\Table\AppTable; use Cake\ORM\Table; -use Cake\Validation\Validator; use Cake\Core\Configure; -use Cake\ORM\TableRegistry; + +require_once(APP . 'Model' . DS . 'Table' . DS . 'SettingProviders' . DS . 'CerebrateSettingsProvider.php'); +use App\Settings\SettingsProvider\CerebrateSettingsProvider; class SettingsTable extends AppTable { @@ -16,7 +17,7 @@ class SettingsTable extends AppTable { parent::initialize($config); $this->setTable(false); - $this->SettingsProvider = TableRegistry::getTableLocator()->get('SettingsProvider'); + $this->SettingsProvider = new CerebrateSettingsProvider(); } public function getSettings($full=false): array diff --git a/templates/Instance/settings.php b/templates/Instance/settings.php index 20215c6..334b5c5 100644 --- a/templates/Instance/settings.php +++ b/templates/Instance/settings.php @@ -26,191 +26,48 @@ array_unshift($tabContents, $notice); ?>