AdminSettingsController.php 3.7 KB

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