diff --git a/src/inc/apiv2/model/ConfigAPI.php b/src/inc/apiv2/model/ConfigAPI.php index 8534033e5..d324b946d 100644 --- a/src/inc/apiv2/model/ConfigAPI.php +++ b/src/inc/apiv2/model/ConfigAPI.php @@ -47,6 +47,10 @@ protected function createObject(array $data): int { protected function deleteObject(object $object): void { throw new HttpError("Configs cannot be deleted via API"); } + + protected function updateObject(int $objectId, array $data): void { + ConfigUtils::updateSingleConfig($objectId, $data); + } /** * @throws HTException diff --git a/src/inc/utils/ConfigUtils.php b/src/inc/utils/ConfigUtils.php index fce182f54..c7a3ea490 100644 --- a/src/inc/utils/ConfigUtils.php +++ b/src/inc/utils/ConfigUtils.php @@ -76,6 +76,37 @@ public static function getAll() { } const DEFAULT_CONFIG_SECTION = 5; + + public static function updateSingleConfig($id, $attributes) { + $currentConfig = Factory::getConfigFactory()->get($id); + if (is_null($currentConfig)) { + throw new HTException("No config with this ID!"); + } + $newValue = $attributes[Config::VALUE] ?? null; + $name = $currentConfig->getItem(); + + if (is_null($newValue)) { + throw new HTException("No new config value provided"); + } + if ($currentConfig->getValue() === $newValue) { + return; //The value was not changed so we don't need to update it. + } + + $lengthLimits = [ + DConfig::HASH_MAX_LENGTH => 'setMaxHashLength', + DConfig::PLAINTEXT_MAX_LENGTH => 'setPlaintextMaxLength' + ]; + if (isset($lengthLimits[$name])) { + $limit = intval($newValue); + if (!Util::{$lengthLimits[$name]}($limit)) { + throw new HTException("Failed to update {$name}!"); + } + } + + SConfig::getInstance()->addValue($name, $newValue); + $currentConfig->setValue($newValue); + ConfigUtils::set($currentConfig, false); + } /** * @param array $arr id => [attributes] @@ -86,34 +117,7 @@ public static function getAll() { */ public static function updateConfigs($arr) { foreach ($arr as $id => $attributes) { - $currentConfig = Factory::getConfigFactory()->get($id); - $newValue = $attributes[Config::VALUE] ?? null; - $name = $currentConfig->getItem(); - - if (is_null($newValue)) { - throw new HTException("No new config value provided"); - } - if (is_null($currentConfig)) { - throw new HTException("No config with this ID!"); - } - if ($currentConfig->getValue() === $newValue) { - continue; //The value was not changed so we dont need to update it - } - - $lengthLimits = [ - DConfig::HASH_MAX_LENGTH => 'setMaxHashLength', - DConfig::PLAINTEXT_MAX_LENGTH => 'setPlaintextMaxLength' - ]; - if (isset($lengthLimits[$name])) { - $limit = intval($newValue); - if (!Util::{$lengthLimits[$name]}($limit)) { - throw new HTException("Failed to update {$name}!"); - } - } - - SConfig::getInstance()->addValue($name, $newValue); - $currentConfig->setValue($newValue); - ConfigUtils::set($currentConfig, false); + self::updateSingleConfig($id, $attributes); } SConfig::reload();