AdminSettingsController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\IgnoreOpenAPI;
  31. use OCP\AppFramework\Http\TemplateResponse;
  32. use OCP\Group\ISubAdmin;
  33. use OCP\IGroupManager;
  34. use OCP\INavigationManager;
  35. use OCP\IRequest;
  36. use OCP\IUser;
  37. use OCP\IUserSession;
  38. use OCP\Settings\IManager as ISettingsManager;
  39. use OCP\Template;
  40. #[IgnoreOpenAPI]
  41. class AdminSettingsController extends Controller {
  42. use CommonSettingsTrait;
  43. public function __construct(
  44. $appName,
  45. IRequest $request,
  46. INavigationManager $navigationManager,
  47. ISettingsManager $settingsManager,
  48. IUserSession $userSession,
  49. IGroupManager $groupManager,
  50. ISubAdmin $subAdmin
  51. ) {
  52. parent::__construct($appName, $request);
  53. $this->navigationManager = $navigationManager;
  54. $this->settingsManager = $settingsManager;
  55. $this->userSession = $userSession;
  56. $this->groupManager = $groupManager;
  57. $this->subAdmin = $subAdmin;
  58. }
  59. /**
  60. * @NoCSRFRequired
  61. * @NoAdminRequired
  62. * @NoSubAdminRequired
  63. * We are checking the permissions in the getSettings method. If there is no allowed
  64. * settings for the given section. The user will be gretted by an error message.
  65. */
  66. public function index(string $section): TemplateResponse {
  67. return $this->getIndexResponse('admin', $section);
  68. }
  69. /**
  70. * @param string $section
  71. * @return array
  72. */
  73. protected function getSettings($section) {
  74. /** @var IUser $user */
  75. $user = $this->userSession->getUser();
  76. $isSubAdmin = !$this->groupManager->isAdmin($user->getUID()) && $this->subAdmin->isSubAdmin($user);
  77. $settings = $this->settingsManager->getAllowedAdminSettings($section, $user);
  78. if (empty($settings)) {
  79. throw new NotAdminException("Logged in user doesn't have permission to access these settings.");
  80. }
  81. $formatted = $this->formatSettings($settings);
  82. // Do not show legacy forms for sub admins
  83. if ($section === 'additional' && !$isSubAdmin) {
  84. $formatted['content'] .= $this->getLegacyForms();
  85. }
  86. return $formatted;
  87. }
  88. /**
  89. * @return bool|string
  90. */
  91. private function getLegacyForms() {
  92. $forms = \OC_App::getForms('admin');
  93. $forms = array_map(function ($form) {
  94. if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) {
  95. $sectionName = str_replace('<h2' . $regs['class'] . '>', '', $regs[0]);
  96. $sectionName = str_replace('</h2>', '', $sectionName);
  97. $anchor = strtolower($sectionName);
  98. $anchor = str_replace(' ', '-', $anchor);
  99. return [
  100. 'anchor' => $anchor,
  101. 'section-name' => $sectionName,
  102. 'form' => $form
  103. ];
  104. }
  105. return [
  106. 'form' => $form
  107. ];
  108. }, $forms);
  109. $out = new Template('settings', 'settings/additional');
  110. $out->assign('forms', $forms);
  111. return $out->fetchPage();
  112. }
  113. }