Delegation.php 4.5 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. class Delegation implements ISettings {
  33. /** @var IManager */
  34. private $settingManager;
  35. /** @var IInitialState $initialStateService */
  36. private $initialStateService;
  37. /** @var IGroupManager $groupManager */
  38. private $groupManager;
  39. /** @var AuthorizedGroupService $authorizedGroupService */
  40. private $authorizedGroupService;
  41. public function __construct(
  42. IManager $settingManager,
  43. IInitialState $initialStateService,
  44. IGroupManager $groupManager,
  45. AuthorizedGroupService $authorizedGroupService
  46. ) {
  47. $this->settingManager = $settingManager;
  48. $this->initialStateService = $initialStateService;
  49. $this->groupManager = $groupManager;
  50. $this->authorizedGroupService = $authorizedGroupService;
  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. return [
  80. 'class' => get_class($setting),
  81. 'sectionName' => $section->getName() . ($setting->getName() !== null ? ' - ' . $setting->getName() : ''),
  82. 'priority' => $section->getPriority(),
  83. ];
  84. }, $sectionSettings)
  85. );
  86. }
  87. }
  88. usort($settings, function (array $a, array $b) {
  89. if ($a['priority'] == $b['priority']) {
  90. return 0;
  91. }
  92. return ($a['priority'] < $b['priority']) ? -1 : 1;
  93. });
  94. $this->initialStateService->provideInitialState('available-settings', $settings);
  95. }
  96. public function initAvailableGroupState(): void {
  97. // Available groups initialization
  98. $groups = [];
  99. $groupsClass = $this->groupManager->search('');
  100. foreach ($groupsClass as $group) {
  101. if ($group->getGID() === 'admin') {
  102. continue; // Admin already have access to everything
  103. }
  104. $groups[] = [
  105. 'displayName' => $group->getDisplayName(),
  106. 'gid' => $group->getGID(),
  107. ];
  108. }
  109. $this->initialStateService->provideInitialState('available-groups', $groups);
  110. }
  111. public function initAuthorizedGroupState(): void {
  112. // Already set authorized groups
  113. $this->initialStateService->provideInitialState('authorized-groups', $this->authorizedGroupService->findAll());
  114. }
  115. public function getForm(): TemplateResponse {
  116. $this->initSettingState();
  117. $this->initAvailableGroupState();
  118. $this->initAuthorizedGroupState();
  119. return new TemplateResponse(Application::APP_ID, 'settings/admin/delegation', [], '');
  120. }
  121. /**
  122. * @return string the section ID, e.g. 'sharing'
  123. */
  124. public function getSection() {
  125. return 'admindelegation';
  126. }
  127. /*
  128. * @inheritdoc
  129. */
  130. public function getPriority() {
  131. return 75;
  132. }
  133. }