This commit is contained in:
Sukhwinder Dhillon 2022-05-19 09:45:01 +02:00 committed by raviks789
parent 7e49896331
commit dd26cc5558
3 changed files with 31 additions and 12 deletions

View File

@ -95,7 +95,7 @@ class ConfigController extends Controller
{ {
$this->assertPermission('config/general'); $this->assertPermission('config/general');
$form = new GeneralConfigForm(); $form = new GeneralConfigForm();
$form->setIniConfig(Config::app()); $form->setConfigScope();
$form->handleRequest(); $form->handleRequest();
$this->view->form = $form; $this->view->form = $form;

View File

@ -28,6 +28,8 @@ class GeneralConfigForm extends ConfigForm
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
parent::createElements($formData);
$appConfigForm = new ApplicationConfigForm(); $appConfigForm = new ApplicationConfigForm();
$loggingConfigForm = new LoggingConfigForm(); $loggingConfigForm = new LoggingConfigForm();
$themingConfigForm = new ThemingConfigForm(); $themingConfigForm = new ThemingConfigForm();

View File

@ -129,6 +129,18 @@ class ConfigForm extends Form
return $this; return $this;
} }
public function createElements(array $formData)
{
$this->addElement(
'hidden',
'hash',
[
'value' => $formData['hash'] ?? '',
'required' => true
]
);
}
public function isValid($formData) public function isValid($formData)
{ {
$valid = parent::isValid($formData); $valid = parent::isValid($formData);
@ -206,7 +218,11 @@ class ConfigForm extends Form
return false; return false;
} catch (Exception $e) { } catch (Exception $e) {
//TODO: Add exception for db config if (! $this->isConfigResourceIni()) {
$this->addError($e->getMessage());
return false;
}
$this->addDecorator('ViewScript', array( $this->addDecorator('ViewScript', array(
'viewModule' => 'default', 'viewModule' => 'default',
@ -238,6 +254,7 @@ class ConfigForm extends Form
} }
$values = static::transformEmptyValuesToNull($this->getValues()); $values = static::transformEmptyValuesToNull($this->getValues());
unset($values['hash']);
$valuesAsStr = implode(', ', array_map( $valuesAsStr = implode(', ', array_map(
function ($v, $k) { return sprintf("%s=%s", $k, $v); }, function ($v, $k) { return sprintf("%s=%s", $k, $v); },
@ -245,7 +262,7 @@ class ConfigForm extends Form
array_keys($values) array_keys($values)
)); ));
$hash = sha1($valuesAsStr, true); $newHash = sha1($valuesAsStr, true);
$db = $this->getDb(); $db = $this->getDb();
@ -263,7 +280,7 @@ class ConfigForm extends Form
$db->insert('icingaweb_config_scope', [ $db->insert('icingaweb_config_scope', [
'module' => $this->getModuleName(), 'module' => $this->getModuleName(),
'name' => $this->getScopeName(), 'name' => $this->getScopeName(),
'hash' => $hash 'hash' => $newHash
]); ]);
$id = $db->lastInsertId(); $id = $db->lastInsertId();
@ -277,11 +294,11 @@ class ConfigForm extends Form
} }
} }
} elseif ($data->hash !== $hash) { } elseif ($data->hash !== $newHash) {
$db->update('icingaweb_config_scope', [ $db->update('icingaweb_config_scope', [
'module' => $this->getModuleName(), 'module' => $this->getModuleName(),
'name' => $this->getScopeName(), 'name' => $this->getScopeName(),
'hash' => $hash 'hash' => $newHash
], ['id = ?' => $data->id]); ], ['id = ?' => $data->id]);
$db->delete('icingaweb_config_option', ['scope_id = ?' => $data->id]); $db->delete('icingaweb_config_option', ['scope_id = ?' => $data->id]);
@ -322,17 +339,17 @@ class ConfigForm extends Form
protected function fromDb() protected function fromDb()
{ {
$options = []; $data = ConfigScope::on($this->getDb())->with(['option']);
$db = (new self())->getDb();
$data = ConfigScope::on($db)->with(['option']);
$data->filter(Filter::all( $data->filter(Filter::all(
Filter::equal('module', $this->getModuleName()), Filter::equal('module', $this->getModuleName()),
Filter::equal('name', $this->getScopeName()) Filter::equal('name', $this->getScopeName())
)); ));
foreach ($data as $v) { $options = [];
$options[$v->name][$v->option->name] = $v->option->value;
foreach ($data as $values) {
$options[$values->name][$values->option->name] = $values->option->value;
$options[$values->name]['hash'] = bin2hex($values->hash);
} }
return new ConfigObject($options); return new ConfigObject($options);