You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdminSettingsController.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Kate Döen <kate.doeen@nextcloud.com>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\Settings\Controller;
  28. use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http\Attribute\OpenAPI;
  31. use OCP\AppFramework\Http\TemplateResponse;
  32. use OCP\AppFramework\Services\IInitialState;
  33. use OCP\Group\ISubAdmin;
  34. use OCP\IGroupManager;
  35. use OCP\INavigationManager;
  36. use OCP\IRequest;
  37. use OCP\IUser;
  38. use OCP\IUserSession;
  39. use OCP\Settings\IDeclarativeManager;
  40. use OCP\Settings\IManager as ISettingsManager;
  41. use OCP\Template;
  42. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  43. class AdminSettingsController extends Controller {
  44. use CommonSettingsTrait;
  45. public function __construct(
  46. $appName,
  47. IRequest $request,
  48. INavigationManager $navigationManager,
  49. ISettingsManager $settingsManager,
  50. IUserSession $userSession,
  51. IGroupManager $groupManager,
  52. ISubAdmin $subAdmin,
  53. IDeclarativeManager $declarativeSettingsManager,
  54. IInitialState $initialState,
  55. ) {
  56. parent::__construct($appName, $request);
  57. $this->navigationManager = $navigationManager;
  58. $this->settingsManager = $settingsManager;
  59. $this->userSession = $userSession;
  60. $this->groupManager = $groupManager;
  61. $this->subAdmin = $subAdmin;
  62. $this->declarativeSettingsManager = $declarativeSettingsManager;
  63. $this->initialState = $initialState;
  64. }
  65. /**
  66. * @NoCSRFRequired
  67. * @NoAdminRequired
  68. * @NoSubAdminRequired
  69. * We are checking the permissions in the getSettings method. If there is no allowed
  70. * settings for the given section. The user will be gretted by an error message.
  71. */
  72. public function index(string $section): TemplateResponse {
  73. return $this->getIndexResponse('admin', $section);
  74. }
  75. /**
  76. * @param string $section
  77. * @return array
  78. */
  79. protected function getSettings($section) {
  80. /** @var IUser $user */
  81. $user = $this->userSession->getUser();
  82. $isSubAdmin = !$this->groupManager->isAdmin($user->getUID()) && $this->subAdmin->isSubAdmin($user);
  83. $settings = $this->settingsManager->getAllowedAdminSettings($section, $user);
  84. $declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($user, 'admin', $section);
  85. if (empty($settings) && empty($declarativeFormIDs)) {
  86. throw new NotAdminException("Logged in user doesn't have permission to access these settings.");
  87. }
  88. $formatted = $this->formatSettings($settings);
  89. // Do not show legacy forms for sub admins
  90. if ($section === 'additional' && !$isSubAdmin) {
  91. $formatted['content'] .= $this->getLegacyForms();
  92. }
  93. return $formatted;
  94. }
  95. /**
  96. * @return bool|string
  97. */
  98. private function getLegacyForms() {
  99. $forms = \OC_App::getForms('admin');
  100. $forms = array_map(function ($form) {
  101. if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) {
  102. $sectionName = str_replace('<h2' . $regs['class'] . '>', '', $regs[0]);
  103. $sectionName = str_replace('</h2>', '', $sectionName);
  104. $anchor = strtolower($sectionName);
  105. $anchor = str_replace(' ', '-', $anchor);
  106. return [
  107. 'anchor' => $anchor,
  108. 'section-name' => $sectionName,
  109. 'form' => $form
  110. ];
  111. }
  112. return [
  113. 'form' => $form
  114. ];
  115. }, $forms);
  116. $out = new Template('settings', 'settings/additional');
  117. $out->assign('forms', $forms);
  118. return $out->fetchPage();
  119. }
  120. }