1
0

Delegation.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 Carl Schwan <carl@carlschwan.eu>
  4. *
  5. * @author Carl Schwan <carl@carlschwan.eu>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Settings\Settings\Admin;
  24. use OCA\Settings\AppInfo\Application;
  25. use OCA\Settings\Service\AuthorizedGroupService;
  26. use OCP\AppFramework\Http\TemplateResponse;
  27. use OCP\AppFramework\Services\IInitialState;
  28. use OCP\IGroupManager;
  29. use OCP\Settings\IDelegatedSettings;
  30. use OCP\Settings\IManager;
  31. use OCP\Settings\ISettings;
  32. use OCP\IURLGenerator;
  33. class Delegation implements ISettings {
  34. private IManager $settingManager;
  35. private IInitialState $initialStateService;
  36. private IGroupManager $groupManager;
  37. private AuthorizedGroupService $authorizedGroupService;
  38. private IURLGenerator $urlGenerator;
  39. public function __construct(
  40. IManager $settingManager,
  41. IInitialState $initialStateService,
  42. IGroupManager $groupManager,
  43. AuthorizedGroupService $authorizedGroupService,
  44. IURLGenerator $urlGenerator
  45. ) {
  46. $this->settingManager = $settingManager;
  47. $this->initialStateService = $initialStateService;
  48. $this->groupManager = $groupManager;
  49. $this->authorizedGroupService = $authorizedGroupService;
  50. $this->urlGenerator = $urlGenerator;
  51. }
  52. /**
  53. * Filter out the ISettings that are not IDelegatedSettings from $innerSection
  54. * and add them to $settings.
  55. *
  56. * @param IDelegatedSettings[] $settings
  57. * @param ISettings[] $innerSection
  58. * @return IDelegatedSettings[]
  59. */
  60. private function getDelegatedSettings(array $settings, array $innerSection): array {
  61. foreach ($innerSection as $setting) {
  62. if ($setting instanceof IDelegatedSettings) {
  63. $settings[] = $setting;
  64. }
  65. }
  66. return $settings;
  67. }
  68. private function initSettingState(): void {
  69. // Available settings page initialization
  70. $sections = $this->settingManager->getAdminSections();
  71. $settings = [];
  72. foreach ($sections as $sectionPriority) {
  73. foreach ($sectionPriority as $section) {
  74. $sectionSettings = $this->settingManager->getAdminSettings($section->getId());
  75. $sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
  76. $settings = array_merge(
  77. $settings,
  78. array_map(function (IDelegatedSettings $setting) use ($section) {
  79. $sectionName = $section->getName() . ($setting->getName() !== null ? ' - ' . $setting->getName() : '');
  80. return [
  81. 'class' => get_class($setting),
  82. 'sectionName' => $sectionName,
  83. 'id' => mb_strtolower(str_replace(' ', '-', $sectionName)),
  84. 'priority' => $section->getPriority(),
  85. ];
  86. }, $sectionSettings)
  87. );
  88. }
  89. }
  90. usort($settings, function (array $a, array $b) {
  91. if ($a['priority'] == $b['priority']) {
  92. return 0;
  93. }
  94. return ($a['priority'] < $b['priority']) ? -1 : 1;
  95. });
  96. $this->initialStateService->provideInitialState('available-settings', $settings);
  97. }
  98. public function initAvailableGroupState(): void {
  99. // Available groups initialization
  100. $groups = [];
  101. $groupsClass = $this->groupManager->search('');
  102. foreach ($groupsClass as $group) {
  103. if ($group->getGID() === 'admin') {
  104. continue; // Admin already have access to everything
  105. }
  106. $groups[] = [
  107. 'displayName' => $group->getDisplayName(),
  108. 'gid' => $group->getGID(),
  109. ];
  110. }
  111. $this->initialStateService->provideInitialState('available-groups', $groups);
  112. }
  113. public function initAuthorizedGroupState(): void {
  114. // Already set authorized groups
  115. $this->initialStateService->provideInitialState('authorized-groups', $this->authorizedGroupService->findAll());
  116. }
  117. public function getForm(): TemplateResponse {
  118. $this->initSettingState();
  119. $this->initAvailableGroupState();
  120. $this->initAuthorizedGroupState();
  121. $this->initialStateService->provideInitialState('authorized-settings-doc-link', $this->urlGenerator->linkToDocs('admin-delegation'));
  122. return new TemplateResponse(Application::APP_ID, 'settings/admin/delegation', [], '');
  123. }
  124. /**
  125. * @return string the section ID, e.g. 'sharing'
  126. */
  127. public function getSection() {
  128. return 'admindelegation';
  129. }
  130. /*
  131. * @inheritdoc
  132. */
  133. public function getPriority() {
  134. return 75;
  135. }
  136. }