You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeclarativeSettingsController.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2023 Kate Döen <kate.doeen@nextcloud.com>
  4. *
  5. * @author Kate Döen <kate.doeen@nextcloud.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Settings\Controller;
  24. use Exception;
  25. use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
  26. use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
  27. use OCA\Settings\ResponseDefinitions;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCS\OCSBadRequestException;
  32. use OCP\AppFramework\OCSController;
  33. use OCP\IRequest;
  34. use OCP\IUserSession;
  35. use OCP\Settings\IDeclarativeManager;
  36. use Psr\Log\LoggerInterface;
  37. /**
  38. * @psalm-import-type SettingsDeclarativeForm from ResponseDefinitions
  39. */
  40. class DeclarativeSettingsController extends OCSController {
  41. public function __construct(
  42. string $appName,
  43. IRequest $request,
  44. private IUserSession $userSession,
  45. private IDeclarativeManager $declarativeManager,
  46. private LoggerInterface $logger,
  47. ) {
  48. parent::__construct($appName, $request);
  49. }
  50. /**
  51. * Sets a declarative settings value
  52. *
  53. * @param string $app ID of the app
  54. * @param string $formId ID of the form
  55. * @param string $fieldId ID of the field
  56. * @param mixed $value Value to be saved
  57. * @return DataResponse<Http::STATUS_OK, null, array{}>
  58. * @throws NotLoggedInException Not logged in or not an admin user
  59. * @throws NotAdminException Not logged in or not an admin user
  60. * @throws OCSBadRequestException Invalid arguments to save value
  61. *
  62. * 200: Value set successfully
  63. */
  64. #[NoAdminRequired]
  65. public function setValue(string $app, string $formId, string $fieldId, mixed $value): DataResponse {
  66. $user = $this->userSession->getUser();
  67. if ($user === null) {
  68. throw new NotLoggedInException();
  69. }
  70. try {
  71. $this->declarativeManager->loadSchemas();
  72. $this->declarativeManager->setValue($user, $app, $formId, $fieldId, $value);
  73. return new DataResponse(null);
  74. } catch (NotAdminException $e) {
  75. throw $e;
  76. } catch (Exception $e) {
  77. $this->logger->error('Failed to set declarative settings value: ' . $e->getMessage());
  78. throw new OCSBadRequestException();
  79. }
  80. }
  81. /**
  82. * Gets all declarative forms with the values prefilled.
  83. *
  84. * @return DataResponse<Http::STATUS_OK, list<SettingsDeclarativeForm>, array{}>
  85. * @throws NotLoggedInException
  86. * @NoSubAdminRequired
  87. *
  88. * 200: Forms returned
  89. */
  90. #[NoAdminRequired]
  91. public function getForms(): DataResponse {
  92. $user = $this->userSession->getUser();
  93. if ($user === null) {
  94. throw new NotLoggedInException();
  95. }
  96. $this->declarativeManager->loadSchemas();
  97. return new DataResponse($this->declarativeManager->getFormsWithValues($user, null, null));
  98. }
  99. }