From 89b4caede5a5af144d0f74897aea2f737151b7d5 Mon Sep 17 00:00:00 2001 From: jessevz Date: Mon, 16 Mar 2026 12:01:16 +0100 Subject: [PATCH 1/2] Made it possible to update a single config --- src/inc/apiv2/model/ConfigAPI.php | 4 +++ src/inc/utils/ConfigUtils.php | 60 ++++++++++++++++--------------- 2 files changed, 36 insertions(+), 28 deletions(-) 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..ca391e642 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); + $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) { + return; //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); + } /** * @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(); From e9cc2d8396151a9f8b4fe2ea680aace85d2f2044 Mon Sep 17 00:00:00 2001 From: jessevz Date: Mon, 16 Mar 2026 13:10:32 +0100 Subject: [PATCH 2/2] Fixed copilot suggestions --- src/inc/utils/ConfigUtils.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/inc/utils/ConfigUtils.php b/src/inc/utils/ConfigUtils.php index ca391e642..c7a3ea490 100644 --- a/src/inc/utils/ConfigUtils.php +++ b/src/inc/utils/ConfigUtils.php @@ -79,17 +79,17 @@ public static function getAll() { 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 (is_null($currentConfig)) { - throw new HTException("No config with this ID!"); - } if ($currentConfig->getValue() === $newValue) { - return; //The value was not changed so we dont need to update it + return; //The value was not changed so we don't need to update it. } $lengthLimits = [