group.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-"
  66. */
  67. public static function createGroup( $gid ) {
  68. // Check the name for bad characters
  69. // Allowed are: "a-z", "A-Z", "0-9" and "_.@-"
  70. if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $gid )) {
  71. return false;
  72. }
  73. // No empty group names!
  74. if( !$gid ) {
  75. return false;
  76. }
  77. // No duplicate group names
  78. if( in_array( $gid, self::getGroups())) {
  79. return false;
  80. }
  81. $run = true;
  82. OC_Hook::emit( "OC_Group", "pre_createGroup", array( "run" => &$run, "gid" => $gid ));
  83. if($run) {
  84. //create the group in the first backend that supports creating groups
  85. foreach(self::$_usedBackends as $backend) {
  86. if(!$backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP))
  87. continue;
  88. $backend->createGroup($gid);
  89. OC_Hook::emit( "OC_User", "post_createGroup", array( "gid" => $gid ));
  90. return true;
  91. }
  92. return false;
  93. }else{
  94. return false;
  95. }
  96. }
  97. /**
  98. * @brief delete a group
  99. * @param string $gid gid of the group to delete
  100. * @return bool
  101. *
  102. * Deletes a group and removes it from the group_user-table
  103. */
  104. public static function deleteGroup( $gid ) {
  105. // Prevent users from deleting group admin
  106. if( $gid == "admin" ) {
  107. return false;
  108. }
  109. $run = true;
  110. OC_Hook::emit( "OC_Group", "pre_deleteGroup", array( "run" => &$run, "gid" => $gid ));
  111. if($run) {
  112. //delete the group from all backends
  113. foreach(self::$_usedBackends as $backend) {
  114. if(!$backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP))
  115. continue;
  116. $backend->deleteGroup($gid);
  117. OC_Hook::emit( "OC_User", "post_deleteGroup", array( "gid" => $gid ));
  118. return true;
  119. }
  120. return false;
  121. }else{
  122. return false;
  123. }
  124. }
  125. /**
  126. * @brief is user in group?
  127. * @param string $uid uid of the user
  128. * @param string $gid gid of the group
  129. * @return bool
  130. *
  131. * Checks whether the user is member of a group or not.
  132. */
  133. public static function inGroup( $uid, $gid ) {
  134. foreach(self::$_usedBackends as $backend) {
  135. if($backend->inGroup($uid,$gid)) {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. /**
  142. * @brief Add a user to a group
  143. * @param string $uid Name of the user to add to group
  144. * @param string $gid Name of the group in which add the user
  145. * @return bool
  146. *
  147. * Adds a user to a group.
  148. */
  149. public static function addToGroup( $uid, $gid ) {
  150. // Does the group exist?
  151. if( !OC_Group::groupExists($gid)) {
  152. return false;
  153. }
  154. // Go go go
  155. $run = true;
  156. OC_Hook::emit( "OC_Group", "pre_addToGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
  157. if($run) {
  158. $success=false;
  159. //add the user to the all backends that have the group
  160. foreach(self::$_usedBackends as $backend) {
  161. if(!$backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP))
  162. continue;
  163. if($backend->groupExists($gid)) {
  164. $success|=$backend->addToGroup($uid, $gid);
  165. }
  166. }
  167. if($success) {
  168. OC_Hook::emit( "OC_User", "post_addToGroup", array( "uid" => $uid, "gid" => $gid ));
  169. }
  170. return $success;
  171. }else{
  172. return false;
  173. }
  174. }
  175. /**
  176. * @brief Removes a user from a group
  177. * @param string $uid Name of the user to remove from group
  178. * @param string $gid Name of the group from which remove the user
  179. * @return bool
  180. *
  181. * removes the user from a group.
  182. */
  183. public static function removeFromGroup( $uid, $gid ) {
  184. $run = true;
  185. OC_Hook::emit( "OC_Group", "pre_removeFromGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
  186. if($run) {
  187. //remove the user from the all backends that have the group
  188. foreach(self::$_usedBackends as $backend) {
  189. if(!$backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP))
  190. continue;
  191. $backend->removeFromGroup($uid, $gid);
  192. OC_Hook::emit( "OC_User", "post_removeFromGroup", array( "uid" => $uid, "gid" => $gid ));
  193. }
  194. return true;
  195. }else{
  196. return false;
  197. }
  198. }
  199. /**
  200. * @brief Get all groups a user belongs to
  201. * @param string $uid Name of the user
  202. * @return array with group names
  203. *
  204. * This function fetches all groups a user belongs to. It does not check
  205. * if the user exists at all.
  206. */
  207. public static function getUserGroups( $uid ) {
  208. $groups=array();
  209. foreach(self::$_usedBackends as $backend) {
  210. $groups=array_merge($backend->getUserGroups($uid),$groups);
  211. }
  212. asort($groups);
  213. return $groups;
  214. }
  215. /**
  216. * @brief get a list of all groups
  217. * @returns array with group names
  218. *
  219. * Returns a list with all groups
  220. */
  221. public static function getGroups($search = '', $limit = -1, $offset = 0) {
  222. $groups = array();
  223. foreach (self::$_usedBackends as $backend) {
  224. $groups = array_merge($backend->getGroups($search, $limit, $offset), $groups);
  225. }
  226. asort($groups);
  227. return $groups;
  228. }
  229. /**
  230. * check if a group exists
  231. * @param string $gid
  232. * @return bool
  233. */
  234. public static function groupExists($gid) {
  235. foreach(self::$_usedBackends as $backend) {
  236. if ($backend->groupExists($gid)) {
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. /**
  243. * @brief get a list of all users in a group
  244. * @returns array with user ids
  245. */
  246. public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  247. $users=array();
  248. foreach(self::$_usedBackends as $backend) {
  249. $users = array_merge($backend->usersInGroup($gid, $search, $limit, $offset), $users);
  250. }
  251. return $users;
  252. }
  253. /**
  254. * @brief get a list of all users in several groups
  255. * @param array $gids
  256. * @param string $search
  257. * @param int $limit
  258. * @param int $offset
  259. * @return array with user ids
  260. */
  261. public static function usersInGroups($gids, $search = '', $limit = -1, $offset = 0) {
  262. $users = array();
  263. foreach ($gids as $gid) {
  264. // TODO Need to apply limits to groups as total
  265. $users = array_merge(array_diff(self::usersInGroup($gid, $search, $limit, $offset), $users), $users);
  266. }
  267. return $users;
  268. }
  269. }