2017-05-15 19:38:12 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2017-05-15 19:38:12 -04:00
|
|
|
/**
|
2024-06-03 04:23:34 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-05-15 19:38:12 -04:00
|
|
|
*/
|
2023-12-07 10:39:16 -05:00
|
|
|
|
2019-09-17 10:33:27 -04:00
|
|
|
namespace OCA\Settings\Controller;
|
2017-05-15 19:38:12 -04:00
|
|
|
|
2024-09-27 15:40:30 -04:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
|
2023-12-07 10:39:16 -05:00
|
|
|
use OCA\Settings\AppInfo\Application;
|
2017-05-15 19:38:12 -04:00
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2023-12-07 10:39:16 -05:00
|
|
|
use OCP\AppFramework\Services\IInitialState;
|
2019-05-22 04:48:51 -04:00
|
|
|
use OCP\Group\ISubAdmin;
|
|
|
|
|
use OCP\IGroupManager;
|
2018-03-05 11:06:28 -05:00
|
|
|
use OCP\INavigationManager;
|
2019-05-22 04:48:51 -04:00
|
|
|
use OCP\IUserSession;
|
2023-12-07 10:39:16 -05:00
|
|
|
use OCP\Settings\IDeclarativeManager;
|
|
|
|
|
use OCP\Settings\IDeclarativeSettingsForm;
|
2021-11-24 08:08:16 -05:00
|
|
|
use OCP\Settings\IIconSection;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\Settings\IManager as ISettingsManager;
|
2017-05-15 19:38:12 -04:00
|
|
|
use OCP\Settings\ISettings;
|
2023-12-07 10:39:16 -05:00
|
|
|
use OCP\Util;
|
2017-05-15 19:38:12 -04:00
|
|
|
|
2023-12-07 10:39:16 -05:00
|
|
|
/**
|
2024-09-27 15:40:30 -04:00
|
|
|
* @psalm-import-type DeclarativeSettingsFormSchemaWithValues from IDeclarativeSettingsForm
|
|
|
|
|
* @psalm-import-type DeclarativeSettingsFormSchemaWithoutValues from IDeclarativeSettingsForm
|
2023-12-07 10:39:16 -05:00
|
|
|
*/
|
2020-04-10 08:19:56 -04:00
|
|
|
trait CommonSettingsTrait {
|
2019-05-22 04:48:51 -04:00
|
|
|
|
2017-05-15 19:38:12 -04:00
|
|
|
/** @var ISettingsManager */
|
|
|
|
|
private $settingsManager;
|
|
|
|
|
|
2018-03-05 11:06:28 -05:00
|
|
|
/** @var INavigationManager */
|
|
|
|
|
private $navigationManager;
|
|
|
|
|
|
2019-05-22 04:48:51 -04:00
|
|
|
/** @var IUserSession */
|
|
|
|
|
private $userSession;
|
|
|
|
|
|
|
|
|
|
/** @var IGroupManager */
|
|
|
|
|
private $groupManager;
|
|
|
|
|
|
|
|
|
|
/** @var ISubAdmin */
|
|
|
|
|
private $subAdmin;
|
|
|
|
|
|
2023-12-07 10:39:16 -05:00
|
|
|
private IDeclarativeManager $declarativeSettingsManager;
|
|
|
|
|
|
|
|
|
|
/** @var IInitialState */
|
|
|
|
|
private $initialState;
|
|
|
|
|
|
2021-11-24 08:08:16 -05:00
|
|
|
/**
|
|
|
|
|
* @param IIconSection[][] $sections
|
2023-12-07 10:39:16 -05:00
|
|
|
* @psalm-param 'admin'|'personal' $type
|
2026-01-25 08:33:14 -05:00
|
|
|
* @return list<array{id: string, name: string, active: bool, icon: string}>
|
2021-11-24 08:08:16 -05:00
|
|
|
*/
|
2021-07-22 05:41:29 -04:00
|
|
|
protected function formatSections(array $sections, string $currentSection, string $type, string $currentType): array {
|
2017-05-15 19:38:12 -04:00
|
|
|
$templateParameters = [];
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($sections as $prioritizedSections) {
|
2017-05-15 19:38:12 -04:00
|
|
|
foreach ($prioritizedSections as $section) {
|
2020-04-10 08:19:56 -04:00
|
|
|
if ($type === 'admin') {
|
2021-07-22 05:41:29 -04:00
|
|
|
$settings = $this->settingsManager->getAllowedAdminSettings($section->getID(), $this->userSession->getUser());
|
2020-04-10 08:19:56 -04:00
|
|
|
} elseif ($type === 'personal') {
|
2017-05-15 19:38:12 -04:00
|
|
|
$settings = $this->settingsManager->getPersonalSettings($section->getID());
|
|
|
|
|
}
|
2023-12-07 10:39:16 -05:00
|
|
|
|
|
|
|
|
/** @psalm-suppress PossiblyNullArgument */
|
|
|
|
|
$declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($this->userSession->getUser(), $type, $section->getID());
|
|
|
|
|
|
2024-09-17 08:41:25 -04:00
|
|
|
if (empty($settings) && empty($declarativeFormIDs)) {
|
2017-05-15 19:38:12 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 17:51:49 -04:00
|
|
|
$icon = $section->getIcon();
|
2017-05-15 19:38:12 -04:00
|
|
|
|
2017-06-22 12:15:33 -04:00
|
|
|
$active = $section->getID() === $currentSection
|
|
|
|
|
&& $type === $currentType;
|
|
|
|
|
|
2017-05-15 19:38:12 -04:00
|
|
|
$templateParameters[] = [
|
2026-01-25 08:33:14 -05:00
|
|
|
'id' => $section->getID(),
|
|
|
|
|
'name' => $section->getName(),
|
2020-10-05 09:12:57 -04:00
|
|
|
'active' => $active,
|
|
|
|
|
'icon' => $icon,
|
2017-05-15 19:38:12 -04:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $templateParameters;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-25 08:33:14 -05:00
|
|
|
/**
|
|
|
|
|
* @return list<array{id: string, name: string, active: bool, icon: string}>
|
|
|
|
|
*/
|
2023-12-07 10:39:16 -05:00
|
|
|
protected function formatPersonalSections(string $currentType, string $currentSection): array {
|
2017-05-15 19:38:12 -04:00
|
|
|
$sections = $this->settingsManager->getPersonalSections();
|
2023-12-07 10:39:16 -05:00
|
|
|
return $this->formatSections($sections, $currentSection, 'personal', $currentType);
|
2017-05-15 19:38:12 -04:00
|
|
|
}
|
|
|
|
|
|
2026-01-25 08:33:14 -05:00
|
|
|
/**
|
|
|
|
|
* @return list<array{id: string, name: string, active: bool, icon: string}>
|
|
|
|
|
*/
|
2023-12-07 10:39:16 -05:00
|
|
|
protected function formatAdminSections(string $currentType, string $currentSection): array {
|
2017-05-15 19:38:12 -04:00
|
|
|
$sections = $this->settingsManager->getAdminSections();
|
2023-12-07 10:39:16 -05:00
|
|
|
return $this->formatSections($sections, $currentSection, 'admin', $currentType);
|
2017-05-15 19:38:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-10-18 06:04:22 -04:00
|
|
|
* @param list<ISettings> $settings
|
2024-09-27 15:40:30 -04:00
|
|
|
* @param list<DeclarativeSettingsFormSchemaWithValues> $declarativeSettings
|
2021-11-24 08:08:16 -05:00
|
|
|
* @return array{content: string}
|
2017-05-15 19:38:12 -04:00
|
|
|
*/
|
2024-09-27 15:40:30 -04:00
|
|
|
private function formatSettings(array $settings, array $declarativeSettings): array {
|
|
|
|
|
$settings = array_merge($settings, $declarativeSettings);
|
|
|
|
|
|
|
|
|
|
usort($settings, function ($first, $second) {
|
|
|
|
|
$priorityOne = $first instanceof ISettings ? $first->getPriority() : $first['priority'];
|
|
|
|
|
$priorityTwo = $second instanceof ISettings ? $second->getPriority() : $second['priority'];
|
|
|
|
|
return $priorityOne - $priorityTwo;
|
|
|
|
|
});
|
|
|
|
|
|
2017-05-15 19:38:12 -04:00
|
|
|
$html = '';
|
2024-09-27 15:40:30 -04:00
|
|
|
foreach ($settings as $setting) {
|
|
|
|
|
if ($setting instanceof ISettings) {
|
2017-05-15 19:38:12 -04:00
|
|
|
$form = $setting->getForm();
|
|
|
|
|
$html .= $form->renderAs('')->render();
|
2024-09-27 15:40:30 -04:00
|
|
|
} else {
|
|
|
|
|
$html .= '<div id="' . $setting['app'] . '_' . $setting['id'] . '"></div>';
|
2017-05-15 19:38:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ['content' => $html];
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 10:39:16 -05:00
|
|
|
/**
|
|
|
|
|
* @psalm-param 'admin'|'personal' $type
|
|
|
|
|
*/
|
2021-11-24 08:08:16 -05:00
|
|
|
private function getIndexResponse(string $type, string $section): TemplateResponse {
|
2024-09-27 15:40:30 -04:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
|
assert($user !== null, 'No user logged in for settings');
|
|
|
|
|
|
|
|
|
|
$this->declarativeSettingsManager->loadSchemas();
|
|
|
|
|
$declarativeSettings = $this->declarativeSettingsManager->getFormsWithValues($user, $type, $section);
|
|
|
|
|
|
2025-05-22 13:14:33 -04:00
|
|
|
foreach ($declarativeSettings as &$form) {
|
|
|
|
|
foreach ($form['fields'] as &$field) {
|
|
|
|
|
if (isset($field['sensitive']) && $field['sensitive'] === true && !empty($field['value'])) {
|
|
|
|
|
$field['value'] = 'dummySecret';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 05:07:25 -05:00
|
|
|
if ($type === 'personal') {
|
2024-09-27 15:40:30 -04:00
|
|
|
$settings = array_values($this->settingsManager->getPersonalSettings($section));
|
2022-09-14 05:24:11 -04:00
|
|
|
if ($section === 'theming') {
|
|
|
|
|
$this->navigationManager->setActiveEntry('accessibility_settings');
|
|
|
|
|
} else {
|
|
|
|
|
$this->navigationManager->setActiveEntry('settings');
|
|
|
|
|
}
|
2022-03-07 05:07:25 -05:00
|
|
|
} elseif ($type === 'admin') {
|
2024-09-27 15:40:30 -04:00
|
|
|
$settings = array_values($this->settingsManager->getAllowedAdminSettings($section, $user));
|
|
|
|
|
if (empty($settings) && empty($declarativeSettings)) {
|
|
|
|
|
throw new NotAdminException('Logged in user does not have permission to access these settings.');
|
|
|
|
|
}
|
2022-03-07 05:07:25 -05:00
|
|
|
$this->navigationManager->setActiveEntry('admin_settings');
|
2024-09-27 15:40:30 -04:00
|
|
|
} else {
|
|
|
|
|
throw new InvalidArgumentException('$type must be either "admin" or "personal"');
|
2022-03-07 05:07:25 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-27 15:40:30 -04:00
|
|
|
if (!empty($declarativeSettings)) {
|
2023-12-07 10:39:16 -05:00
|
|
|
Util::addScript(Application::APP_ID, 'declarative-settings-forms');
|
2024-09-27 15:40:30 -04:00
|
|
|
$this->initialState->provideInitialState('declarative-settings-forms', $declarativeSettings);
|
2023-12-07 10:39:16 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-25 08:33:14 -05:00
|
|
|
$this->initialState->provideInitialState('sections', [
|
|
|
|
|
'personal' => $this->formatPersonalSections($type, $section),
|
|
|
|
|
'admin' => $this->formatAdminSections($type, $section),
|
|
|
|
|
]);
|
|
|
|
|
|
2024-09-27 15:40:30 -04:00
|
|
|
$settings = array_merge(...$settings);
|
|
|
|
|
$templateParams = $this->formatSettings($settings, $declarativeSettings);
|
|
|
|
|
|
2021-11-24 08:08:16 -05:00
|
|
|
$activeSection = $this->settingsManager->getSection($type, $section);
|
|
|
|
|
if ($activeSection) {
|
|
|
|
|
$templateParams['pageTitle'] = $activeSection->getName();
|
2022-12-05 04:56:55 -05:00
|
|
|
$templateParams['activeSectionId'] = $activeSection->getID();
|
2024-01-05 03:45:50 -05:00
|
|
|
$templateParams['activeSectionType'] = $type;
|
2021-11-24 08:08:16 -05:00
|
|
|
}
|
2017-05-15 19:38:12 -04:00
|
|
|
|
2026-01-25 08:33:14 -05:00
|
|
|
Util::addScript(Application::APP_ID, 'main', prepend: true);
|
2017-05-15 19:38:12 -04:00
|
|
|
return new TemplateResponse('settings', 'settings/frame', $templateParams);
|
|
|
|
|
}
|
|
|
|
|
}
|