group.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 group management
  37. private static $_usedBackends = array();
  38. /**
  39. * @brief set the group backend
  40. * @param string $backend The backend to use for user managment
  41. * @returns true/false
  42. */
  43. public static function useBackend( $backend ){
  44. if($backend instanceof OC_Group_Backend){
  45. self::$_usedBackends[]=$backend;
  46. }
  47. }
  48. /**
  49. * remove all used backends
  50. */
  51. public static function clearBackends(){
  52. self::$_usedBackends=array();
  53. }
  54. /**
  55. * @brief Try to create a new group
  56. * @param $gid The name of the group to create
  57. * @returns true/false
  58. *
  59. * Trys to create a new group. If the group name already exists, false will
  60. * be returned. Basic checking of Group name
  61. *
  62. * Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-"
  63. */
  64. public static function createGroup( $gid ){
  65. // Check the name for bad characters
  66. // Allowed are: "a-z", "A-Z", "0-9" and "_.@-"
  67. if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $gid )){
  68. return false;
  69. }
  70. // No empty group names!
  71. if( !$gid ){
  72. return false;
  73. }
  74. // No duplicate group names
  75. if( in_array( $gid, self::getGroups())){
  76. return false;
  77. }
  78. $run = true;
  79. OC_Hook::emit( "OC_Group", "pre_createGroup", array( "run" => &$run, "gid" => $gid ));
  80. if($run){
  81. //create the user in the first backend that supports creating users
  82. foreach(self::$_usedBackends as $backend){
  83. if(!$backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP))
  84. continue;
  85. $backend->createGroup($gid);
  86. OC_Hook::emit( "OC_User", "post_createGroup", array( "gid" => $gid ));
  87. return true;
  88. }
  89. }else{
  90. return false;
  91. }
  92. }
  93. /**
  94. * @brief delete a group
  95. * @param $gid gid of the group to delete
  96. * @returns true/false
  97. *
  98. * Deletes a group and removes it from the group_user-table
  99. */
  100. public static function deleteGroup( $gid ){
  101. // Prevent users from deleting group admin
  102. if( $gid == "admin" ){
  103. return false;
  104. }
  105. $run = true;
  106. OC_Hook::emit( "OC_Group", "pre_deleteGroup", array( "run" => &$run, "gid" => $gid ));
  107. if($run){
  108. //delete the group from all backends
  109. foreach(self::$_usedBackends as $backend){
  110. if(!$backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP))
  111. continue;
  112. $backend->deleteGroup($gid);
  113. OC_Hook::emit( "OC_User", "post_deleteGroup", array( "gid" => $gid ));
  114. return true;
  115. }
  116. }else{
  117. return false;
  118. }
  119. }
  120. /**
  121. * @brief is user in group?
  122. * @param $uid uid of the user
  123. * @param $gid gid of the group
  124. * @returns true/false
  125. *
  126. * Checks whether the user is member of a group or not.
  127. */
  128. public static function inGroup( $uid, $gid ){
  129. foreach(self::$_usedBackends as $backend){
  130. if(!$backend->implementsActions(OC_GROUP_BACKEND_IN_GROUP))
  131. continue;
  132. if($backend->inGroup($uid,$gid)){
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. /**
  139. * @brief Add a user to a group
  140. * @param $uid Name of the user to add to group
  141. * @param $gid Name of the group in which add the user
  142. * @returns true/false
  143. *
  144. * Adds a user to a group.
  145. */
  146. public static function addToGroup( $uid, $gid ){
  147. // Does the group exist?
  148. if( !OC_Group::groupExists($gid)){
  149. return false;
  150. }
  151. // Go go go
  152. $run = true;
  153. OC_Hook::emit( "OC_Group", "pre_addToGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
  154. if($run){
  155. $succes=false;
  156. //add the user to the all backends that have the group
  157. foreach(self::$_usedBackends as $backend){
  158. if(!$backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP))
  159. continue;
  160. $succes|=$backend->addToGroup($uid, $gid);
  161. OC_Hook::emit( "OC_User", "post_addToGroup", array( "uid" => $uid, "gid" => $gid ));
  162. }
  163. return $succes;
  164. }else{
  165. return false;
  166. }
  167. }
  168. /**
  169. * @brief Removes a user from a group
  170. * @param $uid Name of the user to remove from group
  171. * @param $gid Name of the group from which remove the user
  172. * @returns true/false
  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 $uid Name of the user
  195. * @returns 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. if(!$backend->implementsActions(OC_GROUP_BACKEND_GET_USER_GROUPS))
  204. continue;
  205. $groups=array_merge($backend->getUserGroups($uid),$groups);
  206. }
  207. return $groups;
  208. }
  209. /**
  210. * @brief get a list of all groups
  211. * @returns array with group names
  212. *
  213. * Returns a list with all groups
  214. */
  215. public static function getGroups(){
  216. $groups=array();
  217. foreach(self::$_usedBackends as $backend){
  218. if(!$backend->implementsActions(OC_GROUP_BACKEND_GET_GROUPS))
  219. continue;
  220. $groups=array_merge($backend->getGroups(),$groups);
  221. }
  222. return $groups;
  223. }
  224. /**
  225. * check if a group exists
  226. * @param string $gid
  227. * @return bool
  228. */
  229. public static function groupExists($gid){
  230. foreach(self::$_usedBackends as $backend){
  231. if ($backend->groupExists($gid)){
  232. return true;
  233. }
  234. }
  235. return false;
  236. }
  237. /**
  238. * @brief get a list of all users in a group
  239. * @returns array with user ids
  240. */
  241. public static function usersInGroup($gid){
  242. $users=array();
  243. foreach(self::$_usedBackends as $backend){
  244. if(!$backend->implementsActions(OC_GROUP_BACKEND_GET_USERS))
  245. continue;
  246. $users=array_merge($backend->usersInGroup($gid),$users);
  247. }
  248. return $users;
  249. }
  250. }