Delegation.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings\Settings\Admin;
  7. use OCA\Settings\AppInfo\Application;
  8. use OCA\Settings\Service\AuthorizedGroupService;
  9. use OCP\AppFramework\Http\TemplateResponse;
  10. use OCP\AppFramework\Services\IInitialState;
  11. use OCP\IGroupManager;
  12. use OCP\IURLGenerator;
  13. use OCP\Settings\IDelegatedSettings;
  14. use OCP\Settings\IManager;
  15. use OCP\Settings\ISettings;
  16. class Delegation implements ISettings {
  17. private IManager $settingManager;
  18. private IInitialState $initialStateService;
  19. private IGroupManager $groupManager;
  20. private AuthorizedGroupService $authorizedGroupService;
  21. private IURLGenerator $urlGenerator;
  22. public function __construct(
  23. IManager $settingManager,
  24. IInitialState $initialStateService,
  25. IGroupManager $groupManager,
  26. AuthorizedGroupService $authorizedGroupService,
  27. IURLGenerator $urlGenerator
  28. ) {
  29. $this->settingManager = $settingManager;
  30. $this->initialStateService = $initialStateService;
  31. $this->groupManager = $groupManager;
  32. $this->authorizedGroupService = $authorizedGroupService;
  33. $this->urlGenerator = $urlGenerator;
  34. }
  35. /**
  36. * Filter out the ISettings that are not IDelegatedSettings from $innerSection
  37. * and add them to $settings.
  38. *
  39. * @param IDelegatedSettings[] $settings
  40. * @param ISettings[] $innerSection
  41. * @return IDelegatedSettings[]
  42. */
  43. private function getDelegatedSettings(array $settings, array $innerSection): array {
  44. foreach ($innerSection as $setting) {
  45. if ($setting instanceof IDelegatedSettings) {
  46. $settings[] = $setting;
  47. }
  48. }
  49. return $settings;
  50. }
  51. private function initSettingState(): void {
  52. // Available settings page initialization
  53. $sections = $this->settingManager->getAdminSections();
  54. $settings = [];
  55. foreach ($sections as $sectionPriority) {
  56. foreach ($sectionPriority as $section) {
  57. $sectionSettings = $this->settingManager->getAdminSettings($section->getId());
  58. $sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
  59. $settings = array_merge(
  60. $settings,
  61. array_map(function (IDelegatedSettings $setting) use ($section) {
  62. $sectionName = $section->getName() . ($setting->getName() !== null ? ' - ' . $setting->getName() : '');
  63. return [
  64. 'class' => get_class($setting),
  65. 'sectionName' => $sectionName,
  66. 'id' => mb_strtolower(str_replace(' ', '-', $sectionName)),
  67. 'priority' => $section->getPriority(),
  68. ];
  69. }, $sectionSettings)
  70. );
  71. }
  72. }
  73. usort($settings, function (array $a, array $b) {
  74. if ($a['priority'] == $b['priority']) {
  75. return 0;
  76. }
  77. return ($a['priority'] < $b['priority']) ? -1 : 1;
  78. });
  79. $this->initialStateService->provideInitialState('available-settings', $settings);
  80. }
  81. public function initAvailableGroupState(): void {
  82. // Available groups initialization
  83. $groups = [];
  84. $groupsClass = $this->groupManager->search('');
  85. foreach ($groupsClass as $group) {
  86. if ($group->getGID() === 'admin') {
  87. continue; // Admin already have access to everything
  88. }
  89. $groups[] = [
  90. 'displayName' => $group->getDisplayName(),
  91. 'gid' => $group->getGID(),
  92. ];
  93. }
  94. $this->initialStateService->provideInitialState('available-groups', $groups);
  95. }
  96. public function initAuthorizedGroupState(): void {
  97. // Already set authorized groups
  98. $this->initialStateService->provideInitialState('authorized-groups', $this->authorizedGroupService->findAll());
  99. }
  100. public function getForm(): TemplateResponse {
  101. $this->initSettingState();
  102. $this->initAvailableGroupState();
  103. $this->initAuthorizedGroupState();
  104. $this->initialStateService->provideInitialState('authorized-settings-doc-link', $this->urlGenerator->linkToDocs('admin-delegation'));
  105. return new TemplateResponse(Application::APP_ID, 'settings/admin/delegation', [], '');
  106. }
  107. /**
  108. * @return string the section ID, e.g. 'sharing'
  109. */
  110. public function getSection() {
  111. return 'admindelegation';
  112. }
  113. /*
  114. * @inheritdoc
  115. */
  116. public function getPriority() {
  117. return 75;
  118. }
  119. }