database.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. *
  24. * The following SQL statement is just a help for developers and will not be
  25. * executed!
  26. *
  27. * CREATE TABLE `groups` (
  28. * `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  29. * PRIMARY KEY (`gid`)
  30. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  31. *
  32. * CREATE TABLE `group_user` (
  33. * `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  34. * `uid` varchar(64) COLLATE utf8_unicode_ci NOT NULL
  35. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  36. *
  37. */
  38. /**
  39. * Class for group management in a SQL Database (e.g. MySQL, SQLite)
  40. */
  41. class OC_Group_Database extends OC_Group_Backend {
  42. static private $userGroupCache=array();
  43. /**
  44. * @brief Try to create a new group
  45. * @param $gid The name of the group to create
  46. * @returns true/false
  47. *
  48. * Trys to create a new group. If the group name already exists, false will
  49. * be returned.
  50. */
  51. public static function createGroup( $gid ){
  52. // Check for existence
  53. $query = OC_DB::prepare( "SELECT gid FROM `*PREFIX*groups` WHERE gid = ?" );
  54. $result = $query->execute( array( $gid ));
  55. if( $result->fetchRow() ){
  56. // Can not add an existing group
  57. return false;
  58. }
  59. else{
  60. // Add group and exit
  61. $query = OC_DB::prepare( "INSERT INTO `*PREFIX*groups` ( `gid` ) VALUES( ? )" );
  62. $result = $query->execute( array( $gid ));
  63. return $result ? true : false;
  64. }
  65. }
  66. /**
  67. * @brief delete a group
  68. * @param $gid gid of the group to delete
  69. * @returns true/false
  70. *
  71. * Deletes a group and removes it from the group_user-table
  72. */
  73. public static function deleteGroup( $gid ){
  74. // Delete the group
  75. $query = OC_DB::prepare( "DELETE FROM `*PREFIX*groups` WHERE gid = ?" );
  76. $result = $query->execute( array( $gid ));
  77. // Delete the group-user relation
  78. $query = OC_DB::prepare( "DELETE FROM `*PREFIX*group_user` WHERE gid = ?" );
  79. $result = $query->execute( array( $gid ));
  80. return true;
  81. }
  82. /**
  83. * @brief is user in group?
  84. * @param $uid uid of the user
  85. * @param $gid gid of the group
  86. * @returns true/false
  87. *
  88. * Checks whether the user is member of a group or not.
  89. */
  90. public static function inGroup( $uid, $gid ){
  91. // check
  92. $query = OC_DB::prepare( "SELECT uid FROM `*PREFIX*group_user` WHERE gid = ? AND uid = ?" );
  93. $result = $query->execute( array( $gid, $uid ));
  94. return $result->fetchRow() ? true : false;
  95. }
  96. /**
  97. * @brief Add a user to a group
  98. * @param $uid Name of the user to add to group
  99. * @param $gid Name of the group in which add the user
  100. * @returns true/false
  101. *
  102. * Adds a user to a group.
  103. */
  104. public static function addToGroup( $uid, $gid ){
  105. // No duplicate entries!
  106. if( !self::inGroup( $uid, $gid )){
  107. $query = OC_DB::prepare( "INSERT INTO `*PREFIX*group_user` ( `uid`, `gid` ) VALUES( ?, ? )" );
  108. $result = $query->execute( array( $uid, $gid ));
  109. return true;
  110. }else{
  111. return false;
  112. }
  113. }
  114. /**
  115. * @brief Removes a user from a group
  116. * @param $uid Name of the user to remove from group
  117. * @param $gid Name of the group from which remove the user
  118. * @returns true/false
  119. *
  120. * removes the user from a group.
  121. */
  122. public static function removeFromGroup( $uid, $gid ){
  123. $query = OC_DB::prepare( "DELETE FROM *PREFIX*group_user WHERE uid = ? AND gid = ?" );
  124. $result = $query->execute( array( $uid, $gid ));
  125. return true;
  126. }
  127. /**
  128. * @brief Get all groups a user belongs to
  129. * @param $uid Name of the user
  130. * @returns array with group names
  131. *
  132. * This function fetches all groups a user belongs to. It does not check
  133. * if the user exists at all.
  134. */
  135. public static function getUserGroups( $uid ){
  136. // No magic!
  137. $query = OC_DB::prepare( "SELECT gid FROM `*PREFIX*group_user` WHERE uid = ?" );
  138. $result = $query->execute( array( $uid ));
  139. $groups = array();
  140. while( $row = $result->fetchRow()){
  141. $groups[] = $row["gid"];
  142. }
  143. return $groups;
  144. }
  145. /**
  146. * @brief get a list of all groups
  147. * @returns array with group names
  148. *
  149. * Returns a list with all groups
  150. */
  151. public static function getGroups(){
  152. $query = OC_DB::prepare( "SELECT gid FROM `*PREFIX*groups`" );
  153. $result = $query->execute();
  154. $groups = array();
  155. while( $row = $result->fetchRow()){
  156. $groups[] = $row["gid"];
  157. }
  158. return $groups;
  159. }
  160. /**
  161. * @brief get a list of all users in a group
  162. * @returns array with user ids
  163. */
  164. public static function usersInGroup($gid){
  165. $query=OC_DB::prepare('SELECT uid FROM *PREFIX*group_user WHERE gid=?');
  166. $users=array();
  167. $result=$query->execute(array($gid));
  168. while($row=$result->fetchRow()){
  169. $users[]=$row['uid'];
  170. }
  171. return $users;
  172. }
  173. }