1
0

PreferencesController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Kate Döen <kate.doeen@nextcloud.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Provisioning_API\Controller;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\AppFramework\OCSController;
  29. use OCP\Config\BeforePreferenceDeletedEvent;
  30. use OCP\Config\BeforePreferenceSetEvent;
  31. use OCP\EventDispatcher\IEventDispatcher;
  32. use OCP\IConfig;
  33. use OCP\IRequest;
  34. use OCP\IUserSession;
  35. class PreferencesController extends OCSController {
  36. private IConfig $config;
  37. private IUserSession $userSession;
  38. private IEventDispatcher $eventDispatcher;
  39. public function __construct(
  40. string $appName,
  41. IRequest $request,
  42. IConfig $config,
  43. IUserSession $userSession,
  44. IEventDispatcher $eventDispatcher
  45. ) {
  46. parent::__construct($appName, $request);
  47. $this->config = $config;
  48. $this->userSession = $userSession;
  49. $this->eventDispatcher = $eventDispatcher;
  50. }
  51. /**
  52. * @NoAdminRequired
  53. * @NoSubAdminRequired
  54. *
  55. * Update multiple preference values of an app
  56. *
  57. * @param string $appId ID of the app
  58. * @param array<string, string> $configs Key-value pairs of the preferences
  59. *
  60. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
  61. *
  62. * 200: Preferences updated successfully
  63. * 400: Preference invalid
  64. */
  65. public function setMultiplePreferences(string $appId, array $configs): DataResponse {
  66. $userId = $this->userSession->getUser()->getUID();
  67. foreach ($configs as $configKey => $configValue) {
  68. $event = new BeforePreferenceSetEvent(
  69. $userId,
  70. $appId,
  71. $configKey,
  72. $configValue
  73. );
  74. $this->eventDispatcher->dispatchTyped($event);
  75. if (!$event->isValid()) {
  76. // No listener validated that the preference can be set (to this value)
  77. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  78. }
  79. }
  80. foreach ($configs as $configKey => $configValue) {
  81. $this->config->setUserValue(
  82. $userId,
  83. $appId,
  84. $configKey,
  85. $configValue
  86. );
  87. }
  88. return new DataResponse();
  89. }
  90. /**
  91. * @NoAdminRequired
  92. * @NoSubAdminRequired
  93. *
  94. * Update a preference value of an app
  95. *
  96. * @param string $appId ID of the app
  97. * @param string $configKey Key of the preference
  98. * @param string $configValue New value
  99. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
  100. *
  101. * 200: Preference updated successfully
  102. * 400: Preference invalid
  103. */
  104. public function setPreference(string $appId, string $configKey, string $configValue): DataResponse {
  105. $userId = $this->userSession->getUser()->getUID();
  106. $event = new BeforePreferenceSetEvent(
  107. $userId,
  108. $appId,
  109. $configKey,
  110. $configValue
  111. );
  112. $this->eventDispatcher->dispatchTyped($event);
  113. if (!$event->isValid()) {
  114. // No listener validated that the preference can be set (to this value)
  115. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  116. }
  117. $this->config->setUserValue(
  118. $userId,
  119. $appId,
  120. $configKey,
  121. $configValue
  122. );
  123. return new DataResponse();
  124. }
  125. /**
  126. * @NoAdminRequired
  127. * @NoSubAdminRequired
  128. *
  129. * Delete multiple preferences for an app
  130. *
  131. * @param string $appId ID of the app
  132. * @param string[] $configKeys Keys to delete
  133. *
  134. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
  135. * 200: Preferences deleted successfully
  136. * 400: Preference invalid
  137. */
  138. public function deleteMultiplePreference(string $appId, array $configKeys): DataResponse {
  139. $userId = $this->userSession->getUser()->getUID();
  140. foreach ($configKeys as $configKey) {
  141. $event = new BeforePreferenceDeletedEvent(
  142. $userId,
  143. $appId,
  144. $configKey
  145. );
  146. $this->eventDispatcher->dispatchTyped($event);
  147. if (!$event->isValid()) {
  148. // No listener validated that the preference can be deleted
  149. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  150. }
  151. }
  152. foreach ($configKeys as $configKey) {
  153. $this->config->deleteUserValue(
  154. $userId,
  155. $appId,
  156. $configKey
  157. );
  158. }
  159. return new DataResponse();
  160. }
  161. /**
  162. * @NoAdminRequired
  163. * @NoSubAdminRequired
  164. *
  165. * Delete a preference for an app
  166. *
  167. * @param string $appId ID of the app
  168. * @param string $configKey Key to delete
  169. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
  170. *
  171. * 200: Preference deleted successfully
  172. * 400: Preference invalid
  173. */
  174. public function deletePreference(string $appId, string $configKey): DataResponse {
  175. $userId = $this->userSession->getUser()->getUID();
  176. $event = new BeforePreferenceDeletedEvent(
  177. $userId,
  178. $appId,
  179. $configKey
  180. );
  181. $this->eventDispatcher->dispatchTyped($event);
  182. if (!$event->isValid()) {
  183. // No listener validated that the preference can be deleted
  184. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  185. }
  186. $this->config->deleteUserValue(
  187. $userId,
  188. $appId,
  189. $configKey
  190. );
  191. return new DataResponse();
  192. }
  193. }