1
0

MetaData.php 5.0 KB

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