1
0

MetaData.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * @author John Molakvoæ <skjnldsv@protonmail.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Group;
  29. use OCP\IUserSession;
  30. use OCP\IGroupManager;
  31. class MetaData {
  32. const SORT_NONE = 0;
  33. const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends
  34. const SORT_GROUPNAME = 2;
  35. /** @var string */
  36. protected $user;
  37. /** @var bool */
  38. protected $isAdmin;
  39. /** @var array */
  40. protected $metaData = array();
  41. /** @var IGroupManager */
  42. protected $groupManager;
  43. /** @var bool */
  44. protected $sorting = false;
  45. /** @var IUserSession */
  46. protected $userSession;
  47. /**
  48. * @param string $user the uid of the current user
  49. * @param bool $isAdmin whether the current users is an admin
  50. * @param IGroupManager $groupManager
  51. * @param IUserManager $userManager
  52. * @param IUserSession $userSession
  53. */
  54. public function __construct(
  55. $user,
  56. $isAdmin,
  57. IGroupManager $groupManager,
  58. IUserSession $userSession
  59. ) {
  60. $this->user = $user;
  61. $this->isAdmin = (bool)$isAdmin;
  62. $this->groupManager = $groupManager;
  63. $this->userSession = $userSession;
  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', 'usercount' and 'disabled'
  148. */
  149. private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) {
  150. return array(
  151. 'id' => $group->getGID(),
  152. 'name' => $group->getDisplayName(),
  153. 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0,
  154. 'disabled' => $group->countDisabled(),
  155. 'canAdd' => $group->canAddUser(),
  156. 'canRemove' => $group->canRemoveUser(),
  157. );
  158. }
  159. /**
  160. * sorts the result array, if applicable
  161. * @param array $entries the result array, by reference
  162. * @param array $sortKeys the array containing the sort keys
  163. * @param return null
  164. */
  165. private function sort(&$entries, $sortKeys) {
  166. if ($this->sorting === self::SORT_USERCOUNT) {
  167. array_multisort($sortKeys, SORT_DESC, $entries);
  168. } else if ($this->sorting === self::SORT_GROUPNAME) {
  169. array_multisort($sortKeys, SORT_ASC, $entries);
  170. }
  171. }
  172. /**
  173. * returns the available groups
  174. * @param string $search a search string
  175. * @return \OCP\IGroup[]
  176. */
  177. public function getGroups($search = '') {
  178. if($this->isAdmin) {
  179. return $this->groupManager->search($search);
  180. } else {
  181. $userObject = $this->userSession->getUser();
  182. if($userObject !== null) {
  183. $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject);
  184. } else {
  185. $groups = [];
  186. }
  187. return $groups;
  188. }
  189. }
  190. }