Skip to content

Commit

Permalink
Merge pull request #42661 from nextcloud/feat/settings/declarative
Browse files Browse the repository at this point in the history
  • Loading branch information
Altahrim authored Mar 12, 2024
2 parents f2f0442 + 865fd3b commit 92b97ec
Show file tree
Hide file tree
Showing 55 changed files with 3,326 additions and 70 deletions.
6 changes: 5 additions & 1 deletion apps/settings/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,9 @@
['name' => 'WebAuthn#deleteRegistration', 'url' => '/settings/api/personal/webauthn/registration/{id}', 'verb' => 'DELETE' , 'root' => ''],

['name' => 'Reasons#getPdf', 'url' => '/settings/download/reasons', 'verb' => 'GET', 'root' => ''],
]
],
'ocs' => [
['name' => 'DeclarativeSettings#setValue', 'url' => '/settings/api/declarative/value', 'verb' => 'POST', 'root' => ''],
['name' => 'DeclarativeSettings#getForms', 'url' => '/settings/api/declarative/forms', 'verb' => 'GET', 'root' => ''],
],
];
2 changes: 2 additions & 0 deletions apps/settings/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
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 All @@ -43,6 +44,7 @@
'OCA\\Settings\\Listener\\UserRemovedFromGroupActivityListener' => $baseDir . '/../lib/Listener/UserRemovedFromGroupActivityListener.php',
'OCA\\Settings\\Mailer\\NewUserMailHelper' => $baseDir . '/../lib/Mailer/NewUserMailHelper.php',
'OCA\\Settings\\Middleware\\SubadminMiddleware' => $baseDir . '/../lib/Middleware/SubadminMiddleware.php',
'OCA\\Settings\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php',
'OCA\\Settings\\Search\\AppSearch' => $baseDir . '/../lib/Search/AppSearch.php',
'OCA\\Settings\\Search\\SectionSearch' => $baseDir . '/../lib/Search/SectionSearch.php',
'OCA\\Settings\\Search\\UserSearch' => $baseDir . '/../lib/Search/UserSearch.php',
Expand Down
2 changes: 2 additions & 0 deletions apps/settings/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
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 All @@ -58,6 +59,7 @@ class ComposerStaticInitSettings
'OCA\\Settings\\Listener\\UserRemovedFromGroupActivityListener' => __DIR__ . '/..' . '/../lib/Listener/UserRemovedFromGroupActivityListener.php',
'OCA\\Settings\\Mailer\\NewUserMailHelper' => __DIR__ . '/..' . '/../lib/Mailer/NewUserMailHelper.php',
'OCA\\Settings\\Middleware\\SubadminMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/SubadminMiddleware.php',
'OCA\\Settings\\ResponseDefinitions' => __DIR__ . '/..' . '/../lib/ResponseDefinitions.php',
'OCA\\Settings\\Search\\AppSearch' => __DIR__ . '/..' . '/../lib/Search/AppSearch.php',
'OCA\\Settings\\Search\\SectionSearch' => __DIR__ . '/..' . '/../lib/Search/SectionSearch.php',
'OCA\\Settings\\Search\\UserSearch' => __DIR__ . '/..' . '/../lib/Search/UserSearch.php',
Expand Down
4 changes: 2 additions & 2 deletions apps/settings/composer/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'b1797842784b250fb01ed5e3bf130705eb94751b',
'reference' => '4ff660ca2e0baa02440ba07296ed7e75fa544c0e',
'type' => 'library',
'install_path' => __DIR__ . '/../',
'aliases' => array(),
Expand All @@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'b1797842784b250fb01ed5e3bf130705eb94751b',
'reference' => '4ff660ca2e0baa02440ba07296ed7e75fa544c0e',
'type' => 'library',
'install_path' => __DIR__ . '/../',
'aliases' => array(),
Expand Down
11 changes: 9 additions & 2 deletions apps/settings/lib/Controller/AdminSettingsController.php
Original file line number Diff line number Diff line change
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($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);
Expand Down
63 changes: 46 additions & 17 deletions apps/settings/lib/Controller/CommonSettingsTrait.php
Original file line number Diff line number Diff line change
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,11 @@ 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)) {

/** @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;
}

Expand All @@ -107,14 +118,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 +144,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 +158,24 @@ 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));

/** @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();
Expand Down
108 changes: 108 additions & 0 deletions apps/settings/lib/Controller/DeclarativeSettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Kate Döen <[email protected]>
*
* @author Kate Döen <[email protected]>
*
* @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));
}
}
8 changes: 7 additions & 1 deletion apps/settings/lib/Controller/PersonalSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
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;

Expand All @@ -48,14 +50,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->subAdmin = $subAdmin;
$this->groupManager = $groupManager;
$this->declarativeSettingsManager = $declarativeSettingsManager;
$this->initialState = $initialState;
}

/**
Expand Down
Loading

0 comments on commit 92b97ec

Please sign in to comment.