mirror of https://github.com/nextcloud/server.git
Add API to change preferences
Signed-off-by: Joas Schilling <coding@schilljs.com>pull/32560/head
parent
8d599c3410
commit
31dc9aa682
@ -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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue