group.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Georg Ehrke <georg@owncloud.com>
  9. * @author goodkiller <markopraakli@gmail.com>
  10. * @author Jakob Sack <mail@jakobsack.de>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author macjohnny <estebanmarin@gmx.ch>
  13. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Qingping Hou <dave2008713@gmail.com>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Robin McCorkell <robin@mccorkell.me.uk>
  18. * @author Thomas Müller <thomas.mueller@tmit.eu>
  19. *
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. /**
  36. * This class provides all methods needed for managing groups.
  37. *
  38. * Note that &run is deprecated and won't work anymore.
  39. * Hooks provided:
  40. * pre_createGroup(&run, gid)
  41. * post_createGroup(gid)
  42. * pre_deleteGroup(&run, gid)
  43. * post_deleteGroup(gid)
  44. * pre_addToGroup(&run, uid, gid)
  45. * post_addToGroup(uid, gid)
  46. * pre_removeFromGroup(&run, uid, gid)
  47. * post_removeFromGroup(uid, gid)
  48. */
  49. class OC_Group {
  50. /**
  51. * @return \OC\Group\Manager
  52. * @deprecated Use \OC::$server->getGroupManager();
  53. */
  54. public static function getManager() {
  55. return \OC::$server->getGroupManager();
  56. }
  57. /**
  58. * @return \OC\User\Manager
  59. * @deprecated Use \OC::$server->getUserManager()
  60. */
  61. private static function getUserManager() {
  62. return \OC::$server->getUserManager();
  63. }
  64. /**
  65. * set the group backend
  66. * @param \OC_Group_Backend $backend The backend to use for user managment
  67. * @return bool
  68. */
  69. public static function useBackend($backend) {
  70. self::getManager()->addBackend($backend);
  71. return true;
  72. }
  73. /**
  74. * remove all used backends
  75. */
  76. public static function clearBackends() {
  77. self::getManager()->clearBackends();
  78. }
  79. /**
  80. * Try to create a new group
  81. * @param string $gid The name of the group to create
  82. * @return bool
  83. *
  84. * Tries to create a new group. If the group name already exists, false will
  85. * be returned. Basic checking of Group name
  86. * @deprecated Use \OC::$server->getGroupManager()->createGroup() instead
  87. */
  88. public static function createGroup($gid) {
  89. if (self::getManager()->createGroup($gid)) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. /**
  96. * delete a group
  97. * @param string $gid gid of the group to delete
  98. * @return bool
  99. *
  100. * Deletes a group and removes it from the group_user-table
  101. * @deprecated Use \OC::$server->getGroupManager()->delete() instead
  102. */
  103. public static function deleteGroup($gid) {
  104. $group = self::getManager()->get($gid);
  105. if ($group) {
  106. if ($group->delete()) {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. /**
  113. * is user in group?
  114. * @param string $uid uid of the user
  115. * @param string $gid gid of the group
  116. * @return bool
  117. *
  118. * Checks whether the user is member of a group or not.
  119. * @deprecated Use \OC::$server->getGroupManager->inGroup($user);
  120. */
  121. public static function inGroup($uid, $gid) {
  122. $group = self::getManager()->get($gid);
  123. $user = self::getUserManager()->get($uid);
  124. if ($group and $user) {
  125. return $group->inGroup($user);
  126. }
  127. return false;
  128. }
  129. /**
  130. * Add a user to a group
  131. * @param string $uid Name of the user to add to group
  132. * @param string $gid Name of the group in which add the user
  133. * @return bool
  134. *
  135. * Adds a user to a group.
  136. * @deprecated Use \OC::$server->getGroupManager->addUser();
  137. */
  138. public static function addToGroup($uid, $gid) {
  139. $group = self::getManager()->get($gid);
  140. $user = self::getUserManager()->get($uid);
  141. if ($group and $user) {
  142. $group->addUser($user);
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. /**
  149. * Removes a user from a group
  150. * @param string $uid Name of the user to remove from group
  151. * @param string $gid Name of the group from which remove the user
  152. * @return bool
  153. *
  154. * removes the user from a group.
  155. */
  156. public static function removeFromGroup($uid, $gid) {
  157. $group = self::getManager()->get($gid);
  158. $user = self::getUserManager()->get($uid);
  159. if ($group and $user) {
  160. OC_Hook::emit("OC_Group", "pre_removeFromGroup", array("run" => true, "uid" => $uid, "gid" => $gid));
  161. $group->removeUser($user);
  162. OC_Hook::emit("OC_User", "post_removeFromGroup", array("uid" => $uid, "gid" => $gid));
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. }
  168. /**
  169. * Get all groups a user belongs to
  170. * @param string $uid Name of the user
  171. * @return array an array of group names
  172. *
  173. * This function fetches all groups a user belongs to. It does not check
  174. * if the user exists at all.
  175. * @deprecated Use \OC::$server->getGroupManager->getUserGroupIds($user)
  176. */
  177. public static function getUserGroups($uid) {
  178. $user = self::getUserManager()->get($uid);
  179. if ($user) {
  180. return self::getManager()->getUserGroupIds($user);
  181. } else {
  182. return array();
  183. }
  184. }
  185. /**
  186. * get a list of all groups
  187. * @param string $search
  188. * @param int|null $limit
  189. * @param int|null $offset
  190. * @return array an array of group names
  191. *
  192. * Returns a list with all groups
  193. */
  194. public static function getGroups($search = '', $limit = null, $offset = null) {
  195. $groups = self::getManager()->search($search, $limit, $offset);
  196. $groupIds = array();
  197. foreach ($groups as $group) {
  198. $groupIds[] = $group->getGID();
  199. }
  200. return $groupIds;
  201. }
  202. /**
  203. * check if a group exists
  204. *
  205. * @param string $gid
  206. * @return bool
  207. * @deprecated Use \OC::$server->getGroupManager->groupExists($gid)
  208. */
  209. public static function groupExists($gid) {
  210. return self::getManager()->groupExists($gid);
  211. }
  212. /**
  213. * get a list of all users in a group
  214. * @param string $gid
  215. * @param string $search
  216. * @param int $limit
  217. * @param int $offset
  218. * @return array an array of user ids
  219. */
  220. public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  221. $group = self::getManager()->get($gid);
  222. if ($group) {
  223. $users = $group->searchUsers($search, $limit, $offset);
  224. $userIds = array();
  225. foreach ($users as $user) {
  226. $userIds[] = $user->getUID();
  227. }
  228. return $userIds;
  229. } else {
  230. return array();
  231. }
  232. }
  233. /**
  234. * get a list of all users in several groups
  235. * @param string[] $gids
  236. * @param string $search
  237. * @param int $limit
  238. * @param int $offset
  239. * @return array an array of user ids
  240. */
  241. public static function usersInGroups($gids, $search = '', $limit = -1, $offset = 0) {
  242. $users = array();
  243. foreach ($gids as $gid) {
  244. // TODO Need to apply limits to groups as total
  245. $users = array_merge(array_diff(self::usersInGroup($gid, $search, $limit, $offset), $users), $users);
  246. }
  247. return $users;
  248. }
  249. /**
  250. * get a list of all display names in a group
  251. * @param string $gid
  252. * @param string $search
  253. * @param int $limit
  254. * @param int $offset
  255. * @return array an array of display names (value) and user ids(key)
  256. * @deprecated Use \OC::$server->getGroupManager->displayNamesInGroup($gid, $search, $limit, $offset)
  257. */
  258. public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  259. return self::getManager()->displayNamesInGroup($gid, $search, $limit, $offset);
  260. }
  261. /**
  262. * get a list of all display names in several groups
  263. * @param array $gids
  264. * @param string $search
  265. * @param int $limit
  266. * @param int $offset
  267. * @return array an array of display names (Key) user ids (value)
  268. */
  269. public static function displayNamesInGroups($gids, $search = '', $limit = -1, $offset = 0) {
  270. $displayNames = array();
  271. foreach ($gids as $gid) {
  272. // TODO Need to apply limits to groups as total
  273. $diff = array_diff(
  274. self::displayNamesInGroup($gid, $search, $limit, $offset),
  275. $displayNames
  276. );
  277. if ($diff) {
  278. // A fix for LDAP users. array_merge loses keys...
  279. $displayNames = $diff + $displayNames;
  280. }
  281. }
  282. return $displayNames;
  283. }
  284. }