new: [CLI] Allow to set setting value to `null`

pull/7927/head
Jakub Onderka 2021-11-05 22:49:11 +01:00
parent f35053d288
commit 51feb8034f
2 changed files with 33 additions and 18 deletions

View File

@ -22,12 +22,18 @@ class AdminShell extends AppShell
'value' => ['help' => __('Setting value'), 'required' => true], 'value' => ['help' => __('Setting value'), 'required' => true],
], ],
'options' => [ 'options' => [
'force' => array( 'force' => [
'short' => 'f', 'short' => 'f',
'help' => 'Force the command.', 'help' => 'Force the command.',
'default' => false, 'default' => false,
'boolean' => true 'boolean' => true
) ],
'null' => [
'short' => 'n',
'help' => 'Set the value to null.',
'default' => false,
'boolean' => true
],
] ]
], ],
]); ]);
@ -392,15 +398,17 @@ class AdminShell extends AppShell
public function setSetting() public function setSetting()
{ {
$setting_name = !isset($this->args[0]) ? null : $this->args[0]; list($setting_name, $value) = $this->args;
$value = !isset($this->args[1]) ? null : $this->args[1];
if ($value === 'false') { if ($value === 'false') {
$value = 0; $value = 0;
} elseif ($value === 'true') { } elseif ($value === 'true') {
$value = 1; $value = 1;
} }
if ($this->params['null']) {
$value = null;
}
$cli_user = array('id' => 0, 'email' => 'SYSTEM', 'Organisation' => array('name' => 'SYSTEM')); $cli_user = array('id' => 0, 'email' => 'SYSTEM', 'Organisation' => array('name' => 'SYSTEM'));
if (empty($setting_name) || $value === null) { if (empty($setting_name) || ($value === null && !$this->params['null'])) {
die('Usage: ' . $this->Server->command_line_functions['console_admin_tasks']['data']['Set setting'] . PHP_EOL); die('Usage: ' . $this->Server->command_line_functions['console_admin_tasks']['data']['Set setting'] . PHP_EOL);
} }
$setting = $this->Server->getSettingData($setting_name); $setting = $this->Server->getSettingData($setting_name);
@ -410,7 +418,7 @@ class AdminShell extends AppShell
} }
$result = $this->Server->serverSettingsEditValue($cli_user, $setting, $value, $this->params['force']); $result = $this->Server->serverSettingsEditValue($cli_user, $setting, $value, $this->params['force']);
if ($result === true) { if ($result === true) {
echo 'Setting "' . $setting_name . '" changed to ' . $value . PHP_EOL; $this->out(__('Setting "%s" changed to %s', $setting_name, is_string($value) ? '"' . $value . '"' : (string)$value));
} else { } else {
$message = __("The setting change was rejected. MISP considers the requested setting value as invalid and would lead to the following error:\n\n\"%s\"\n\nIf you still want to force this change, please supply the --force argument.\n", $result); $message = __("The setting change was rejected. MISP considers the requested setting value as invalid and would lead to the following error:\n\n\"%s\"\n\nIf you still want to force this change, please supply the --force argument.\n", $result);
$this->error(__('Setting change rejected.'), $message); $this->error(__('Setting change rejected.'), $message);

View File

@ -2147,21 +2147,28 @@ class Server extends AppModel
return $beforeResult; return $beforeResult;
} }
} }
$value = trim($value); if ($value !== null) {
if ($setting['type'] === 'boolean') { $value = trim($value);
$value = (bool)$value; if ($setting['type'] === 'boolean') {
} else if ($setting['type'] === 'numeric') { $value = (bool)$value;
$value = (int)($value); } else if ($setting['type'] === 'numeric') {
} $value = (int)($value);
if (isset($setting['test'])) {
if ($setting['test'] instanceof Closure) {
$testResult = $setting['test']($value);
} else {
$testResult = $this->{$setting['test']}($value);
} }
if (isset($setting['test'])) {
if ($setting['test'] instanceof Closure) {
$testResult = $setting['test']($value);
} else {
$testResult = $this->{$setting['test']}($value);
}
} else {
$testResult = true; # No test defined for this setting: cannot fail
}
} else if (isset($setting['null']) && $setting['null']) {
$testResult = true;
} else { } else {
$testResult = true; # No test defined for this setting: cannot fail $testResult = __('Value could not be null.');
} }
if (!$forceSave && $testResult !== true) { if (!$forceSave && $testResult !== true) {
if ($testResult === false) { if ($testResult === false) {
$errorMessage = $setting['errorMessage']; $errorMessage = $setting['errorMessage'];