You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.1 KiB

3 years ago
  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 Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  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\IGroupManager;
  33. use OCP\IUserSession;
  34. class MetaData {
  35. public const SORT_NONE = 0;
  36. public const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends
  37. public const SORT_GROUPNAME = 2;
  38. /** @var string */
  39. protected $user;
  40. /** @var bool */
  41. protected $isAdmin;
  42. /** @var array */
  43. protected $metaData = [];
  44. /** @var GroupManager */
  45. protected $groupManager;
  46. /** @var bool */
  47. protected $sorting = false;
  48. /** @var IUserSession */
  49. protected $userSession;
  50. /**
  51. * @param string $user the uid of the current user
  52. * @param bool $isAdmin whether the current users is an admin
  53. * @param IGroupManager $groupManager
  54. * @param IUserSession $userSession
  55. */
  56. public function __construct(
  57. $user,
  58. $isAdmin,
  59. IGroupManager $groupManager,
  60. IUserSession $userSession
  61. ) {
  62. $this->user = $user;
  63. $this->isAdmin = (bool)$isAdmin;
  64. $this->groupManager = $groupManager;
  65. $this->userSession = $userSession;
  66. }
  67. /**
  68. * returns an array with meta data about all available groups
  69. * the array is structured as follows:
  70. * [0] array containing meta data about admin groups
  71. * [1] array containing meta data about unprivileged groups
  72. * @param string $groupSearch only effective when instance was created with
  73. * isAdmin being true
  74. * @param string $userSearch the pattern users are search for
  75. * @return array
  76. */
  77. public function get($groupSearch = '', $userSearch = '') {
  78. $key = $groupSearch . '::' . $userSearch;
  79. if (isset($this->metaData[$key])) {
  80. return $this->metaData[$key];
  81. }
  82. $adminGroups = [];
  83. $groups = [];
  84. $sortGroupsIndex = 0;
  85. $sortGroupsKeys = [];
  86. $sortAdminGroupsIndex = 0;
  87. $sortAdminGroupsKeys = [];
  88. foreach ($this->getGroups($groupSearch) as $group) {
  89. $groupMetaData = $this->generateGroupMetaData($group, $userSearch);
  90. if (strtolower($group->getGID()) !== 'admin') {
  91. $this->addEntry(
  92. $groups,
  93. $sortGroupsKeys,
  94. $sortGroupsIndex,
  95. $groupMetaData);
  96. } else {
  97. //admin group is hard coded to 'admin' for now. In future,
  98. //backends may define admin groups too. Then the if statement
  99. //has to be adjusted accordingly.
  100. $this->addEntry(
  101. $adminGroups,
  102. $sortAdminGroupsKeys,
  103. $sortAdminGroupsIndex,
  104. $groupMetaData);
  105. }
  106. }
  107. //whether sorting is necessary is will be checked in sort()
  108. $this->sort($groups, $sortGroupsKeys);
  109. $this->sort($adminGroups, $sortAdminGroupsKeys);
  110. $this->metaData[$key] = [$adminGroups, $groups];
  111. return $this->metaData[$key];
  112. }
  113. /**
  114. * sets the sort mode, see SORT_* constants for supported modes
  115. *
  116. * @param int $sortMode
  117. */
  118. public function setSorting($sortMode) {
  119. switch ($sortMode) {
  120. case self::SORT_USERCOUNT:
  121. case self::SORT_GROUPNAME:
  122. $this->sorting = $sortMode;
  123. break;
  124. default:
  125. $this->sorting = self::SORT_NONE;
  126. }
  127. }
  128. /**
  129. * adds an group entry to the resulting array
  130. * @param array $entries the resulting array, by reference
  131. * @param array $sortKeys the sort key array, by reference
  132. * @param int $sortIndex the sort key index, by reference
  133. * @param array $data the group's meta data as returned by generateGroupMetaData()
  134. */
  135. private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) {
  136. $entries[] = $data;
  137. if ($this->sorting === self::SORT_USERCOUNT) {
  138. $sortKeys[$sortIndex] = $data['usercount'];
  139. $sortIndex++;
  140. } elseif ($this->sorting === self::SORT_GROUPNAME) {
  141. $sortKeys[$sortIndex] = $data['name'];
  142. $sortIndex++;
  143. }
  144. }
  145. /**
  146. * creates an array containing the group meta data
  147. * @param \OCP\IGroup $group
  148. * @param string $userSearch
  149. * @return array with the keys 'id', 'name', 'usercount' and 'disabled'
  150. */
  151. private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) {
  152. return [
  153. 'id' => $group->getGID(),
  154. 'name' => $group->getDisplayName(),
  155. 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0,
  156. 'disabled' => $group->countDisabled(),
  157. 'canAdd' => $group->canAddUser(),
  158. 'canRemove' => $group->canRemoveUser(),
  159. ];
  160. }
  161. /**
  162. * sorts the result array, if applicable
  163. * @param array $entries the result array, by reference
  164. * @param array $sortKeys the array containing the sort keys
  165. * @param return null
  166. */
  167. private function sort(&$entries, $sortKeys) {
  168. if ($this->sorting === self::SORT_USERCOUNT) {
  169. array_multisort($sortKeys, SORT_DESC, $entries);
  170. } elseif ($this->sorting === self::SORT_GROUPNAME) {
  171. array_multisort($sortKeys, SORT_ASC, $entries);
  172. }
  173. }
  174. /**
  175. * returns the available groups
  176. * @param string $search a search string
  177. * @return \OCP\IGroup[]
  178. */
  179. public function getGroups($search = '') {
  180. if ($this->isAdmin) {
  181. return $this->groupManager->search($search);
  182. } else {
  183. $userObject = $this->userSession->getUser();
  184. if ($userObject !== null) {
  185. $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject);
  186. } else {
  187. $groups = [];
  188. }
  189. return $groups;
  190. }
  191. }
  192. }