Skip to content

Commit 2975a99

Browse files
Merge pull request #54335 from nextcloud/feat/noid/lexicon-appconfig-controller
feat(lexicon): get value type from lexicon
2 parents b243fd3 + 4f0d563 commit 2975a99

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

apps/provisioning_api/lib/Controller/AppConfigController.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
use OC\AppConfig;
1212
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
13+
use OC\Config\ConfigManager;
1314
use OCP\App\IAppManager;
1415
use OCP\AppFramework\Http;
1516
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1617
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
1718
use OCP\AppFramework\Http\DataResponse;
1819
use OCP\AppFramework\OCSController;
20+
use OCP\Config\ValueType;
1921
use OCP\Exceptions\AppConfigUnknownKeyException;
2022
use OCP\IAppConfig;
2123
use OCP\IGroupManager;
@@ -37,6 +39,7 @@ public function __construct(
3739
private IGroupManager $groupManager,
3840
private IManager $settingManager,
3941
private IAppManager $appManager,
42+
private readonly ConfigManager $configManager,
4043
) {
4144
parent::__construct($appName, $request);
4245
}
@@ -130,19 +133,27 @@ public function setValue(string $app, string $key, string $value): DataResponse
130133
}
131134

132135
$type = null;
133-
try {
134-
$configDetails = $this->appConfig->getDetails($app, $key);
135-
$type = $configDetails['type'];
136-
} catch (AppConfigUnknownKeyException) {
136+
137+
// checking expected type from lexicon
138+
$keyDetails = $this->appConfig->getKeyDetails($app, $key);
139+
if (array_key_exists('valueType', $keyDetails)) {
140+
$type = $keyDetails['valueType'];
141+
} else {
142+
// if no details from lexicon, get from eventual current value in database
143+
try {
144+
$configDetails = $this->appConfig->getDetails($app, $key);
145+
$type = $configDetails['type'];
146+
} catch (AppConfigUnknownKeyException) {
147+
}
137148
}
138149

139150
/** @psalm-suppress InternalMethod */
140151
match ($type) {
141-
IAppConfig::VALUE_BOOL => $this->appConfig->setValueBool($app, $key, (bool)$value),
142-
IAppConfig::VALUE_FLOAT => $this->appConfig->setValueFloat($app, $key, (float)$value),
143-
IAppConfig::VALUE_INT => $this->appConfig->setValueInt($app, $key, (int)$value),
144-
IAppConfig::VALUE_STRING => $this->appConfig->setValueString($app, $key, $value),
145-
IAppConfig::VALUE_ARRAY => $this->appConfig->setValueArray($app, $key, \json_decode($value, true)),
152+
IAppConfig::VALUE_BOOL, ValueType::BOOL => $this->appConfig->setValueBool($app, $key, $this->configManager->convertToBool($value)),
153+
IAppConfig::VALUE_FLOAT, ValueType::FLOAT => $this->appConfig->setValueFloat($app, $key, $this->configManager->convertToFloat($value)),
154+
IAppConfig::VALUE_INT, ValueType::INT => $this->appConfig->setValueInt($app, $key, $this->configManager->convertToInt($value)),
155+
IAppConfig::VALUE_STRING, ValueType::STRING => $this->appConfig->setValueString($app, $key, $value),
156+
IAppConfig::VALUE_ARRAY, ValueType::ARRAY => $this->appConfig->setValueArray($app, $key, $this->configManager->convertToArray($value)),
146157
default => $this->appConfig->setValueMixed($app, $key, $value),
147158
};
148159

apps/provisioning_api/tests/Controller/AppConfigControllerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace OCA\Provisioning_API\Tests\Controller;
99

1010
use OC\AppConfig;
11+
use OC\Config\ConfigManager;
1112
use OCA\Provisioning_API\Controller\AppConfigController;
1213
use OCP\App\IAppManager;
1314
use OCP\AppFramework\Http;
@@ -38,6 +39,7 @@ class AppConfigControllerTest extends TestCase {
3839
private IManager&MockObject $settingManager;
3940
private IGroupManager&MockObject $groupManager;
4041
private IAppManager $appManager;
42+
private ConfigManager $configManager;
4143

4244
protected function setUp(): void {
4345
parent::setUp();
@@ -48,6 +50,7 @@ protected function setUp(): void {
4850
$this->settingManager = $this->createMock(IManager::class);
4951
$this->groupManager = $this->createMock(IGroupManager::class);
5052
$this->appManager = Server::get(IAppManager::class);
53+
$this->configManager = Server::get(ConfigManager::class);
5154
}
5255

5356
/**
@@ -67,6 +70,7 @@ protected function getInstance(array $methods = []) {
6770
$this->groupManager,
6871
$this->settingManager,
6972
$this->appManager,
73+
$this->configManager,
7074
);
7175
} else {
7276
return $this->getMockBuilder(AppConfigController::class)
@@ -79,6 +83,7 @@ protected function getInstance(array $methods = []) {
7983
$this->groupManager,
8084
$this->settingManager,
8185
$this->appManager,
86+
$this->configManager,
8287
])
8388
->onlyMethods($methods)
8489
->getMock();

0 commit comments

Comments
 (0)