You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

group.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. // The backend used for group management
  37. /**
  38. * @var OC_Group_Interface[]
  39. */
  40. private static $_usedBackends = array();
  41. /**
  42. * @brief set the group backend
  43. * @param string $backend The backend to use for user managment
  44. * @return bool
  45. */
  46. public static function useBackend( $backend ) {
  47. if($backend instanceof OC_Group_Interface) {
  48. self::$_usedBackends[]=$backend;
  49. }
  50. }
  51. /**
  52. * remove all used backends
  53. */
  54. public static function clearBackends() {
  55. self::$_usedBackends=array();
  56. }
  57. /**
  58. * @brief Try to create a new group
  59. * @param string $gid The name of the group to create
  60. * @return bool
  61. *
  62. * Tries to create a new group. If the group name already exists, false will
  63. * be returned. Basic checking of Group name
  64. */
  65. public static function createGroup( $gid ) {
  66. // No empty group names!
  67. if( !$gid ) {
  68. return false;
  69. }
  70. // No duplicate group names
  71. if( in_array( $gid, self::getGroups())) {
  72. return false;
  73. }
  74. $run = true;
  75. OC_Hook::emit( "OC_Group", "pre_createGroup", array( "run" => &$run, "gid" => $gid ));
  76. if($run) {
  77. //create the group in the first backend that supports creating groups
  78. foreach(self::$_usedBackends as $backend) {
  79. if(!$backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP))
  80. continue;
  81. $backend->createGroup($gid);
  82. OC_Hook::emit( "OC_User", "post_createGroup", array( "gid" => $gid ));
  83. return true;
  84. }
  85. return false;
  86. }else{
  87. return false;
  88. }
  89. }
  90. /**
  91. * @brief delete a group
  92. * @param string $gid gid of the group to delete
  93. * @return bool
  94. *
  95. * Deletes a group and removes it from the group_user-table
  96. */
  97. public static function deleteGroup( $gid ) {
  98. // Prevent users from deleting group admin
  99. if( $gid == "admin" ) {
  100. return false;
  101. }
  102. $run = true;
  103. OC_Hook::emit( "OC_Group", "pre_deleteGroup", array( "run" => &$run, "gid" => $gid ));
  104. if($run) {
  105. //delete the group from all backends
  106. foreach(self::$_usedBackends as $backend) {
  107. if(!$backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP))
  108. continue;
  109. $backend->deleteGroup($gid);
  110. OC_Hook::emit( "OC_User", "post_deleteGroup", array( "gid" => $gid ));
  111. return true;
  112. }
  113. return false;
  114. }else{
  115. return false;
  116. }
  117. }
  118. /**
  119. * @brief is user in group?
  120. * @param string $uid uid of the user
  121. * @param string $gid gid of the group
  122. * @return bool
  123. *
  124. * Checks whether the user is member of a group or not.
  125. */
  126. public static function inGroup( $uid, $gid ) {
  127. foreach(self::$_usedBackends as $backend) {
  128. if($backend->inGroup($uid, $gid)) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. /**
  135. * @brief Add a user to a group
  136. * @param string $uid Name of the user to add to group
  137. * @param string $gid Name of the group in which add the user
  138. * @return bool
  139. *
  140. * Adds a user to a group.
  141. */
  142. public static function addToGroup( $uid, $gid ) {
  143. // Does the group exist?
  144. if( !OC_Group::groupExists($gid)) {
  145. return false;
  146. }
  147. // Go go go
  148. $run = true;
  149. OC_Hook::emit( "OC_Group", "pre_addToGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
  150. if($run) {
  151. $success=false;
  152. //add the user to the all backends that have the group
  153. foreach(self::$_usedBackends as $backend) {
  154. if(!$backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP))
  155. continue;
  156. if($backend->groupExists($gid)) {
  157. $success|=$backend->addToGroup($uid, $gid);
  158. }
  159. }
  160. if($success) {
  161. OC_Hook::emit( "OC_User", "post_addToGroup", array( "uid" => $uid, "gid" => $gid ));
  162. }
  163. return $success;
  164. }else{
  165. return false;
  166. }
  167. }
  168. /**
  169. * @brief Removes a user from a group
  170. * @param string $uid Name of the user to remove from group
  171. * @param string $gid Name of the group from which remove the user
  172. * @return bool
  173. *
  174. * removes the user from a group.
  175. */
  176. public static function removeFromGroup( $uid, $gid ) {
  177. $run = true;
  178. OC_Hook::emit( "OC_Group", "pre_removeFromGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
  179. if($run) {
  180. //remove the user from the all backends that have the group
  181. foreach(self::$_usedBackends as $backend) {
  182. if(!$backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP))
  183. continue;
  184. $backend->removeFromGroup($uid, $gid);
  185. OC_Hook::emit( "OC_User", "post_removeFromGroup", array( "uid" => $uid, "gid" => $gid ));
  186. }
  187. return true;
  188. }else{
  189. return false;
  190. }
  191. }
  192. /**
  193. * @brief Get all groups a user belongs to
  194. * @param string $uid Name of the user
  195. * @return array with group names
  196. *
  197. * This function fetches all groups a user belongs to. It does not check
  198. * if the user exists at all.
  199. */
  200. public static function getUserGroups( $uid ) {
  201. $groups=array();
  202. foreach(self::$_usedBackends as $backend) {
  203. $groups=array_merge($backend->getUserGroups($uid), $groups);
  204. }
  205. asort($groups);
  206. return $groups;
  207. }
  208. /**
  209. * @brief get a list of all groups
  210. * @returns array with group names
  211. *
  212. * Returns a list with all groups
  213. */
  214. public static function getGroups($search = '', $limit = -1, $offset = 0) {
  215. $groups = array();
  216. foreach (self::$_usedBackends as $backend) {
  217. $groups = array_merge($backend->getGroups($search, $limit, $offset), $groups);
  218. }
  219. asort($groups);
  220. return $groups;
  221. }
  222. /**
  223. * check if a group exists
  224. * @param string $gid
  225. * @return bool
  226. */
  227. public static function groupExists($gid) {
  228. foreach(self::$_usedBackends as $backend) {
  229. if ($backend->groupExists($gid)) {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. /**
  236. * @brief get a list of all users in a group
  237. * @returns array with user ids
  238. */
  239. public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  240. $users=array();
  241. foreach(self::$_usedBackends as $backend) {
  242. $users = array_merge($backend->usersInGroup($gid, $search, $limit, $offset), $users);
  243. }
  244. return $users;
  245. }
  246. /**
  247. * @brief get a list of all users in several groups
  248. * @param array $gids
  249. * @param string $search
  250. * @param int $limit
  251. * @param int $offset
  252. * @return array with user ids
  253. */
  254. public static function usersInGroups($gids, $search = '', $limit = -1, $offset = 0) {
  255. $users = array();
  256. foreach ($gids as $gid) {
  257. // TODO Need to apply limits to groups as total
  258. $users = array_merge(array_diff(self::usersInGroup($gid, $search, $limit, $offset), $users), $users);
  259. }
  260. return $users;
  261. }
  262. /**
  263. * @brief get a list of all display names in a group
  264. * @returns array with display names (value) and user ids(key)
  265. */
  266. public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  267. $displayNames=array();
  268. foreach(self::$_usedBackends as $backend) {
  269. if($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) {
  270. $displayNames = array_merge($backend->displayNamesInGroup($gid, $search, $limit, $offset), $displayNames);
  271. } else {
  272. $users = $backend->usersInGroup($gid, $search, $limit, $offset);
  273. $names = array_combine($users, $users);
  274. $displayNames = array_merge($names, $displayNames);
  275. }
  276. }
  277. return $displayNames;
  278. }
  279. /**
  280. * @brief get a list of all display names in several groups
  281. * @param array $gids
  282. * @param string $search
  283. * @param int $limit
  284. * @param int $offset
  285. * @return array with display names (Key) user ids (value)
  286. */
  287. public static function displayNamesInGroups($gids, $search = '', $limit = -1, $offset = 0) {
  288. $displayNames = array();
  289. foreach ($gids as $gid) {
  290. // TODO Need to apply limits to groups as total
  291. $diff = array_diff(
  292. self::displayNamesInGroup($gid, $search, $limit, $offset),
  293. $displayNames
  294. );
  295. if ($diff) {
  296. $displayNames = array_merge($diff, $displayNames);
  297. }
  298. }
  299. return $displayNames;
  300. }
  301. }