|
32 | 32 | use OCP\AppFramework\Http\Attribute\PublicPage; |
33 | 33 | use OCP\AppFramework\Http\Attribute\UseSession; |
34 | 34 | use OCP\AppFramework\Http\DataResponse; |
| 35 | +use OCP\IConfig; |
35 | 36 | use OCP\IL10N; |
36 | 37 | use OCP\IRequest; |
37 | 38 | use OCP\IURLGenerator; |
@@ -62,6 +63,7 @@ public function __construct( |
62 | 63 | protected IUserSession $userSession, |
63 | 64 | protected SessionService $sessionService, |
64 | 65 | private ValidateHelper $validateHelper, |
| 66 | + private IConfig $config, |
65 | 67 | ) { |
66 | 68 | parent::__construct(Application::APP_ID, $request); |
67 | 69 | } |
@@ -518,4 +520,43 @@ public function readPfxData(string $password): DataResponse { |
518 | 520 | Http::STATUS_ACCEPTED |
519 | 521 | ); |
520 | 522 | } |
| 523 | + |
| 524 | + /** |
| 525 | + * Set user config value |
| 526 | + * |
| 527 | + * @param string $key Config key |
| 528 | + * @param mixed $value Config value |
| 529 | + * @return DataResponse<Http::STATUS_OK, array{key: string, value: mixed}, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{message: string}, array{}> |
| 530 | + * |
| 531 | + * 200: Config updated |
| 532 | + * 400: Error updating config |
| 533 | + */ |
| 534 | + #[NoAdminRequired] |
| 535 | + #[NoCSRFRequired] |
| 536 | + #[ApiRoute(verb: 'PUT', url: '/api/{apiVersion}/account/config/{key}', requirements: ['apiVersion' => '(v1)'])] |
| 537 | + public function setConfig(string $key): DataResponse { |
| 538 | + try { |
| 539 | + $user = $this->userSession->getUser(); |
| 540 | + if (!$user) { |
| 541 | + throw new \Exception('User not authenticated'); |
| 542 | + } |
| 543 | + $data = $this->request->getParams(); |
| 544 | + $value = $data['value'] ?? null; |
| 545 | + |
| 546 | + if (is_bool($value)) { |
| 547 | + $value = $value ? '1' : '0'; |
| 548 | + } |
| 549 | + |
| 550 | + $this->config->setUserValue($user->getUID(), Application::APP_ID, $key, $value); |
| 551 | + |
| 552 | + return new DataResponse([ |
| 553 | + 'key' => $key, |
| 554 | + 'value' => $value, |
| 555 | + ], Http::STATUS_OK); |
| 556 | + } catch (\Throwable $e) { |
| 557 | + return new DataResponse([ |
| 558 | + 'message' => $e->getMessage(), |
| 559 | + ], Http::STATUS_BAD_REQUEST); |
| 560 | + } |
| 561 | + } |
521 | 562 | } |
0 commit comments