group.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. 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 group in the first backend that supports creating groups
  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->inGroup($uid,$gid)){
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. /**
  137. * @brief Add a user to a group
  138. * @param $uid Name of the user to add to group
  139. * @param $gid Name of the group in which add the user
  140. * @returns true/false
  141. *
  142. * Adds a user to a group.
  143. */
  144. public static function addToGroup( $uid, $gid ){
  145. // Does the group exist?
  146. if( !OC_Group::groupExists($gid)){
  147. return false;
  148. }
  149. // Go go go
  150. $run = true;
  151. OC_Hook::emit( "OC_Group", "pre_addToGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
  152. if($run){
  153. $succes=false;
  154. //add the user to the all backends that have the group
  155. foreach(self::$_usedBackends as $backend){
  156. if(!$backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP))
  157. continue;
  158. if($backend->groupExists($gid)){
  159. $succes|=$backend->addToGroup($uid, $gid);
  160. }
  161. }
  162. if($succes){
  163. OC_Hook::emit( "OC_User", "post_addToGroup", array( "uid" => $uid, "gid" => $gid ));
  164. }
  165. return $succes;
  166. }else{
  167. return false;
  168. }
  169. }
  170. /**
  171. * @brief Removes a user from a group
  172. * @param $uid Name of the user to remove from group
  173. * @param $gid Name of the group from which remove the user
  174. * @returns true/false
  175. *
  176. * removes the user from a group.
  177. */
  178. public static function removeFromGroup( $uid, $gid ){
  179. $run = true;
  180. OC_Hook::emit( "OC_Group", "pre_removeFromGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
  181. if($run){
  182. //remove the user from the all backends that have the group
  183. foreach(self::$_usedBackends as $backend){
  184. if(!$backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP))
  185. continue;
  186. $backend->removeFromGroup($uid, $gid);
  187. OC_Hook::emit( "OC_User", "post_removeFromGroup", array( "uid" => $uid, "gid" => $gid ));
  188. }
  189. return true;
  190. }else{
  191. return false;
  192. }
  193. }
  194. /**
  195. * @brief Get all groups a user belongs to
  196. * @param $uid Name of the user
  197. * @returns array with group names
  198. *
  199. * This function fetches all groups a user belongs to. It does not check
  200. * if the user exists at all.
  201. */
  202. public static function getUserGroups( $uid ){
  203. $groups=array();
  204. foreach(self::$_usedBackends as $backend){
  205. $groups=array_merge($backend->getUserGroups($uid),$groups);
  206. }
  207. asort($groups);
  208. return $groups;
  209. }
  210. /**
  211. * @brief get a list of all groups
  212. * @returns array with group names
  213. *
  214. * Returns a list with all groups
  215. */
  216. public static function getGroups(){
  217. $groups=array();
  218. foreach(self::$_usedBackends as $backend){
  219. $groups=array_merge($backend->getGroups(),$groups);
  220. }
  221. asort($groups);
  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. $users=array_merge($backend->usersInGroup($gid),$users);
  245. }
  246. return $users;
  247. }
  248. }