group.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * @return \OC\Group\Manager
  38. * @deprecated Use \OC::$server->getGroupManager();
  39. */
  40. public static function getManager() {
  41. return \OC::$server->getGroupManager();
  42. }
  43. /**
  44. * @return \OC\User\Manager
  45. * @deprecated Use \OC::$server->getUserManager()
  46. */
  47. private static function getUserManager() {
  48. return \OC::$server->getUserManager();
  49. }
  50. /**
  51. * set the group backend
  52. * @param \OC_Group_Backend $backend The backend to use for user managment
  53. * @return bool
  54. */
  55. public static function useBackend($backend) {
  56. self::getManager()->addBackend($backend);
  57. return true;
  58. }
  59. /**
  60. * remove all used backends
  61. */
  62. public static function clearBackends() {
  63. self::getManager()->clearBackends();
  64. }
  65. /**
  66. * Try to create a new group
  67. * @param string $gid The name of the group to create
  68. * @return bool
  69. *
  70. * Tries to create a new group. If the group name already exists, false will
  71. * be returned. Basic checking of Group name
  72. * @deprecated Use \OC::$server->getGroupManager()->createGroup() instead
  73. */
  74. public static function createGroup($gid) {
  75. if (self::getManager()->createGroup($gid)) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81. /**
  82. * delete a group
  83. * @param string $gid gid of the group to delete
  84. * @return bool
  85. *
  86. * Deletes a group and removes it from the group_user-table
  87. * @deprecated Use \OC::$server->getGroupManager()->delete() instead
  88. */
  89. public static function deleteGroup($gid) {
  90. $group = self::getManager()->get($gid);
  91. if ($group) {
  92. if ($group->delete()) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. /**
  99. * is user in group?
  100. * @param string $uid uid of the user
  101. * @param string $gid gid of the group
  102. * @return bool
  103. *
  104. * Checks whether the user is member of a group or not.
  105. * @deprecated Use \OC::$server->getGroupManager->inGroup($user);
  106. */
  107. public static function inGroup($uid, $gid) {
  108. $group = self::getManager()->get($gid);
  109. $user = self::getUserManager()->get($uid);
  110. if ($group and $user) {
  111. return $group->inGroup($user);
  112. }
  113. return false;
  114. }
  115. /**
  116. * Add a user to a group
  117. * @param string $uid Name of the user to add to group
  118. * @param string $gid Name of the group in which add the user
  119. * @return bool
  120. *
  121. * Adds a user to a group.
  122. * @deprecated Use \OC::$server->getGroupManager->addUser();
  123. */
  124. public static function addToGroup($uid, $gid) {
  125. $group = self::getManager()->get($gid);
  126. $user = self::getUserManager()->get($uid);
  127. if ($group and $user) {
  128. $group->addUser($user);
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. }
  134. /**
  135. * Removes a user from a group
  136. * @param string $uid Name of the user to remove from group
  137. * @param string $gid Name of the group from which remove the user
  138. * @return bool
  139. *
  140. * removes the user from a group.
  141. */
  142. public static function removeFromGroup($uid, $gid) {
  143. $group = self::getManager()->get($gid);
  144. $user = self::getUserManager()->get($uid);
  145. if ($group and $user) {
  146. OC_Hook::emit("OC_Group", "pre_removeFromGroup", array("run" => true, "uid" => $uid, "gid" => $gid));
  147. $group->removeUser($user);
  148. OC_Hook::emit("OC_User", "post_removeFromGroup", array("uid" => $uid, "gid" => $gid));
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. /**
  155. * Get all groups a user belongs to
  156. * @param string $uid Name of the user
  157. * @return array an array of group names
  158. *
  159. * This function fetches all groups a user belongs to. It does not check
  160. * if the user exists at all.
  161. * @deprecated Use \OC::$server->getGroupManager->getuserGroupIds($user)
  162. */
  163. public static function getUserGroups($uid) {
  164. $user = self::getUserManager()->get($uid);
  165. if ($user) {
  166. return self::getManager()->getUserGroupIds($user);
  167. } else {
  168. return array();
  169. }
  170. }
  171. /**
  172. * get a list of all groups
  173. * @param string $search
  174. * @param int|null $limit
  175. * @param int|null $offset
  176. * @return array an array of group names
  177. *
  178. * Returns a list with all groups
  179. */
  180. public static function getGroups($search = '', $limit = null, $offset = null) {
  181. $groups = self::getManager()->search($search, $limit, $offset);
  182. $groupIds = array();
  183. foreach ($groups as $group) {
  184. $groupIds[] = $group->getGID();
  185. }
  186. return $groupIds;
  187. }
  188. /**
  189. * check if a group exists
  190. *
  191. * @param string $gid
  192. * @return bool
  193. * @deprecated Use \OC::$server->getGroupManager->groupExists($gid)
  194. */
  195. public static function groupExists($gid) {
  196. return self::getManager()->groupExists($gid);
  197. }
  198. /**
  199. * get a list of all users in a group
  200. * @param string $gid
  201. * @param string $search
  202. * @param int $limit
  203. * @param int $offset
  204. * @return array an array of user ids
  205. */
  206. public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  207. $group = self::getManager()->get($gid);
  208. if ($group) {
  209. $users = $group->searchUsers($search, $limit, $offset);
  210. $userIds = array();
  211. foreach ($users as $user) {
  212. $userIds[] = $user->getUID();
  213. }
  214. return $userIds;
  215. } else {
  216. return array();
  217. }
  218. }
  219. /**
  220. * get a list of all users in several groups
  221. * @param string[] $gids
  222. * @param string $search
  223. * @param int $limit
  224. * @param int $offset
  225. * @return array an array of user ids
  226. */
  227. public static function usersInGroups($gids, $search = '', $limit = -1, $offset = 0) {
  228. $users = array();
  229. foreach ($gids as $gid) {
  230. // TODO Need to apply limits to groups as total
  231. $users = array_merge(array_diff(self::usersInGroup($gid, $search, $limit, $offset), $users), $users);
  232. }
  233. return $users;
  234. }
  235. /**
  236. * get a list of all display names in a group
  237. * @param string $gid
  238. * @param string $search
  239. * @param int $limit
  240. * @param int $offset
  241. * @return array an array of display names (value) and user ids(key)
  242. * @deprecated Use \OC::$server->getGroupManager->displayNamesInGroup($gid, $search, $limit, $offset)
  243. */
  244. public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  245. return self::getManager()->displayNamesInGroup($gid, $search, $limit, $offset);
  246. }
  247. /**
  248. * get a list of all display names in several groups
  249. * @param array $gids
  250. * @param string $search
  251. * @param int $limit
  252. * @param int $offset
  253. * @return array an array of display names (Key) user ids (value)
  254. */
  255. public static function displayNamesInGroups($gids, $search = '', $limit = -1, $offset = 0) {
  256. $displayNames = array();
  257. foreach ($gids as $gid) {
  258. // TODO Need to apply limits to groups as total
  259. $diff = array_diff(
  260. self::displayNamesInGroup($gid, $search, $limit, $offset),
  261. $displayNames
  262. );
  263. if ($diff) {
  264. $displayNames = array_merge($diff, $displayNames);
  265. }
  266. }
  267. return $displayNames;
  268. }
  269. }