Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2022-05-23 17:28:25 +0300
committerJoas Schilling <coding@schilljs.com>2022-05-23 17:44:23 +0300
commit31dc9aa682952f6228e13a2c33de5492a8cfa7bb (patch)
treefd7a0d2b184fa47ca64015cd7b25b536d7a9b721
parent8d599c341023c5fe850da9bdbc3807a277a61151 (diff)
Add API to change preferencesfeature/noid/preferences-api
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r--apps/provisioning_api/appinfo/routes.php6
-rw-r--r--apps/provisioning_api/composer/composer/autoload_classmap.php1
-rw-r--r--apps/provisioning_api/composer/composer/autoload_static.php1
-rw-r--r--apps/provisioning_api/lib/Controller/PreferencesController.php185
-rw-r--r--lib/composer/composer/autoload_classmap.php2
-rw-r--r--lib/composer/composer/autoload_static.php2
-rw-r--r--lib/public/Config/BeforePreferenceDeletedEvent.php83
-rw-r--r--lib/public/Config/BeforePreferenceSetEvent.php92
8 files changed, 372 insertions, 0 deletions
diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php
index 54d550260b8..ab404ff8459 100644
--- a/apps/provisioning_api/appinfo/routes.php
+++ b/apps/provisioning_api/appinfo/routes.php
@@ -73,6 +73,12 @@ return [
['name' => 'AppConfig#getValue', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'GET'],
['name' => 'AppConfig#setValue', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'POST'],
['name' => 'AppConfig#deleteKey', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'DELETE'],
+
+ // Preferences
+ ['name' => 'Preferences#setPreference', 'url' => '/api/v1/config/users/{appId}/{configKey}', 'verb' => 'POST'],
+ ['name' => 'Preferences#setMultiplePreferences', 'url' => '/api/v1/config/users/{appId}', 'verb' => 'POST'],
+ ['name' => 'Preferences#deletePreference', 'url' => '/api/v1/config/users/{appId}/{configKey}', 'verb' => 'DELETE'],
+ ['name' => 'Preferences#deleteMultiplePreference', 'url' => '/api/v1/config/users/{appId}', 'verb' => 'DELETE'],
],
'routes' => [
// Verification
diff --git a/apps/provisioning_api/composer/composer/autoload_classmap.php b/apps/provisioning_api/composer/composer/autoload_classmap.php
index 7f840d39729..12c2daf7a14 100644
--- a/apps/provisioning_api/composer/composer/autoload_classmap.php
+++ b/apps/provisioning_api/composer/composer/autoload_classmap.php
@@ -13,6 +13,7 @@ return array(
'OCA\\Provisioning_API\\Controller\\AppConfigController' => $baseDir . '/../lib/Controller/AppConfigController.php',
'OCA\\Provisioning_API\\Controller\\AppsController' => $baseDir . '/../lib/Controller/AppsController.php',
'OCA\\Provisioning_API\\Controller\\GroupsController' => $baseDir . '/../lib/Controller/GroupsController.php',
+ 'OCA\\Provisioning_API\\Controller\\PreferencesController' => $baseDir . '/../lib/Controller/PreferencesController.php',
'OCA\\Provisioning_API\\Controller\\UsersController' => $baseDir . '/../lib/Controller/UsersController.php',
'OCA\\Provisioning_API\\Controller\\VerificationController' => $baseDir . '/../lib/Controller/VerificationController.php',
'OCA\\Provisioning_API\\FederatedShareProviderFactory' => $baseDir . '/../lib/FederatedShareProviderFactory.php',
diff --git a/apps/provisioning_api/composer/composer/autoload_static.php b/apps/provisioning_api/composer/composer/autoload_static.php
index 6dbf6b45c79..5aadb1574b3 100644
--- a/apps/provisioning_api/composer/composer/autoload_static.php
+++ b/apps/provisioning_api/composer/composer/autoload_static.php
@@ -28,6 +28,7 @@ class ComposerStaticInitProvisioning_API
'OCA\\Provisioning_API\\Controller\\AppConfigController' => __DIR__ . '/..' . '/../lib/Controller/AppConfigController.php',
'OCA\\Provisioning_API\\Controller\\AppsController' => __DIR__ . '/..' . '/../lib/Controller/AppsController.php',
'OCA\\Provisioning_API\\Controller\\GroupsController' => __DIR__ . '/..' . '/../lib/Controller/GroupsController.php',
+ 'OCA\\Provisioning_API\\Controller\\PreferencesController' => __DIR__ . '/..' . '/../lib/Controller/PreferencesController.php',
'OCA\\Provisioning_API\\Controller\\UsersController' => __DIR__ . '/..' . '/../lib/Controller/UsersController.php',
'OCA\\Provisioning_API\\Controller\\VerificationController' => __DIR__ . '/..' . '/../lib/Controller/VerificationController.php',
'OCA\\Provisioning_API\\FederatedShareProviderFactory' => __DIR__ . '/..' . '/../lib/FederatedShareProviderFactory.php',
diff --git a/apps/provisioning_api/lib/Controller/PreferencesController.php b/apps/provisioning_api/lib/Controller/PreferencesController.php
new file mode 100644
index 00000000000..2dba8b86eb6
--- /dev/null
+++ b/apps/provisioning_api/lib/Controller/PreferencesController.php
@@ -0,0 +1,185 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.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
+ */
+ 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
+ */
+ 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
+ */
+ 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
+ */
+ 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();
+ }
+}
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 520d2097918..d4e2cbe7c8e 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -160,6 +160,8 @@ return array(
'OCP\\Comments\\IllegalIDChangeException' => $baseDir . '/lib/public/Comments/IllegalIDChangeException.php',
'OCP\\Comments\\MessageTooLongException' => $baseDir . '/lib/public/Comments/MessageTooLongException.php',
'OCP\\Comments\\NotFoundException' => $baseDir . '/lib/public/Comments/NotFoundException.php',
+ 'OCP\\Config\\BeforePreferenceDeletedEvent' => $baseDir . '/lib/public/Config/BeforePreferenceDeletedEvent.php',
+ 'OCP\\Config\\BeforePreferenceSetEvent' => $baseDir . '/lib/public/Config/BeforePreferenceSetEvent.php',
'OCP\\Console\\ConsoleEvent' => $baseDir . '/lib/public/Console/ConsoleEvent.php',
'OCP\\Constants' => $baseDir . '/lib/public/Constants.php',
'OCP\\Contacts\\ContactsMenu\\IAction' => $baseDir . '/lib/public/Contacts/ContactsMenu/IAction.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 294969211ec..d1997b2b785 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -189,6 +189,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Comments\\IllegalIDChangeException' => __DIR__ . '/../../..' . '/lib/public/Comments/IllegalIDChangeException.php',
'OCP\\Comments\\MessageTooLongException' => __DIR__ . '/../../..' . '/lib/public/Comments/MessageTooLongException.php',
'OCP\\Comments\\NotFoundException' => __DIR__ . '/../../..' . '/lib/public/Comments/NotFoundException.php',
+ 'OCP\\Config\\BeforePreferenceDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceDeletedEvent.php',
+ 'OCP\\Config\\BeforePreferenceSetEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceSetEvent.php',
'OCP\\Console\\ConsoleEvent' => __DIR__ . '/../../..' . '/lib/public/Console/ConsoleEvent.php',
'OCP\\Constants' => __DIR__ . '/../../..' . '/lib/public/Constants.php',
'OCP\\Contacts\\ContactsMenu\\IAction' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/IAction.php',
diff --git a/lib/public/Config/BeforePreferenceDeletedEvent.php b/lib/public/Config/BeforePreferenceDeletedEvent.php
new file mode 100644
index 00000000000..a2d1dad7034
--- /dev/null
+++ b/lib/public/Config/BeforePreferenceDeletedEvent.php
@@ -0,0 +1,83 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\Config;
+
+use OCP\EventDispatcher\Event;
+
+/**
+ * @since 25.0.0
+ */
+class BeforePreferenceDeletedEvent extends Event {
+ protected string $userId;
+ protected string $appId;
+ protected string $configKey;
+ protected bool $valid = false;
+
+ /**
+ * @since 25.0.0
+ */
+ public function __construct(string $userId, string $appId, string $configKey) {
+ parent::__construct();
+ $this->userId = $userId;
+ $this->appId = $appId;
+ $this->configKey = $configKey;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function getUserId(): string {
+ return $this->userId;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function getAppId(): string {
+ return $this->appId;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function getConfigKey(): string {
+ return $this->configKey;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function isValid(): bool {
+ return $this->valid;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function setValid(bool $valid): void {
+ $this->valid = $valid;
+ }
+}
diff --git a/lib/public/Config/BeforePreferenceSetEvent.php b/lib/public/Config/BeforePreferenceSetEvent.php
new file mode 100644
index 00000000000..31681405a47
--- /dev/null
+++ b/lib/public/Config/BeforePreferenceSetEvent.php
@@ -0,0 +1,92 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\Config;
+
+use OCP\EventDispatcher\Event;
+
+/**
+ * @since 25.0.0
+ */
+class BeforePreferenceSetEvent extends Event {
+ protected string $userId;
+ protected string $appId;
+ protected string $configKey;
+ protected string $configValue;
+ protected bool $valid = false;
+
+ /**
+ * @since 25.0.0
+ */
+ public function __construct(string $userId, string $appId, string $configKey, string $configValue) {
+ parent::__construct();
+ $this->userId = $userId;
+ $this->appId = $appId;
+ $this->configKey = $configKey;
+ $this->configValue = $configValue;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function getUserId(): string {
+ return $this->userId;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function getAppId(): string {
+ return $this->appId;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function getConfigKey(): string {
+ return $this->configKey;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function getConfigValue(): string {
+ return $this->configValue;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function isValid(): bool {
+ return $this->valid;
+ }
+
+ /**
+ * @since 25.0.0
+ */
+ public function setValid(bool $valid): void {
+ $this->valid = $valid;
+ }
+}