MetaData.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Stephan Peijnik <speijnik@anexia-it.com>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Group;
  31. use OC\Group\Manager as GroupManager;
  32. use OCP\IGroup;
  33. use OCP\IGroupManager;
  34. use OCP\IUserSession;
  35. class MetaData {
  36. public const SORT_NONE = 0;
  37. public const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends
  38. public const SORT_GROUPNAME = 2;
  39. /** @var string */
  40. protected $user;
  41. /** @var bool */
  42. protected $isAdmin;
  43. /** @var array */
  44. protected $metaData = [];
  45. /** @var GroupManager */
  46. protected $groupManager;
  47. /** @var int */
  48. protected $sorting = self::SORT_NONE;
  49. /** @var IUserSession */
  50. protected $userSession;
  51. /**
  52. * @param string $user the uid of the current user
  53. * @param bool $isAdmin whether the current users is an admin
  54. */
  55. public function __construct(
  56. string $user,
  57. bool $isAdmin,
  58. IGroupManager $groupManager,
  59. IUserSession $userSession
  60. ) {
  61. $this->user = $user;
  62. $this->isAdmin = $isAdmin;
  63. $this->groupManager = $groupManager;
  64. $this->userSession = $userSession;
  65. }
  66. /**
  67. * returns an array with meta data about all available groups
  68. * the array is structured as follows:
  69. * [0] array containing meta data about admin groups
  70. * [1] array containing meta data about unprivileged groups
  71. * @param string $groupSearch only effective when instance was created with
  72. * isAdmin being true
  73. * @param string $userSearch the pattern users are search for
  74. */
  75. public function get(string $groupSearch = '', string $userSearch = ''): array {
  76. $key = $groupSearch . '::' . $userSearch;
  77. if (isset($this->metaData[$key])) {
  78. return $this->metaData[$key];
  79. }
  80. $adminGroups = [];
  81. $groups = [];
  82. $sortGroupsIndex = 0;
  83. $sortGroupsKeys = [];
  84. $sortAdminGroupsIndex = 0;
  85. $sortAdminGroupsKeys = [];
  86. foreach ($this->getGroups($groupSearch) as $group) {
  87. $groupMetaData = $this->generateGroupMetaData($group, $userSearch);
  88. if (strtolower($group->getGID()) !== 'admin') {
  89. $this->addEntry(
  90. $groups,
  91. $sortGroupsKeys,
  92. $sortGroupsIndex,
  93. $groupMetaData);
  94. } else {
  95. //admin group is hard coded to 'admin' for now. In future,
  96. //backends may define admin groups too. Then the if statement
  97. //has to be adjusted accordingly.
  98. $this->addEntry(
  99. $adminGroups,
  100. $sortAdminGroupsKeys,
  101. $sortAdminGroupsIndex,
  102. $groupMetaData);
  103. }
  104. }
  105. //whether sorting is necessary is will be checked in sort()
  106. $this->sort($groups, $sortGroupsKeys);
  107. $this->sort($adminGroups, $sortAdminGroupsKeys);
  108. $this->metaData[$key] = [$adminGroups, $groups];
  109. return $this->metaData[$key];
  110. }
  111. /**
  112. * sets the sort mode, see SORT_* constants for supported modes
  113. */
  114. public function setSorting(int $sortMode): void {
  115. switch ($sortMode) {
  116. case self::SORT_USERCOUNT:
  117. case self::SORT_GROUPNAME:
  118. $this->sorting = $sortMode;
  119. break;
  120. default:
  121. $this->sorting = self::SORT_NONE;
  122. }
  123. }
  124. /**
  125. * adds an group entry to the resulting array
  126. * @param array $entries the resulting array, by reference
  127. * @param array $sortKeys the sort key array, by reference
  128. * @param int $sortIndex the sort key index, by reference
  129. * @param array $data the group's meta data as returned by generateGroupMetaData()
  130. */
  131. private function addEntry(array &$entries, array &$sortKeys, int &$sortIndex, array $data): void {
  132. $entries[] = $data;
  133. if ($this->sorting === self::SORT_USERCOUNT) {
  134. $sortKeys[$sortIndex] = $data['usercount'];
  135. $sortIndex++;
  136. } elseif ($this->sorting === self::SORT_GROUPNAME) {
  137. $sortKeys[$sortIndex] = $data['name'];
  138. $sortIndex++;
  139. }
  140. }
  141. /**
  142. * creates an array containing the group meta data
  143. * @return array with the keys 'id', 'name', 'usercount' and 'disabled'
  144. */
  145. private function generateGroupMetaData(IGroup $group, string $userSearch): array {
  146. return [
  147. 'id' => $group->getGID(),
  148. 'name' => $group->getDisplayName(),
  149. 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0,
  150. 'disabled' => $group->countDisabled(),
  151. 'canAdd' => $group->canAddUser(),
  152. 'canRemove' => $group->canRemoveUser(),
  153. ];
  154. }
  155. /**
  156. * sorts the result array, if applicable
  157. * @param array $entries the result array, by reference
  158. * @param array $sortKeys the array containing the sort keys
  159. */
  160. private function sort(array &$entries, array $sortKeys): void {
  161. if ($this->sorting === self::SORT_USERCOUNT) {
  162. array_multisort($sortKeys, SORT_DESC, $entries);
  163. } elseif ($this->sorting === self::SORT_GROUPNAME) {
  164. array_multisort($sortKeys, SORT_ASC, $entries);
  165. }
  166. }
  167. /**
  168. * returns the available groups
  169. * @return IGroup[]
  170. */
  171. public function getGroups(string $search = ''): array {
  172. if ($this->isAdmin) {
  173. return $this->groupManager->search($search);
  174. } else {
  175. $userObject = $this->userSession->getUser();
  176. if ($userObject !== null) {
  177. $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject);
  178. } else {
  179. $groups = [];
  180. }
  181. return $groups;
  182. }
  183. }
  184. }