PreferencesController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Provisioning_API\Controller;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\AppFramework\OCSController;
  12. use OCP\Config\BeforePreferenceDeletedEvent;
  13. use OCP\Config\BeforePreferenceSetEvent;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\IConfig;
  16. use OCP\IRequest;
  17. use OCP\IUserSession;
  18. class PreferencesController extends OCSController {
  19. public function __construct(
  20. string $appName,
  21. IRequest $request,
  22. private IConfig $config,
  23. private IUserSession $userSession,
  24. private IEventDispatcher $eventDispatcher,
  25. ) {
  26. parent::__construct($appName, $request);
  27. }
  28. /**
  29. * @NoSubAdminRequired
  30. *
  31. * Update multiple preference values of an app
  32. *
  33. * @param string $appId ID of the app
  34. * @param array<string, string> $configs Key-value pairs of the preferences
  35. *
  36. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, list<empty>, array{}>
  37. *
  38. * 200: Preferences updated successfully
  39. * 400: Preference invalid
  40. */
  41. #[NoAdminRequired]
  42. public function setMultiplePreferences(string $appId, array $configs): DataResponse {
  43. $userId = $this->userSession->getUser()->getUID();
  44. foreach ($configs as $configKey => $configValue) {
  45. $event = new BeforePreferenceSetEvent(
  46. $userId,
  47. $appId,
  48. $configKey,
  49. $configValue
  50. );
  51. $this->eventDispatcher->dispatchTyped($event);
  52. if (!$event->isValid()) {
  53. // No listener validated that the preference can be set (to this value)
  54. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  55. }
  56. }
  57. foreach ($configs as $configKey => $configValue) {
  58. $this->config->setUserValue(
  59. $userId,
  60. $appId,
  61. $configKey,
  62. $configValue
  63. );
  64. }
  65. return new DataResponse();
  66. }
  67. /**
  68. * @NoSubAdminRequired
  69. *
  70. * Update a preference value of an app
  71. *
  72. * @param string $appId ID of the app
  73. * @param string $configKey Key of the preference
  74. * @param string $configValue New value
  75. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, list<empty>, array{}>
  76. *
  77. * 200: Preference updated successfully
  78. * 400: Preference invalid
  79. */
  80. #[NoAdminRequired]
  81. public function setPreference(string $appId, string $configKey, string $configValue): DataResponse {
  82. $userId = $this->userSession->getUser()->getUID();
  83. $event = new BeforePreferenceSetEvent(
  84. $userId,
  85. $appId,
  86. $configKey,
  87. $configValue
  88. );
  89. $this->eventDispatcher->dispatchTyped($event);
  90. if (!$event->isValid()) {
  91. // No listener validated that the preference can be set (to this value)
  92. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  93. }
  94. $this->config->setUserValue(
  95. $userId,
  96. $appId,
  97. $configKey,
  98. $configValue
  99. );
  100. return new DataResponse();
  101. }
  102. /**
  103. * @NoSubAdminRequired
  104. *
  105. * Delete multiple preferences for an app
  106. *
  107. * @param string $appId ID of the app
  108. * @param list<string> $configKeys Keys to delete
  109. *
  110. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, list<empty>, array{}>
  111. *
  112. * 200: Preferences deleted successfully
  113. * 400: Preference invalid
  114. */
  115. #[NoAdminRequired]
  116. public function deleteMultiplePreference(string $appId, array $configKeys): DataResponse {
  117. $userId = $this->userSession->getUser()->getUID();
  118. foreach ($configKeys as $configKey) {
  119. $event = new BeforePreferenceDeletedEvent(
  120. $userId,
  121. $appId,
  122. $configKey
  123. );
  124. $this->eventDispatcher->dispatchTyped($event);
  125. if (!$event->isValid()) {
  126. // No listener validated that the preference can be deleted
  127. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  128. }
  129. }
  130. foreach ($configKeys as $configKey) {
  131. $this->config->deleteUserValue(
  132. $userId,
  133. $appId,
  134. $configKey
  135. );
  136. }
  137. return new DataResponse();
  138. }
  139. /**
  140. * @NoSubAdminRequired
  141. *
  142. * Delete a preference for an app
  143. *
  144. * @param string $appId ID of the app
  145. * @param string $configKey Key to delete
  146. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, list<empty>, array{}>
  147. *
  148. * 200: Preference deleted successfully
  149. * 400: Preference invalid
  150. */
  151. #[NoAdminRequired]
  152. public function deletePreference(string $appId, string $configKey): DataResponse {
  153. $userId = $this->userSession->getUser()->getUID();
  154. $event = new BeforePreferenceDeletedEvent(
  155. $userId,
  156. $appId,
  157. $configKey
  158. );
  159. $this->eventDispatcher->dispatchTyped($event);
  160. if (!$event->isValid()) {
  161. // No listener validated that the preference can be deleted
  162. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  163. }
  164. $this->config->deleteUserValue(
  165. $userId,
  166. $appId,
  167. $configKey
  168. );
  169. return new DataResponse();
  170. }
  171. }