MetaData.php 5.7 KB

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