metadata.php 5.9 KB

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