1
0

DeclarativeSettingsController.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Controller;
  8. use Exception;
  9. use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
  10. use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
  11. use OCA\Settings\ResponseDefinitions;
  12. use OCP\AppFramework\Http;
  13. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\AppFramework\OCS\OCSBadRequestException;
  16. use OCP\AppFramework\OCSController;
  17. use OCP\IRequest;
  18. use OCP\IUserSession;
  19. use OCP\Settings\IDeclarativeManager;
  20. use Psr\Log\LoggerInterface;
  21. /**
  22. * @psalm-import-type SettingsDeclarativeForm from ResponseDefinitions
  23. */
  24. class DeclarativeSettingsController extends OCSController {
  25. public function __construct(
  26. string $appName,
  27. IRequest $request,
  28. private IUserSession $userSession,
  29. private IDeclarativeManager $declarativeManager,
  30. private LoggerInterface $logger,
  31. ) {
  32. parent::__construct($appName, $request);
  33. }
  34. /**
  35. * Sets a declarative settings value
  36. *
  37. * @param string $app ID of the app
  38. * @param string $formId ID of the form
  39. * @param string $fieldId ID of the field
  40. * @param mixed $value Value to be saved
  41. * @return DataResponse<Http::STATUS_OK, null, array{}>
  42. * @throws NotLoggedInException Not logged in or not an admin user
  43. * @throws NotAdminException Not logged in or not an admin user
  44. * @throws OCSBadRequestException Invalid arguments to save value
  45. *
  46. * 200: Value set successfully
  47. */
  48. #[NoAdminRequired]
  49. public function setValue(string $app, string $formId, string $fieldId, mixed $value): DataResponse {
  50. $user = $this->userSession->getUser();
  51. if ($user === null) {
  52. throw new NotLoggedInException();
  53. }
  54. try {
  55. $this->declarativeManager->loadSchemas();
  56. $this->declarativeManager->setValue($user, $app, $formId, $fieldId, $value);
  57. return new DataResponse(null);
  58. } catch (NotAdminException $e) {
  59. throw $e;
  60. } catch (Exception $e) {
  61. $this->logger->error('Failed to set declarative settings value: ' . $e->getMessage());
  62. throw new OCSBadRequestException();
  63. }
  64. }
  65. /**
  66. * Gets all declarative forms with the values prefilled.
  67. *
  68. * @return DataResponse<Http::STATUS_OK, list<SettingsDeclarativeForm>, array{}>
  69. * @throws NotLoggedInException
  70. * @NoSubAdminRequired
  71. *
  72. * 200: Forms returned
  73. */
  74. #[NoAdminRequired]
  75. public function getForms(): DataResponse {
  76. $user = $this->userSession->getUser();
  77. if ($user === null) {
  78. throw new NotLoggedInException();
  79. }
  80. $this->declarativeManager->loadSchemas();
  81. return new DataResponse($this->declarativeManager->getFormsWithValues($user, null, null));
  82. }
  83. }