aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/lib/Controller
diff options
context:
space:
mode:
authorjld3103 <jld3103yt@gmail.com>2023-12-07 16:39:16 +0100
committerAndrey Borysenko <andrey18106x@gmail.com>2024-03-12 13:56:54 +0200
commit4ac2375ca2082750432ccc9cff46bf5888b4db30 (patch)
treebca24a21f4dfa0184f8e400e9508fc5600ade8d4 /apps/settings/lib/Controller
parentc42397358f05aa60ae91ed11e7754fddba182cce (diff)
downloadnextcloud-server-4ac2375ca2082750432ccc9cff46bf5888b4db30.tar.gz
nextcloud-server-4ac2375ca2082750432ccc9cff46bf5888b4db30.zip
feat: Add declarative settings
Signed-off-by: jld3103 <jld3103yt@gmail.com> Signed-off-by: Julien Veyssier <julien-nc@posteo.net> Signed-off-by: Andrey Borysenko <andrey18106x@gmail.com>
Diffstat (limited to 'apps/settings/lib/Controller')
-rw-r--r--apps/settings/lib/Controller/AdminSettingsController.php11
-rw-r--r--apps/settings/lib/Controller/CommonSettingsTrait.php63
-rw-r--r--apps/settings/lib/Controller/DeclarativeSettingsController.php105
-rw-r--r--apps/settings/lib/Controller/PersonalSettingsController.php8
4 files changed, 167 insertions, 20 deletions
diff --git a/apps/settings/lib/Controller/AdminSettingsController.php b/apps/settings/lib/Controller/AdminSettingsController.php
index 7b0313c9fa7..8da03607a79 100644
--- a/apps/settings/lib/Controller/AdminSettingsController.php
+++ b/apps/settings/lib/Controller/AdminSettingsController.php
@@ -30,12 +30,14 @@ use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\TemplateResponse;
+use OCP\AppFramework\Services\IInitialState;
use OCP\Group\ISubAdmin;
use OCP\IGroupManager;
use OCP\INavigationManager;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
+use OCP\Settings\IDeclarativeManager;
use OCP\Settings\IManager as ISettingsManager;
use OCP\Template;
@@ -50,7 +52,9 @@ class AdminSettingsController extends Controller {
ISettingsManager $settingsManager,
IUserSession $userSession,
IGroupManager $groupManager,
- ISubAdmin $subAdmin
+ ISubAdmin $subAdmin,
+ IDeclarativeManager $declarativeSettingsManager,
+ IInitialState $initialState,
) {
parent::__construct($appName, $request);
$this->navigationManager = $navigationManager;
@@ -58,6 +62,8 @@ class AdminSettingsController extends Controller {
$this->userSession = $userSession;
$this->groupManager = $groupManager;
$this->subAdmin = $subAdmin;
+ $this->declarativeSettingsManager = $declarativeSettingsManager;
+ $this->initialState = $initialState;
}
/**
@@ -80,7 +86,8 @@ class AdminSettingsController extends Controller {
$user = $this->userSession->getUser();
$isSubAdmin = !$this->groupManager->isAdmin($user->getUID()) && $this->subAdmin->isSubAdmin($user);
$settings = $this->settingsManager->getAllowedAdminSettings($section, $user);
- if (empty($settings)) {
+ $declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($user, 'admin', $section);
+ if (empty($settings) && empty($declarativeFormIDs)) {
throw new NotAdminException("Logged in user doesn't have permission to access these settings.");
}
$formatted = $this->formatSettings($settings);
diff --git a/apps/settings/lib/Controller/CommonSettingsTrait.php b/apps/settings/lib/Controller/CommonSettingsTrait.php
index 5d683d7d824..ab51deadfc3 100644
--- a/apps/settings/lib/Controller/CommonSettingsTrait.php
+++ b/apps/settings/lib/Controller/CommonSettingsTrait.php
@@ -26,17 +26,26 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
+
namespace OCA\Settings\Controller;
+use OCA\Settings\AppInfo\Application;
use OCP\AppFramework\Http\TemplateResponse;
+use OCP\AppFramework\Services\IInitialState;
use OCP\Group\ISubAdmin;
use OCP\IGroupManager;
use OCP\INavigationManager;
use OCP\IUserSession;
+use OCP\Settings\IDeclarativeManager;
+use OCP\Settings\IDeclarativeSettingsForm;
use OCP\Settings\IIconSection;
use OCP\Settings\IManager as ISettingsManager;
use OCP\Settings\ISettings;
+use OCP\Util;
+/**
+ * @psalm-import-type DeclarativeSettingsFormField from IDeclarativeSettingsForm
+ */
trait CommonSettingsTrait {
/** @var ISettingsManager */
@@ -54,28 +63,26 @@ trait CommonSettingsTrait {
/** @var ISubAdmin */
private $subAdmin;
+ private IDeclarativeManager $declarativeSettingsManager;
+
+ /** @var IInitialState */
+ private $initialState;
+
/**
* @return array{forms: array{personal: array, admin: array}}
*/
private function getNavigationParameters(string $currentType, string $currentSection): array {
- $templateParameters = [
- 'personal' => $this->formatPersonalSections($currentType, $currentSection),
- 'admin' => []
- ];
-
- $templateParameters['admin'] = $this->formatAdminSections(
- $currentType,
- $currentSection
- );
-
return [
- 'forms' => $templateParameters
+ 'forms' => [
+ 'personal' => $this->formatPersonalSections($currentType, $currentSection),
+ 'admin' => $this->formatAdminSections($currentType, $currentSection),
+ ],
];
}
/**
* @param IIconSection[][] $sections
- * @psam-param 'admin'|'personal' $type
+ * @psalm-param 'admin'|'personal' $type
* @return list<array{anchor: string, section-name: string, active: bool, icon: string}>
*/
protected function formatSections(array $sections, string $currentSection, string $type, string $currentType): array {
@@ -87,7 +94,11 @@ trait CommonSettingsTrait {
} elseif ($type === 'personal') {
$settings = $this->settingsManager->getPersonalSettings($section->getID());
}
- if (empty($settings) && !($section->getID() === 'additional' && count(\OC_App::getForms('admin')) > 0)) {
+
+ /** @psalm-suppress PossiblyNullArgument */
+ $declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($this->userSession->getUser(), $type, $section->getID());
+
+ if (empty($settings) && empty($declarativeFormIDs) && !($section->getID() === 'additional' && count(\OC_App::getForms('admin')) > 0)) {
continue;
}
@@ -107,14 +118,14 @@ trait CommonSettingsTrait {
return $templateParameters;
}
- protected function formatPersonalSections(string $currentType, string $currentSections): array {
+ protected function formatPersonalSections(string $currentType, string $currentSection): array {
$sections = $this->settingsManager->getPersonalSections();
- return $this->formatSections($sections, $currentSections, 'personal', $currentType);
+ return $this->formatSections($sections, $currentSection, 'personal', $currentType);
}
- protected function formatAdminSections(string $currentType, string $currentSections): array {
+ protected function formatAdminSections(string $currentType, string $currentSection): array {
$sections = $this->settingsManager->getAdminSections();
- return $this->formatSections($sections, $currentSections, 'admin', $currentType);
+ return $this->formatSections($sections, $currentSection, 'admin', $currentType);
}
/**
@@ -133,6 +144,9 @@ trait CommonSettingsTrait {
return ['content' => $html];
}
+ /**
+ * @psalm-param 'admin'|'personal' $type
+ */
private function getIndexResponse(string $type, string $section): TemplateResponse {
if ($type === 'personal') {
if ($section === 'theming') {
@@ -144,9 +158,24 @@ trait CommonSettingsTrait {
$this->navigationManager->setActiveEntry('admin_settings');
}
+ $this->declarativeSettingsManager->loadSchemas();
+
$templateParams = [];
$templateParams = array_merge($templateParams, $this->getNavigationParameters($type, $section));
$templateParams = array_merge($templateParams, $this->getSettings($section));
+
+ /** @psalm-suppress PossiblyNullArgument */
+ $declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($this->userSession->getUser(), $type, $section);
+ if (!empty($declarativeFormIDs)) {
+ foreach ($declarativeFormIDs as $app => $ids) {
+ /** @psalm-suppress PossiblyUndefinedArrayOffset */
+ $templateParams['content'] .= join(array_map(fn (string $id) => '<div id="' . $app . '_' . $id . '"></div>', $ids));
+ }
+ Util::addScript(Application::APP_ID, 'declarative-settings-forms');
+ /** @psalm-suppress PossiblyNullArgument */
+ $this->initialState->provideInitialState('declarative-settings-forms', $this->declarativeSettingsManager->getFormsWithValues($this->userSession->getUser(), $type, $section));
+ }
+
$activeSection = $this->settingsManager->getSection($type, $section);
if ($activeSection) {
$templateParams['pageTitle'] = $activeSection->getName();
diff --git a/apps/settings/lib/Controller/DeclarativeSettingsController.php b/apps/settings/lib/Controller/DeclarativeSettingsController.php
new file mode 100644
index 00000000000..71347066290
--- /dev/null
+++ b/apps/settings/lib/Controller/DeclarativeSettingsController.php
@@ -0,0 +1,105 @@
+<?php
+/**
+ * @copyright Copyright (c) 2023 Kate Döen <kate.doeen@nextcloud.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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Settings\Controller;
+
+use Exception;
+use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
+use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
+use OCA\Settings\ResponseDefinitions;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\Attribute\NoAdminRequired;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\OCS\OCSBadRequestException;
+use OCP\AppFramework\OCSController;
+use OCP\IRequest;
+use OCP\IUserSession;
+use OCP\Settings\IDeclarativeManager;
+use Psr\Log\LoggerInterface;
+
+/**
+ * @psalm-import-type SettingsDeclarativeForm from ResponseDefinitions
+ */
+class DeclarativeSettingsController extends OCSController {
+ public function __construct(
+ string $appName,
+ IRequest $request,
+ private IUserSession $userSession,
+ private IDeclarativeManager $declarativeManager,
+ private LoggerInterface $logger,
+ ) {
+ parent::__construct($appName, $request);
+ }
+
+ /**
+ * Sets a declarative settings value
+ *
+ * @param string $app ID of the app
+ * @param string $formId ID of the form
+ * @param string $fieldId ID of the field
+ * @param mixed $value Value to be saved
+ * @return DataResponse<Http::STATUS_OK, null, array{}>
+ * @throws NotLoggedInException Not logged in or not an admin user
+ * @throws NotAdminException Not logged in or not an admin user
+ * @throws OCSBadRequestException Invalid arguments to save value
+ *
+ * 200: Value set successfully
+ */
+ #[NoAdminRequired]
+ public function setValue(string $app, string $formId, string $fieldId, mixed $value): DataResponse {
+ $user = $this->userSession->getUser();
+ if ($user === null) {
+ throw new NotLoggedInException();
+ }
+
+ try {
+ $this->declarativeManager->loadSchemas();
+ $this->declarativeManager->setValue($user, $app, $formId, $fieldId, $value);
+ return new DataResponse(null);
+ } catch (NotAdminException $e) {
+ throw $e;
+ } catch (Exception $e) {
+ $this->logger->error('Failed to set declarative settings value: ' . $e->getMessage());
+ throw new OCSBadRequestException();
+ }
+ }
+
+ /**
+ * Gets all declarative forms with the values prefilled.
+ *
+ * @return DataResponse<Http::STATUS_OK, list<SettingsDeclarativeForm>, array{}>
+ * @throws NotLoggedInException
+ * @NoSubAdminRequired
+ *
+ * 200: Forms returned
+ */
+ #[NoAdminRequired]
+ public function getForms(): DataResponse {
+ $user = $this->userSession->getUser();
+ if ($user === null) {
+ throw new NotLoggedInException();
+ }
+ $this->declarativeManager->loadSchemas();
+ return new DataResponse($this->declarativeManager->getFormsWithValues($user, null, null));
+ }
+}
diff --git a/apps/settings/lib/Controller/PersonalSettingsController.php b/apps/settings/lib/Controller/PersonalSettingsController.php
index 7d219f5c165..57da74cd99a 100644
--- a/apps/settings/lib/Controller/PersonalSettingsController.php
+++ b/apps/settings/lib/Controller/PersonalSettingsController.php
@@ -29,11 +29,13 @@ namespace OCA\Settings\Controller;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\TemplateResponse;
+use OCP\AppFramework\Services\IInitialState;
use OCP\Group\ISubAdmin;
use OCP\IGroupManager;
use OCP\INavigationManager;
use OCP\IRequest;
use OCP\IUserSession;
+use OCP\Settings\IDeclarativeManager;
use OCP\Settings\IManager as ISettingsManager;
use OCP\Template;
@@ -48,7 +50,9 @@ class PersonalSettingsController extends Controller {
ISettingsManager $settingsManager,
IUserSession $userSession,
IGroupManager $groupManager,
- ISubAdmin $subAdmin
+ ISubAdmin $subAdmin,
+ IDeclarativeManager $declarativeSettingsManager,
+ IInitialState $initialState,
) {
parent::__construct($appName, $request);
$this->navigationManager = $navigationManager;
@@ -56,6 +60,8 @@ class PersonalSettingsController extends Controller {
$this->userSession = $userSession;
$this->subAdmin = $subAdmin;
$this->groupManager = $groupManager;
+ $this->declarativeSettingsManager = $declarativeSettingsManager;
+ $this->initialState = $initialState;
}
/**