123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- declare(strict_types=1);
- /**
- * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
- *
- * @author Joas Schilling <coding@schilljs.com>
- * @author Kate Döen <kate.doeen@nextcloud.com>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- *
- */
- namespace OCA\Provisioning_API\Controller;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\AppFramework\OCSController;
- use OCP\Config\BeforePreferenceDeletedEvent;
- use OCP\Config\BeforePreferenceSetEvent;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\IConfig;
- use OCP\IRequest;
- use OCP\IUserSession;
- class PreferencesController extends OCSController {
- private IConfig $config;
- private IUserSession $userSession;
- private IEventDispatcher $eventDispatcher;
- public function __construct(
- string $appName,
- IRequest $request,
- IConfig $config,
- IUserSession $userSession,
- IEventDispatcher $eventDispatcher
- ) {
- parent::__construct($appName, $request);
- $this->config = $config;
- $this->userSession = $userSession;
- $this->eventDispatcher = $eventDispatcher;
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * Update multiple preference values of an app
- *
- * @param string $appId ID of the app
- * @param array<string, string> $configs Key-value pairs of the preferences
- *
- * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
- *
- * 200: Preferences updated successfully
- * 400: Preference invalid
- */
- public function setMultiplePreferences(string $appId, array $configs): DataResponse {
- $userId = $this->userSession->getUser()->getUID();
- foreach ($configs as $configKey => $configValue) {
- $event = new BeforePreferenceSetEvent(
- $userId,
- $appId,
- $configKey,
- $configValue
- );
- $this->eventDispatcher->dispatchTyped($event);
- if (!$event->isValid()) {
- // No listener validated that the preference can be set (to this value)
- return new DataResponse([], Http::STATUS_BAD_REQUEST);
- }
- }
- foreach ($configs as $configKey => $configValue) {
- $this->config->setUserValue(
- $userId,
- $appId,
- $configKey,
- $configValue
- );
- }
- return new DataResponse();
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * Update a preference value of an app
- *
- * @param string $appId ID of the app
- * @param string $configKey Key of the preference
- * @param string $configValue New value
- * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
- *
- * 200: Preference updated successfully
- * 400: Preference invalid
- */
- public function setPreference(string $appId, string $configKey, string $configValue): DataResponse {
- $userId = $this->userSession->getUser()->getUID();
- $event = new BeforePreferenceSetEvent(
- $userId,
- $appId,
- $configKey,
- $configValue
- );
- $this->eventDispatcher->dispatchTyped($event);
- if (!$event->isValid()) {
- // No listener validated that the preference can be set (to this value)
- return new DataResponse([], Http::STATUS_BAD_REQUEST);
- }
- $this->config->setUserValue(
- $userId,
- $appId,
- $configKey,
- $configValue
- );
- return new DataResponse();
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * Delete multiple preferences for an app
- *
- * @param string $appId ID of the app
- * @param string[] $configKeys Keys to delete
- *
- * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
- * 200: Preferences deleted successfully
- * 400: Preference invalid
- */
- public function deleteMultiplePreference(string $appId, array $configKeys): DataResponse {
- $userId = $this->userSession->getUser()->getUID();
- foreach ($configKeys as $configKey) {
- $event = new BeforePreferenceDeletedEvent(
- $userId,
- $appId,
- $configKey
- );
- $this->eventDispatcher->dispatchTyped($event);
- if (!$event->isValid()) {
- // No listener validated that the preference can be deleted
- return new DataResponse([], Http::STATUS_BAD_REQUEST);
- }
- }
- foreach ($configKeys as $configKey) {
- $this->config->deleteUserValue(
- $userId,
- $appId,
- $configKey
- );
- }
- return new DataResponse();
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * Delete a preference for an app
- *
- * @param string $appId ID of the app
- * @param string $configKey Key to delete
- * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST, array<empty>, array{}>
- *
- * 200: Preference deleted successfully
- * 400: Preference invalid
- */
- public function deletePreference(string $appId, string $configKey): DataResponse {
- $userId = $this->userSession->getUser()->getUID();
- $event = new BeforePreferenceDeletedEvent(
- $userId,
- $appId,
- $configKey
- );
- $this->eventDispatcher->dispatchTyped($event);
- if (!$event->isValid()) {
- // No listener validated that the preference can be deleted
- return new DataResponse([], Http::STATUS_BAD_REQUEST);
- }
- $this->config->deleteUserValue(
- $userId,
- $appId,
- $configKey
- );
- return new DataResponse();
- }
- }
|