group.php 8.3 KB

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