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

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