database.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. *
  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. /**
  43. * @brief Try to create a new group
  44. * @param string $gid The name of the group to create
  45. * @return bool
  46. *
  47. * Tries to create a new group. If the group name already exists, false will
  48. * be returned.
  49. */
  50. public function createGroup( $gid ) {
  51. // Check for existence
  52. $stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?" );
  53. $result = $stmt->execute( array( $gid ));
  54. if( $result->fetchRow() ) {
  55. // Can not add an existing group
  56. return false;
  57. }
  58. else{
  59. // Add group and exit
  60. $stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*groups` ( `gid` ) VALUES( ? )" );
  61. $result = $stmt->execute( array( $gid ));
  62. return $result ? true : false;
  63. }
  64. }
  65. /**
  66. * @brief delete a group
  67. * @param string $gid gid of the group to delete
  68. * @return bool
  69. *
  70. * Deletes a group and removes it from the group_user-table
  71. */
  72. public function deleteGroup( $gid ) {
  73. // Delete the group
  74. $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*groups` WHERE `gid` = ?" );
  75. $stmt->execute( array( $gid ));
  76. // Delete the group-user relation
  77. $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_user` WHERE `gid` = ?" );
  78. $stmt->execute( array( $gid ));
  79. return true;
  80. }
  81. /**
  82. * @brief is user in group?
  83. * @param string $uid uid of the user
  84. * @param string $gid gid of the group
  85. * @return bool
  86. *
  87. * Checks whether the user is member of a group or not.
  88. */
  89. public function inGroup( $uid, $gid ) {
  90. // check
  91. $stmt = OC_DB::prepare( "SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` = ?" );
  92. $result = $stmt->execute( array( $gid, $uid ));
  93. return $result->fetchRow() ? true : false;
  94. }
  95. /**
  96. * @brief Add a user to a group
  97. * @param string $uid Name of the user to add to group
  98. * @param string $gid Name of the group in which add the user
  99. * @return bool
  100. *
  101. * Adds a user to a group.
  102. */
  103. public function addToGroup( $uid, $gid ) {
  104. // No duplicate entries!
  105. if( !$this->inGroup( $uid, $gid )) {
  106. $stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*group_user` ( `uid`, `gid` ) VALUES( ?, ? )" );
  107. $stmt->execute( array( $uid, $gid ));
  108. return true;
  109. }else{
  110. return false;
  111. }
  112. }
  113. /**
  114. * @brief Removes a user from a group
  115. * @param string $uid Name of the user to remove from group
  116. * @param string $gid Name of the group from which remove the user
  117. * @return bool
  118. *
  119. * removes the user from a group.
  120. */
  121. public function removeFromGroup( $uid, $gid ) {
  122. $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_user` WHERE `uid` = ? AND `gid` = ?" );
  123. $stmt->execute( array( $uid, $gid ));
  124. return true;
  125. }
  126. /**
  127. * @brief Get all groups a user belongs to
  128. * @param string $uid Name of the user
  129. * @return array with group names
  130. *
  131. * This function fetches all groups a user belongs to. It does not check
  132. * if the user exists at all.
  133. */
  134. public function getUserGroups( $uid ) {
  135. // No magic!
  136. $stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*group_user` WHERE `uid` = ?" );
  137. $result = $stmt->execute( array( $uid ));
  138. $groups = array();
  139. while( $row = $result->fetchRow()) {
  140. $groups[] = $row["gid"];
  141. }
  142. return $groups;
  143. }
  144. /**
  145. * @brief get a list of all groups
  146. * @param string $search
  147. * @param int $limit
  148. * @param int $offset
  149. * @return array with group names
  150. *
  151. * Returns a list with all groups
  152. */
  153. public function getGroups($search = '', $limit = null, $offset = null) {
  154. $stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` LIKE ?', $limit, $offset);
  155. $result = $stmt->execute(array($search.'%'));
  156. $groups = array();
  157. while ($row = $result->fetchRow()) {
  158. $groups[] = $row['gid'];
  159. }
  160. return $groups;
  161. }
  162. /**
  163. * check if a group exists
  164. * @param string $gid
  165. * @return bool
  166. */
  167. public function groupExists($gid) {
  168. $query = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?');
  169. $result = $query->execute(array($gid))->fetchOne();
  170. if ($result) {
  171. return true;
  172. }
  173. return false;
  174. }
  175. /**
  176. * @brief get a list of all users in a group
  177. * @param string $gid
  178. * @param string $search
  179. * @param int $limit
  180. * @param int $offset
  181. * @return array with user ids
  182. */
  183. public function usersInGroup($gid, $search = '', $limit = null, $offset = null) {
  184. $stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?', $limit, $offset);
  185. $result = $stmt->execute(array($gid, $search.'%'));
  186. $users = array();
  187. while ($row = $result->fetchRow()) {
  188. $users[] = $row['uid'];
  189. }
  190. return $users;
  191. }
  192. /**
  193. * @brief get a list of all display names in a group
  194. * @param string $gid
  195. * @param string $search
  196. * @param int $limit
  197. * @param int $offset
  198. * @return array with display names (value) and user ids (key)
  199. */
  200. public function DisplayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  201. $displayNames = '';
  202. $stmt = OC_DB::prepare('SELECT `*PREFIX*users`.`uid`, `*PREFIX*users`.`displayname` FROM `*PREFIX*users` INNER JOIN `*PREFIX*group_user` ON `*PREFIX*group_user`.`uid` = `*PREFIX*users`.`uid` WHERE `gid` = ? AND `*PREFIX*group_user.uid` LIKE ?', $limit, $offset);
  203. $result = $stmt->execute(array($gid, $search.'%'));
  204. $users = array();
  205. while ($row = $result->fetchRow()) {
  206. $displayName = trim($row['displayname'], ' ');
  207. $displayNames[$row['uid']] = empty($displayName) ? $row['uid'] : $displayName;
  208. }
  209. return $displayNames;
  210. }
  211. }