1
0

Delegation.php 3.6 KB

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