group.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2010 Frank Karlitschek karlitschek@kde.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 user management
  37. private static $_backend;
  38. // Backends available (except database)
  39. private static $_backends = array();
  40. /**
  41. * @brief registers backend
  42. * @param $name name of the backend
  43. * @returns true/false
  44. *
  45. * Makes a list of backends that can be used by other modules
  46. */
  47. public static function registerBackend( $name ){
  48. self::$_backends[] = $name;
  49. return true;
  50. }
  51. /**
  52. * @brief gets available backends
  53. * @returns array of backends
  54. *
  55. * Returns the names of all backends.
  56. */
  57. public static function getBackends(){
  58. return self::$_backends;
  59. }
  60. /**
  61. * @brief set the group backend
  62. * @param string $backend The backend to use for user managment
  63. * @returns true/false
  64. */
  65. public static function setBackend( $backend = 'database' ){
  66. // You'll never know what happens
  67. if( null === $backend OR !is_string( $backend )){
  68. $backend = 'database';
  69. }
  70. // Load backend
  71. switch( $backend ){
  72. case 'database':
  73. case 'mysql':
  74. case 'sqlite':
  75. self::$_backend = new OC_Group_Database();
  76. break;
  77. default:
  78. $className = 'OC_GROUP_' . strToUpper($backend);
  79. self::$_backend = new $className();
  80. break;
  81. }
  82. }
  83. /**
  84. * @brief Try to create a new group
  85. * @param $gid The name of the group to create
  86. * @returns true/false
  87. *
  88. * Trys to create a new group. If the group name already exists, false will
  89. * be returned. Basic checking of Group name
  90. *
  91. * Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-"
  92. */
  93. public static function createGroup( $gid ){
  94. // Check the name for bad characters
  95. // Allowed are: "a-z", "A-Z", "0-9" and "_.@-"
  96. if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $gid )){
  97. return false;
  98. }
  99. // No empty group names!
  100. if( !$gid ){
  101. return false;
  102. }
  103. // No duplicate group names
  104. if( in_array( $gid, self::getGroups())){
  105. return false;
  106. }
  107. $run = true;
  108. OC_Hook::emit( "OC_Group", "pre_createGroup", array( "run" => &$run, "gid" => $gid ));
  109. if( $run && self::$_backend->createGroup( $gid )){
  110. OC_Hook::emit( "OC_Group", "post_createGroup", array( "gid" => $gid ));
  111. return true;
  112. }
  113. else{
  114. return false;
  115. }
  116. }
  117. /**
  118. * @brief delete a group
  119. * @param $gid gid of the group to delete
  120. * @returns true/false
  121. *
  122. * Deletes a group and removes it from the group_user-table
  123. */
  124. public static function deleteGroup( $gid ){
  125. // Prevent users from deleting group admin
  126. if( $gid == "admin" ){
  127. return false;
  128. }
  129. $run = true;
  130. OC_Hook::emit( "OC_Group", "pre_deleteGroup", array( "run" => &$run, "gid" => $gid ));
  131. if( $run && self::$_backend->deleteGroup( $gid )){
  132. OC_Hook::emit( "OC_Group", "post_deleteGroup", array( "gid" => $gid ));
  133. return true;
  134. }
  135. else{
  136. return false;
  137. }
  138. }
  139. /**
  140. * @brief is user in group?
  141. * @param $uid uid of the user
  142. * @param $gid gid of the group
  143. * @returns true/false
  144. *
  145. * Checks whether the user is member of a group or not.
  146. */
  147. public static function inGroup( $uid, $gid ){
  148. return self::$_backend->inGroup($uid, $gid);
  149. }
  150. /**
  151. * @brief Add a user to a group
  152. * @param $uid Name of the user to add to group
  153. * @param $gid Name of the group in which add the user
  154. * @returns true/false
  155. *
  156. * Adds a user to a group.
  157. */
  158. public static function addToGroup( $uid, $gid ){
  159. // Does the user exist?
  160. if( !OC_User::userExists($uid)){
  161. return false;
  162. }
  163. // Does the group exist?
  164. if( !OC_Group::groupExists($gid)){
  165. return false;
  166. }
  167. // Go go go
  168. $run = true;
  169. OC_Hook::emit( "OC_Group", "pre_addToGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
  170. if( $run && self::$_backend->addToGroup( $uid, $gid )){
  171. OC_Hook::emit( "OC_Group", "post_addToGroup", array( "uid" => $uid, "gid" => $gid ));
  172. return true;
  173. }
  174. else{
  175. return false;
  176. }
  177. }
  178. /**
  179. * @brief Removes a user from a group
  180. * @param $uid Name of the user to remove from group
  181. * @param $gid Name of the group from which remove the user
  182. * @returns true/false
  183. *
  184. * removes the user from a group.
  185. */
  186. public static function removeFromGroup( $uid, $gid ){
  187. $run = true;
  188. OC_Hook::emit( "OC_Group", "pre_removeFromGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
  189. if( $run && self::$_backend->removeFromGroup( $uid, $gid )){
  190. OC_Hook::emit( "OC_Group", "post_removeFromGroup", array( "uid" => $uid, "gid" => $gid ));
  191. return true;
  192. }
  193. else{
  194. return false;
  195. }
  196. }
  197. /**
  198. * @brief Get all groups a user belongs to
  199. * @param $uid Name of the user
  200. * @returns array with group names
  201. *
  202. * This function fetches all groups a user belongs to. It does not check
  203. * if the user exists at all.
  204. */
  205. public static function getUserGroups( $uid ){
  206. return self::$_backend->getUserGroups($uid);
  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(){
  215. return self::$_backend->getGroups();
  216. }
  217. /**
  218. * check if a group exists
  219. * @param string $gid
  220. * @return bool
  221. */
  222. public static function groupExists($gid){
  223. return in_array( $gid, self::getGroups());
  224. }
  225. /**
  226. * @brief get a list of all users in a group
  227. * @returns array with user ids
  228. */
  229. public static function usersInGroup($gid){
  230. return self::$_backend->usersInGroup($gid);
  231. }
  232. }