AdminSettingsController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings\Controller;
  7. use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
  8. use OCP\AppFramework\Controller;
  9. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  10. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  11. use OCP\AppFramework\Http\Attribute\OpenAPI;
  12. use OCP\AppFramework\Http\TemplateResponse;
  13. use OCP\AppFramework\Services\IInitialState;
  14. use OCP\Group\ISubAdmin;
  15. use OCP\IGroupManager;
  16. use OCP\INavigationManager;
  17. use OCP\IRequest;
  18. use OCP\IUser;
  19. use OCP\IUserSession;
  20. use OCP\Settings\IDeclarativeManager;
  21. use OCP\Settings\IManager as ISettingsManager;
  22. use OCP\Template;
  23. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  24. class AdminSettingsController extends Controller {
  25. use CommonSettingsTrait;
  26. public function __construct(
  27. $appName,
  28. IRequest $request,
  29. INavigationManager $navigationManager,
  30. ISettingsManager $settingsManager,
  31. IUserSession $userSession,
  32. IGroupManager $groupManager,
  33. ISubAdmin $subAdmin,
  34. IDeclarativeManager $declarativeSettingsManager,
  35. IInitialState $initialState,
  36. ) {
  37. parent::__construct($appName, $request);
  38. $this->navigationManager = $navigationManager;
  39. $this->settingsManager = $settingsManager;
  40. $this->userSession = $userSession;
  41. $this->groupManager = $groupManager;
  42. $this->subAdmin = $subAdmin;
  43. $this->declarativeSettingsManager = $declarativeSettingsManager;
  44. $this->initialState = $initialState;
  45. }
  46. /**
  47. * @NoSubAdminRequired
  48. * We are checking the permissions in the getSettings method. If there is no allowed
  49. * settings for the given section. The user will be gretted by an error message.
  50. */
  51. #[NoAdminRequired]
  52. #[NoCSRFRequired]
  53. public function index(string $section): TemplateResponse {
  54. return $this->getIndexResponse('admin', $section);
  55. }
  56. /**
  57. * @param string $section
  58. * @return array
  59. */
  60. protected function getSettings($section) {
  61. /** @var IUser $user */
  62. $user = $this->userSession->getUser();
  63. $isSubAdmin = !$this->groupManager->isAdmin($user->getUID()) && $this->subAdmin->isSubAdmin($user);
  64. $settings = $this->settingsManager->getAllowedAdminSettings($section, $user);
  65. $declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($user, 'admin', $section);
  66. if (empty($settings) && empty($declarativeFormIDs)) {
  67. throw new NotAdminException("Logged in user doesn't have permission to access these settings.");
  68. }
  69. $formatted = $this->formatSettings($settings);
  70. // Do not show legacy forms for sub admins
  71. if ($section === 'additional' && !$isSubAdmin) {
  72. $formatted['content'] .= $this->getLegacyForms();
  73. }
  74. return $formatted;
  75. }
  76. /**
  77. * @return bool|string
  78. */
  79. private function getLegacyForms() {
  80. $forms = \OC_App::getForms('admin');
  81. $forms = array_map(function ($form) {
  82. if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) {
  83. $sectionName = str_replace('<h2' . $regs['class'] . '>', '', $regs[0]);
  84. $sectionName = str_replace('</h2>', '', $sectionName);
  85. $anchor = strtolower($sectionName);
  86. $anchor = str_replace(' ', '-', $anchor);
  87. return [
  88. 'anchor' => $anchor,
  89. 'section-name' => $sectionName,
  90. 'form' => $form
  91. ];
  92. }
  93. return [
  94. 'form' => $form
  95. ];
  96. }, $forms);
  97. $out = new Template('settings', 'settings/additional');
  98. $out->assign('forms', $forms);
  99. return $out->fetchPage();
  100. }
  101. }