-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathConfigController.php
More file actions
89 lines (76 loc) · 2.5 KB
/
ConfigController.php
File metadata and controls
89 lines (76 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Forms\Controller;
use OCA\Forms\Constants;
use OCA\Forms\Service\ConfigService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class ConfigController extends ApiController {
public function __construct(
protected $appName,
private ConfigService $configService,
private IAppConfig $appConfig,
private LoggerInterface $logger,
IRequest $request,
) {
parent::__construct($appName, $request);
}
/**
* Get the current AppConfig
* @return DataResponse
*/
#[FrontpageRoute(verb: 'GET', url: '/config')]
public function getAppConfig(): DataResponse {
return new DataResponse($this->configService->getAppConfig());
}
/**
* Update values on appConfig.
* Admin required, thus not checking separately.
*
* @param string $configKey AppConfig Key to store
* @param mixed $configValue Corresponding AppConfig Value
*
*/
#[FrontpageRoute(verb: 'PATCH', url: '/config')]
public function updateAppConfig(string $configKey, mixed $configValue): DataResponse {
$this->logger->debug('Updating AppConfig: {configKey} => {configValue}', [
'configKey' => $configKey,
'configValue' => $configValue
]);
// Check for allowed keys
if (!in_array($configKey, Constants::CONFIG_KEYS)) {
return new DataResponse('Unknown appConfig key: ' . $configKey, Http::STATUS_BAD_REQUEST);
}
if ($configKey === Constants::CONFIG_KEY_ALLOWCONFIRMATIONEMAIL && $configValue === true && !$this->configService->isMailConfigured()) {
return new DataResponse('Mail server is not configured', Http::STATUS_BAD_REQUEST);
}
// Set on DB with typed setters
try {
switch (Constants::CONFIG_KEY_TYPES[$configKey]) {
case 'bool':
$this->appConfig->setAppValueBool($configKey, $configValue);
break;
case 'array':
$this->appConfig->setAppValueArray($configKey, $configValue);
break;
case 'int':
$this->appConfig->setAppValueInt($configKey, $configValue);
break;
}
} catch (\InvalidArgumentException) {
return new DataResponse('Invalid value for ' . $configKey, Http::STATUS_BAD_REQUEST);
}
return new DataResponse();
}
}