MetaData.php 6.1 KB

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