CommonSettingsTrait.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings\Controller;
  7. use InvalidArgumentException;
  8. use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
  9. use OCA\Settings\AppInfo\Application;
  10. use OCP\AppFramework\Http\TemplateResponse;
  11. use OCP\AppFramework\Services\IInitialState;
  12. use OCP\Group\ISubAdmin;
  13. use OCP\IGroupManager;
  14. use OCP\INavigationManager;
  15. use OCP\IUserSession;
  16. use OCP\Settings\IDeclarativeManager;
  17. use OCP\Settings\IDeclarativeSettingsForm;
  18. use OCP\Settings\IIconSection;
  19. use OCP\Settings\IManager as ISettingsManager;
  20. use OCP\Settings\ISettings;
  21. use OCP\Util;
  22. /**
  23. * @psalm-import-type DeclarativeSettingsFormSchemaWithValues from IDeclarativeSettingsForm
  24. * @psalm-import-type DeclarativeSettingsFormSchemaWithoutValues from IDeclarativeSettingsForm
  25. */
  26. trait CommonSettingsTrait {
  27. /** @var ISettingsManager */
  28. private $settingsManager;
  29. /** @var INavigationManager */
  30. private $navigationManager;
  31. /** @var IUserSession */
  32. private $userSession;
  33. /** @var IGroupManager */
  34. private $groupManager;
  35. /** @var ISubAdmin */
  36. private $subAdmin;
  37. private IDeclarativeManager $declarativeSettingsManager;
  38. /** @var IInitialState */
  39. private $initialState;
  40. /**
  41. * @return array{forms: array{personal: array, admin: array}}
  42. */
  43. private function getNavigationParameters(string $currentType, string $currentSection): array {
  44. return [
  45. 'forms' => [
  46. 'personal' => $this->formatPersonalSections($currentType, $currentSection),
  47. 'admin' => $this->formatAdminSections($currentType, $currentSection),
  48. ],
  49. ];
  50. }
  51. /**
  52. * @param IIconSection[][] $sections
  53. * @psalm-param 'admin'|'personal' $type
  54. * @return list<array{anchor: string, section-name: string, active: bool, icon: string}>
  55. */
  56. protected function formatSections(array $sections, string $currentSection, string $type, string $currentType): array {
  57. $templateParameters = [];
  58. foreach ($sections as $prioritizedSections) {
  59. foreach ($prioritizedSections as $section) {
  60. if ($type === 'admin') {
  61. $settings = $this->settingsManager->getAllowedAdminSettings($section->getID(), $this->userSession->getUser());
  62. } elseif ($type === 'personal') {
  63. $settings = $this->settingsManager->getPersonalSettings($section->getID());
  64. }
  65. /** @psalm-suppress PossiblyNullArgument */
  66. $declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($this->userSession->getUser(), $type, $section->getID());
  67. if (empty($settings) && empty($declarativeFormIDs)) {
  68. continue;
  69. }
  70. $icon = $section->getIcon();
  71. $active = $section->getID() === $currentSection
  72. && $type === $currentType;
  73. $templateParameters[] = [
  74. 'anchor' => $section->getID(),
  75. 'section-name' => $section->getName(),
  76. 'active' => $active,
  77. 'icon' => $icon,
  78. ];
  79. }
  80. }
  81. return $templateParameters;
  82. }
  83. protected function formatPersonalSections(string $currentType, string $currentSection): array {
  84. $sections = $this->settingsManager->getPersonalSections();
  85. return $this->formatSections($sections, $currentSection, 'personal', $currentType);
  86. }
  87. protected function formatAdminSections(string $currentType, string $currentSection): array {
  88. $sections = $this->settingsManager->getAdminSections();
  89. return $this->formatSections($sections, $currentSection, 'admin', $currentType);
  90. }
  91. /**
  92. * @param list<\OCP\Settings\ISettings> $settings
  93. * @param list<DeclarativeSettingsFormSchemaWithValues> $declarativeSettings
  94. * @return array{content: string}
  95. */
  96. private function formatSettings(array $settings, array $declarativeSettings): array {
  97. $settings = array_merge($settings, $declarativeSettings);
  98. usort($settings, function ($first, $second) {
  99. $priorityOne = $first instanceof ISettings ? $first->getPriority() : $first['priority'];
  100. $priorityTwo = $second instanceof ISettings ? $second->getPriority() : $second['priority'];
  101. return $priorityOne - $priorityTwo;
  102. });
  103. $html = '';
  104. foreach ($settings as $setting) {
  105. if ($setting instanceof ISettings) {
  106. $form = $setting->getForm();
  107. $html .= $form->renderAs('')->render();
  108. } else {
  109. $html .= '<div id="' . $setting['app'] . '_' . $setting['id'] . '"></div>';
  110. }
  111. }
  112. return ['content' => $html];
  113. }
  114. /**
  115. * @psalm-param 'admin'|'personal' $type
  116. */
  117. private function getIndexResponse(string $type, string $section): TemplateResponse {
  118. $user = $this->userSession->getUser();
  119. assert($user !== null, 'No user logged in for settings');
  120. $this->declarativeSettingsManager->loadSchemas();
  121. $declarativeSettings = $this->declarativeSettingsManager->getFormsWithValues($user, $type, $section);
  122. if ($type === 'personal') {
  123. $settings = array_values($this->settingsManager->getPersonalSettings($section));
  124. if ($section === 'theming') {
  125. $this->navigationManager->setActiveEntry('accessibility_settings');
  126. } else {
  127. $this->navigationManager->setActiveEntry('settings');
  128. }
  129. } elseif ($type === 'admin') {
  130. $settings = array_values($this->settingsManager->getAllowedAdminSettings($section, $user));
  131. if (empty($settings) && empty($declarativeSettings)) {
  132. throw new NotAdminException('Logged in user does not have permission to access these settings.');
  133. }
  134. $this->navigationManager->setActiveEntry('admin_settings');
  135. } else {
  136. throw new InvalidArgumentException('$type must be either "admin" or "personal"');
  137. }
  138. if (!empty($declarativeSettings)) {
  139. Util::addScript(Application::APP_ID, 'declarative-settings-forms');
  140. $this->initialState->provideInitialState('declarative-settings-forms', $declarativeSettings);
  141. }
  142. $settings = array_merge(...$settings);
  143. $templateParams = $this->formatSettings($settings, $declarativeSettings);
  144. $templateParams = array_merge($templateParams, $this->getNavigationParameters($type, $section));
  145. $activeSection = $this->settingsManager->getSection($type, $section);
  146. if ($activeSection) {
  147. $templateParams['pageTitle'] = $activeSection->getName();
  148. $templateParams['activeSectionId'] = $activeSection->getID();
  149. $templateParams['activeSectionType'] = $type;
  150. }
  151. return new TemplateResponse('settings', 'settings/frame', $templateParams);
  152. }
  153. }