Skip to content

Commit

Permalink
feat: Add declarative settings
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <jld3103yt@gmail.com>
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
Signed-off-by: Andrey Borysenko <andrey18106x@gmail.com>
  • Loading branch information
provokateurin authored and andrey18106 committed Feb 12, 2024
1 parent 35d0c63 commit 136a5e1
Show file tree
Hide file tree
Showing 39 changed files with 2,285 additions and 31 deletions.
2 changes: 2 additions & 0 deletions apps/settings/appinfo/routes.php
Expand Up @@ -81,5 +81,7 @@
['name' => 'WebAuthn#deleteRegistration', 'url' => '/settings/api/personal/webauthn/registration/{id}', 'verb' => 'DELETE' , 'root' => ''],

['name' => 'Reasons#getPdf', 'url' => '/settings/download/reasons', 'verb' => 'GET', 'root' => ''],

['name' => 'DeclarativeSettings#setValue', 'url' => '/settings/api/declarative', 'verb' => 'POST', 'root' => ''],
]
];
1 change: 1 addition & 0 deletions apps/settings/composer/composer/autoload_classmap.php
Expand Up @@ -27,6 +27,7 @@
'OCA\\Settings\\Controller\\ChangePasswordController' => $baseDir . '/../lib/Controller/ChangePasswordController.php',
'OCA\\Settings\\Controller\\CheckSetupController' => $baseDir . '/../lib/Controller/CheckSetupController.php',
'OCA\\Settings\\Controller\\CommonSettingsTrait' => $baseDir . '/../lib/Controller/CommonSettingsTrait.php',
'OCA\\Settings\\Controller\\DeclarativeSettingsController' => $baseDir . '/../lib/Controller/DeclarativeSettingsController.php',
'OCA\\Settings\\Controller\\HelpController' => $baseDir . '/../lib/Controller/HelpController.php',
'OCA\\Settings\\Controller\\LogSettingsController' => $baseDir . '/../lib/Controller/LogSettingsController.php',
'OCA\\Settings\\Controller\\MailSettingsController' => $baseDir . '/../lib/Controller/MailSettingsController.php',
Expand Down
1 change: 1 addition & 0 deletions apps/settings/composer/composer/autoload_static.php
Expand Up @@ -42,6 +42,7 @@ class ComposerStaticInitSettings
'OCA\\Settings\\Controller\\ChangePasswordController' => __DIR__ . '/..' . '/../lib/Controller/ChangePasswordController.php',
'OCA\\Settings\\Controller\\CheckSetupController' => __DIR__ . '/..' . '/../lib/Controller/CheckSetupController.php',
'OCA\\Settings\\Controller\\CommonSettingsTrait' => __DIR__ . '/..' . '/../lib/Controller/CommonSettingsTrait.php',
'OCA\\Settings\\Controller\\DeclarativeSettingsController' => __DIR__ . '/..' . '/../lib/Controller/DeclarativeSettingsController.php',
'OCA\\Settings\\Controller\\HelpController' => __DIR__ . '/..' . '/../lib/Controller/HelpController.php',
'OCA\\Settings\\Controller\\LogSettingsController' => __DIR__ . '/..' . '/../lib/Controller/LogSettingsController.php',
'OCA\\Settings\\Controller\\MailSettingsController' => __DIR__ . '/..' . '/../lib/Controller/MailSettingsController.php',
Expand Down
11 changes: 9 additions & 2 deletions apps/settings/lib/Controller/AdminSettingsController.php
Expand Up @@ -30,12 +30,14 @@
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;

Expand All @@ -50,14 +52,18 @@ public function __construct(
ISettingsManager $settingsManager,
IUserSession $userSession,
IGroupManager $groupManager,
ISubAdmin $subAdmin
ISubAdmin $subAdmin,
IDeclarativeManager $declarativeSettingsManager,
IInitialState $initialState,
) {
parent::__construct($appName, $request);
$this->navigationManager = $navigationManager;
$this->settingsManager = $settingsManager;
$this->userSession = $userSession;
$this->groupManager = $groupManager;
$this->subAdmin = $subAdmin;
$this->declarativeSettingsManager = $declarativeSettingsManager;
$this->initialState = $initialState;
}

/**
Expand All @@ -80,7 +86,8 @@ protected function getSettings($section) {
$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('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);
Expand Down
61 changes: 44 additions & 17 deletions apps/settings/lib/Controller/CommonSettingsTrait.php
Expand Up @@ -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 */
Expand All @@ -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 {
Expand All @@ -87,7 +94,10 @@ protected function formatSections(array $sections, string $currentSection, strin
} elseif ($type === 'personal') {
$settings = $this->settingsManager->getPersonalSettings($section->getID());
}
if (empty($settings) && !($section->getID() === 'additional' && count(\OC_App::getForms('admin')) > 0)) {

$declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($type, $section->getID());

if (empty($settings) && empty($declarativeFormIDs) && !($section->getID() === 'additional' && count(\OC_App::getForms('admin')) > 0)) {
continue;
}

Expand All @@ -107,14 +117,14 @@ protected function formatSections(array $sections, string $currentSection, strin
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);
}

/**
Expand All @@ -133,6 +143,9 @@ private function formatSettings(array $settings): array {
return ['content' => $html];
}

/**
* @psalm-param 'admin'|'personal' $type
*/
private function getIndexResponse(string $type, string $section): TemplateResponse {
if ($type === 'personal') {
if ($section === 'theming') {
Expand All @@ -144,9 +157,23 @@ private function getIndexResponse(string $type, string $section): TemplateRespon
$this->navigationManager->setActiveEntry('admin_settings');
}

$this->declarativeSettingsManager->loadSchemas();

$templateParams = [];
$templateParams = array_merge($templateParams, $this->getNavigationParameters($type, $section));
$templateParams = array_merge($templateParams, $this->getSettings($section));

$declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($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();
Expand Down
85 changes: 85 additions & 0 deletions apps/settings/lib/Controller/DeclarativeSettingsController.php
@@ -0,0 +1,85 @@
<?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 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;

/**
* @since 29.0.0
*/
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();
}
}
}
12 changes: 11 additions & 1 deletion apps/settings/lib/Controller/PersonalSettingsController.php
Expand Up @@ -26,14 +26,18 @@
*/
namespace OCA\Settings\Controller;

use OC\AppFramework\Bootstrap\Coordinator;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\IEventDispatcher;
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;

Expand All @@ -48,14 +52,20 @@ public function __construct(
ISettingsManager $settingsManager,
IUserSession $userSession,
IGroupManager $groupManager,
ISubAdmin $subAdmin
ISubAdmin $subAdmin,
Coordinator $coordinator,
IDeclarativeManager $declarativeSettingsManager,
IEventDispatcher $eventDispatcher,
IInitialState $initialState,
) {
parent::__construct($appName, $request);
$this->navigationManager = $navigationManager;
$this->settingsManager = $settingsManager;
$this->userSession = $userSession;
$this->subAdmin = $subAdmin;
$this->groupManager = $groupManager;
$this->declarativeSettingsManager = $declarativeSettingsManager;
$this->initialState = $initialState;
}

/**
Expand Down

0 comments on commit 136a5e1

Please sign in to comment.