AdminSettingsController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\OpenAPI;
  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\IRequest;
  16. use OCP\IUser;
  17. use OCP\IUserSession;
  18. use OCP\Settings\IDeclarativeManager;
  19. use OCP\Settings\IManager as ISettingsManager;
  20. use OCP\Template;
  21. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  22. class AdminSettingsController extends Controller {
  23. use CommonSettingsTrait;
  24. public function __construct(
  25. $appName,
  26. IRequest $request,
  27. INavigationManager $navigationManager,
  28. ISettingsManager $settingsManager,
  29. IUserSession $userSession,
  30. IGroupManager $groupManager,
  31. ISubAdmin $subAdmin,
  32. IDeclarativeManager $declarativeSettingsManager,
  33. IInitialState $initialState,
  34. ) {
  35. parent::__construct($appName, $request);
  36. $this->navigationManager = $navigationManager;
  37. $this->settingsManager = $settingsManager;
  38. $this->userSession = $userSession;
  39. $this->groupManager = $groupManager;
  40. $this->subAdmin = $subAdmin;
  41. $this->declarativeSettingsManager = $declarativeSettingsManager;
  42. $this->initialState = $initialState;
  43. }
  44. /**
  45. * @NoCSRFRequired
  46. * @NoAdminRequired
  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. public function index(string $section): TemplateResponse {
  52. return $this->getIndexResponse('admin', $section);
  53. }
  54. /**
  55. * @param string $section
  56. * @return array
  57. */
  58. protected function getSettings($section) {
  59. /** @var IUser $user */
  60. $user = $this->userSession->getUser();
  61. $isSubAdmin = !$this->groupManager->isAdmin($user->getUID()) && $this->subAdmin->isSubAdmin($user);
  62. $settings = $this->settingsManager->getAllowedAdminSettings($section, $user);
  63. $declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($user, 'admin', $section);
  64. if (empty($settings) && empty($declarativeFormIDs)) {
  65. throw new NotAdminException("Logged in user doesn't have permission to access these settings.");
  66. }
  67. $formatted = $this->formatSettings($settings);
  68. // Do not show legacy forms for sub admins
  69. if ($section === 'additional' && !$isSubAdmin) {
  70. $formatted['content'] .= $this->getLegacyForms();
  71. }
  72. return $formatted;
  73. }
  74. /**
  75. * @return bool|string
  76. */
  77. private function getLegacyForms() {
  78. $forms = \OC_App::getForms('admin');
  79. $forms = array_map(function ($form) {
  80. if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) {
  81. $sectionName = str_replace('<h2' . $regs['class'] . '>', '', $regs[0]);
  82. $sectionName = str_replace('</h2>', '', $sectionName);
  83. $anchor = strtolower($sectionName);
  84. $anchor = str_replace(' ', '-', $anchor);
  85. return [
  86. 'anchor' => $anchor,
  87. 'section-name' => $sectionName,
  88. 'form' => $form
  89. ];
  90. }
  91. return [
  92. 'form' => $form
  93. ];
  94. }, $forms);
  95. $out = new Template('settings', 'settings/additional');
  96. $out->assign('forms', $forms);
  97. return $out->fetchPage();
  98. }
  99. }