PreferencesController.php 4.8 KB

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