Skip to content

Commit 799c56c

Browse files
committed
Revert password values back to the config file if submitted empty
1 parent 5553172 commit 799c56c

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

library/Icinga/Web/Form/ConfigForm.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Exception;
99
use Icinga\Application\Config;
1010
use Icinga\Web\Widget\ShowConfiguration;
11+
use ipl\Html\FormElement\PasswordElement;
1112
use ipl\Stdlib\Str;
1213
use ipl\Web\Common\CsrfCounterMeasure;
1314
use ipl\Web\Compat\CompatForm;
@@ -95,6 +96,23 @@ protected function getConfigValue(string $name, mixed $default = null): mixed
9596
return $this->config->get($section, $key, $default);
9697
}
9798

99+
/**
100+
* Checks if the element should be reverted to the value from the INI file if empty.
101+
* This is useful for elements that aren't populated from the INI file.
102+
*
103+
* @param string $elementName The name of the element to check
104+
*
105+
* @return bool
106+
*/
107+
protected function shouldRevertIfEmpty(string $elementName): bool
108+
{
109+
$element = $this->getElement($elementName);
110+
return match (true) {
111+
$element instanceof PasswordElement => true,
112+
default => false,
113+
};
114+
}
115+
98116
/**
99117
* Persist the current configuration to disk
100118
*
@@ -108,7 +126,9 @@ protected function save(): void
108126
continue;
109127
}
110128
[$section, $key] = $parts;
111-
129+
if (Str::isEmpty($value) && $this->shouldRevertIfEmpty($element)) {
130+
$value = $this->config->get($section, $key);
131+
}
112132
$configSection = $this->config->getSection($section);
113133
if (Str::isEmpty($value)) {
114134
unset($configSection[$key]);

library/Icinga/Web/Form/ConfigSectionForm.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ protected function shouldRename(): bool
408408
protected function save(): void
409409
{
410410
foreach ($this->getValues() as $element => $value) {
411+
if (Str::isEmpty($value) && $this->shouldRevertIfEmpty($element)) {
412+
$value = $this->config->get($this->section, $element);
413+
}
411414
$configSection = $this->config->getSection($this->section);
412415
if (Str::isEmpty($value)) {
413416
unset($configSection[$element]);

0 commit comments

Comments
 (0)