PreferencesController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. *
  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 <https://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Provisioning_API\Controller;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\DataResponse;
  27. use OCP\AppFramework\OCSController;
  28. use OCP\Config\BeforePreferenceDeletedEvent;
  29. use OCP\Config\BeforePreferenceSetEvent;
  30. use OCP\EventDispatcher\IEventDispatcher;
  31. use OCP\IConfig;
  32. use OCP\IRequest;
  33. use OCP\IUserSession;
  34. class PreferencesController extends OCSController {
  35. private IConfig $config;
  36. private IUserSession $userSession;
  37. private IEventDispatcher $eventDispatcher;
  38. public function __construct(
  39. string $appName,
  40. IRequest $request,
  41. IConfig $config,
  42. IUserSession $userSession,
  43. IEventDispatcher $eventDispatcher
  44. ) {
  45. parent::__construct($appName, $request);
  46. $this->config = $config;
  47. $this->userSession = $userSession;
  48. $this->eventDispatcher = $eventDispatcher;
  49. }
  50. /**
  51. * @NoAdminRequired
  52. * @NoSubAdminRequired
  53. */
  54. public function setMultiplePreferences(string $appId, array $configs): DataResponse {
  55. $userId = $this->userSession->getUser()->getUID();
  56. foreach ($configs as $configKey => $configValue) {
  57. $event = new BeforePreferenceSetEvent(
  58. $userId,
  59. $appId,
  60. $configKey,
  61. $configValue
  62. );
  63. $this->eventDispatcher->dispatchTyped($event);
  64. if (!$event->isValid()) {
  65. // No listener validated that the preference can be set (to this value)
  66. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  67. }
  68. }
  69. foreach ($configs as $configKey => $configValue) {
  70. $this->config->setUserValue(
  71. $userId,
  72. $appId,
  73. $configKey,
  74. $configValue
  75. );
  76. }
  77. return new DataResponse();
  78. }
  79. /**
  80. * @NoAdminRequired
  81. * @NoSubAdminRequired
  82. */
  83. public function setPreference(string $appId, string $configKey, string $configValue): DataResponse {
  84. $userId = $this->userSession->getUser()->getUID();
  85. $event = new BeforePreferenceSetEvent(
  86. $userId,
  87. $appId,
  88. $configKey,
  89. $configValue
  90. );
  91. $this->eventDispatcher->dispatchTyped($event);
  92. if (!$event->isValid()) {
  93. // No listener validated that the preference can be set (to this value)
  94. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  95. }
  96. $this->config->setUserValue(
  97. $userId,
  98. $appId,
  99. $configKey,
  100. $configValue
  101. );
  102. return new DataResponse();
  103. }
  104. /**
  105. * @NoAdminRequired
  106. * @NoSubAdminRequired
  107. */
  108. public function deleteMultiplePreference(string $appId, array $configKeys): DataResponse {
  109. $userId = $this->userSession->getUser()->getUID();
  110. foreach ($configKeys as $configKey) {
  111. $event = new BeforePreferenceDeletedEvent(
  112. $userId,
  113. $appId,
  114. $configKey
  115. );
  116. $this->eventDispatcher->dispatchTyped($event);
  117. if (!$event->isValid()) {
  118. // No listener validated that the preference can be deleted
  119. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  120. }
  121. }
  122. foreach ($configKeys as $configKey) {
  123. $this->config->deleteUserValue(
  124. $userId,
  125. $appId,
  126. $configKey
  127. );
  128. }
  129. return new DataResponse();
  130. }
  131. /**
  132. * @NoAdminRequired
  133. * @NoSubAdminRequired
  134. */
  135. public function deletePreference(string $appId, string $configKey): DataResponse {
  136. $userId = $this->userSession->getUser()->getUID();
  137. $event = new BeforePreferenceDeletedEvent(
  138. $userId,
  139. $appId,
  140. $configKey
  141. );
  142. $this->eventDispatcher->dispatchTyped($event);
  143. if (!$event->isValid()) {
  144. // No listener validated that the preference can be deleted
  145. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  146. }
  147. $this->config->deleteUserValue(
  148. $userId,
  149. $appId,
  150. $configKey
  151. );
  152. return new DataResponse();
  153. }
  154. }