1
0

MetaData.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Group;
  8. use OC\Group\Manager as GroupManager;
  9. use OCP\IGroup;
  10. use OCP\IGroupManager;
  11. use OCP\IUserSession;
  12. class MetaData {
  13. public const SORT_NONE = 0;
  14. public const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends
  15. public const SORT_GROUPNAME = 2;
  16. /** @var array */
  17. protected $metaData = [];
  18. /** @var int */
  19. protected $sorting = self::SORT_NONE;
  20. /**
  21. * @param string $user the uid of the current user
  22. * @param bool $isAdmin whether the current users is an admin
  23. */
  24. public function __construct(
  25. private string $user,
  26. private bool $isAdmin,
  27. private bool $isDelegatedAdmin,
  28. private IGroupManager $groupManager,
  29. private IUserSession $userSession,
  30. ) {
  31. }
  32. /**
  33. * returns an array with meta data about all available groups
  34. * the array is structured as follows:
  35. * [0] array containing meta data about admin groups
  36. * [1] array containing meta data about unprivileged groups
  37. * @param string $groupSearch only effective when instance was created with
  38. * isAdmin being true
  39. * @param string $userSearch the pattern users are search for
  40. */
  41. public function get(string $groupSearch = '', string $userSearch = ''): array {
  42. $key = $groupSearch . '::' . $userSearch;
  43. if (isset($this->metaData[$key])) {
  44. return $this->metaData[$key];
  45. }
  46. $adminGroups = [];
  47. $groups = [];
  48. $sortGroupsIndex = 0;
  49. $sortGroupsKeys = [];
  50. $sortAdminGroupsIndex = 0;
  51. $sortAdminGroupsKeys = [];
  52. foreach ($this->getGroups($groupSearch) as $group) {
  53. $groupMetaData = $this->generateGroupMetaData($group, $userSearch);
  54. if (strtolower($group->getGID()) !== 'admin') {
  55. $this->addEntry(
  56. $groups,
  57. $sortGroupsKeys,
  58. $sortGroupsIndex,
  59. $groupMetaData);
  60. } else {
  61. //admin group is hard coded to 'admin' for now. In future,
  62. //backends may define admin groups too. Then the if statement
  63. //has to be adjusted accordingly.
  64. $this->addEntry(
  65. $adminGroups,
  66. $sortAdminGroupsKeys,
  67. $sortAdminGroupsIndex,
  68. $groupMetaData);
  69. }
  70. }
  71. //whether sorting is necessary is will be checked in sort()
  72. $this->sort($groups, $sortGroupsKeys);
  73. $this->sort($adminGroups, $sortAdminGroupsKeys);
  74. $this->metaData[$key] = [$adminGroups, $groups];
  75. return $this->metaData[$key];
  76. }
  77. /**
  78. * sets the sort mode, see SORT_* constants for supported modes
  79. */
  80. public function setSorting(int $sortMode): void {
  81. switch ($sortMode) {
  82. case self::SORT_USERCOUNT:
  83. case self::SORT_GROUPNAME:
  84. $this->sorting = $sortMode;
  85. break;
  86. default:
  87. $this->sorting = self::SORT_NONE;
  88. }
  89. }
  90. /**
  91. * adds an group entry to the resulting array
  92. * @param array $entries the resulting array, by reference
  93. * @param array $sortKeys the sort key array, by reference
  94. * @param int $sortIndex the sort key index, by reference
  95. * @param array $data the group's meta data as returned by generateGroupMetaData()
  96. */
  97. private function addEntry(array &$entries, array &$sortKeys, int &$sortIndex, array $data): void {
  98. $entries[] = $data;
  99. if ($this->sorting === self::SORT_USERCOUNT) {
  100. $sortKeys[$sortIndex] = $data['usercount'];
  101. $sortIndex++;
  102. } elseif ($this->sorting === self::SORT_GROUPNAME) {
  103. $sortKeys[$sortIndex] = $data['name'];
  104. $sortIndex++;
  105. }
  106. }
  107. /**
  108. * creates an array containing the group meta data
  109. * @return array with the keys 'id', 'name', 'usercount' and 'disabled'
  110. */
  111. private function generateGroupMetaData(IGroup $group, string $userSearch): array {
  112. return [
  113. 'id' => $group->getGID(),
  114. 'name' => $group->getDisplayName(),
  115. 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0,
  116. 'disabled' => $group->countDisabled(),
  117. 'canAdd' => $group->canAddUser(),
  118. 'canRemove' => $group->canRemoveUser(),
  119. ];
  120. }
  121. /**
  122. * sorts the result array, if applicable
  123. * @param array $entries the result array, by reference
  124. * @param array $sortKeys the array containing the sort keys
  125. */
  126. private function sort(array &$entries, array $sortKeys): void {
  127. if ($this->sorting === self::SORT_USERCOUNT) {
  128. array_multisort($sortKeys, SORT_DESC, $entries);
  129. } elseif ($this->sorting === self::SORT_GROUPNAME) {
  130. array_multisort($sortKeys, SORT_ASC, $entries);
  131. }
  132. }
  133. /**
  134. * returns the available groups
  135. * @return IGroup[]
  136. */
  137. public function getGroups(string $search = ''): array {
  138. if ($this->isAdmin || $this->isDelegatedAdmin) {
  139. return $this->groupManager->search($search);
  140. } else {
  141. $userObject = $this->userSession->getUser();
  142. if ($userObject !== null && $this->groupManager instanceof GroupManager) {
  143. $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject);
  144. } else {
  145. $groups = [];
  146. }
  147. return $groups;
  148. }
  149. }
  150. }