group.php 7.8 KB

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