database.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Jakob Sack <mail@jakobsack.de>
  6. * @author Joas Schilling <nickvergessen@owncloud.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  9. * @author michag86 <micha_g@arcor.de>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <icewind@owncloud.com>
  12. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @copyright Copyright (c) 2015, ownCloud, Inc.
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. /*
  32. *
  33. * The following SQL statement is just a help for developers and will not be
  34. * executed!
  35. *
  36. * CREATE TABLE `groups` (
  37. * `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  38. * PRIMARY KEY (`gid`)
  39. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  40. *
  41. * CREATE TABLE `group_user` (
  42. * `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  43. * `uid` varchar(64) COLLATE utf8_unicode_ci NOT NULL
  44. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  45. *
  46. */
  47. /**
  48. * Class for group management in a SQL Database (e.g. MySQL, SQLite)
  49. */
  50. class OC_Group_Database extends OC_Group_Backend {
  51. /**
  52. * Try to create a new group
  53. * @param string $gid The name of the group to create
  54. * @return bool
  55. *
  56. * Tries to create a new group. If the group name already exists, false will
  57. * be returned.
  58. */
  59. public function createGroup( $gid ) {
  60. // Check for existence
  61. $stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?" );
  62. $result = $stmt->execute( array( $gid ));
  63. if( $result->fetchRow() ) {
  64. // Can not add an existing group
  65. return false;
  66. }
  67. else{
  68. // Add group and exit
  69. $stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*groups` ( `gid` ) VALUES( ? )" );
  70. $result = $stmt->execute( array( $gid ));
  71. return $result ? true : false;
  72. }
  73. }
  74. /**
  75. * delete a group
  76. * @param string $gid gid of the group to delete
  77. * @return bool
  78. *
  79. * Deletes a group and removes it from the group_user-table
  80. */
  81. public function deleteGroup( $gid ) {
  82. // Delete the group
  83. $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*groups` WHERE `gid` = ?" );
  84. $stmt->execute( array( $gid ));
  85. // Delete the group-user relation
  86. $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_user` WHERE `gid` = ?" );
  87. $stmt->execute( array( $gid ));
  88. // Delete the group-groupadmin relation
  89. $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ?" );
  90. $stmt->execute( array( $gid ));
  91. return true;
  92. }
  93. /**
  94. * is user in group?
  95. * @param string $uid uid of the user
  96. * @param string $gid gid of the group
  97. * @return bool
  98. *
  99. * Checks whether the user is member of a group or not.
  100. */
  101. public function inGroup( $uid, $gid ) {
  102. // check
  103. $stmt = OC_DB::prepare( "SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` = ?" );
  104. $result = $stmt->execute( array( $gid, $uid ));
  105. return $result->fetchRow() ? true : false;
  106. }
  107. /**
  108. * Add a user to a group
  109. * @param string $uid Name of the user to add to group
  110. * @param string $gid Name of the group in which add the user
  111. * @return bool
  112. *
  113. * Adds a user to a group.
  114. */
  115. public function addToGroup( $uid, $gid ) {
  116. // No duplicate entries!
  117. if( !$this->inGroup( $uid, $gid )) {
  118. $stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*group_user` ( `uid`, `gid` ) VALUES( ?, ? )" );
  119. $stmt->execute( array( $uid, $gid ));
  120. return true;
  121. }else{
  122. return false;
  123. }
  124. }
  125. /**
  126. * Removes a user from a group
  127. * @param string $uid Name of the user to remove from group
  128. * @param string $gid Name of the group from which remove the user
  129. * @return bool
  130. *
  131. * removes the user from a group.
  132. */
  133. public function removeFromGroup( $uid, $gid ) {
  134. $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_user` WHERE `uid` = ? AND `gid` = ?" );
  135. $stmt->execute( array( $uid, $gid ));
  136. return true;
  137. }
  138. /**
  139. * Get all groups a user belongs to
  140. * @param string $uid Name of the user
  141. * @return array an array of group names
  142. *
  143. * This function fetches all groups a user belongs to. It does not check
  144. * if the user exists at all.
  145. */
  146. public function getUserGroups( $uid ) {
  147. // No magic!
  148. $stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*group_user` WHERE `uid` = ?" );
  149. $result = $stmt->execute( array( $uid ));
  150. $groups = array();
  151. while( $row = $result->fetchRow()) {
  152. $groups[] = $row["gid"];
  153. }
  154. return $groups;
  155. }
  156. /**
  157. * get a list of all groups
  158. * @param string $search
  159. * @param int $limit
  160. * @param int $offset
  161. * @return array an array of group names
  162. *
  163. * Returns a list with all groups
  164. */
  165. public function getGroups($search = '', $limit = null, $offset = null) {
  166. $stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE LOWER(`gid`) LIKE LOWER(?) ORDER BY `gid` ASC', $limit, $offset);
  167. $result = $stmt->execute(array('%' . $search . '%'));
  168. $groups = array();
  169. while ($row = $result->fetchRow()) {
  170. $groups[] = $row['gid'];
  171. }
  172. return $groups;
  173. }
  174. /**
  175. * check if a group exists
  176. * @param string $gid
  177. * @return bool
  178. */
  179. public function groupExists($gid) {
  180. $query = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?');
  181. $result = $query->execute(array($gid))->fetchOne();
  182. if ($result !== false) {
  183. return true;
  184. }
  185. return false;
  186. }
  187. /**
  188. * get a list of all users in a group
  189. * @param string $gid
  190. * @param string $search
  191. * @param int $limit
  192. * @param int $offset
  193. * @return array an array of user ids
  194. */
  195. public function usersInGroup($gid, $search = '', $limit = null, $offset = null) {
  196. $stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ? ORDER BY `uid` ASC',
  197. $limit,
  198. $offset);
  199. $result = $stmt->execute(array($gid, '%'.$search.'%'));
  200. $users = array();
  201. while ($row = $result->fetchRow()) {
  202. $users[] = $row['uid'];
  203. }
  204. return $users;
  205. }
  206. /**
  207. * get the number of all users matching the search string in a group
  208. * @param string $gid
  209. * @param string $search
  210. * @return int|false
  211. * @throws \OC\DatabaseException
  212. */
  213. public function countUsersInGroup($gid, $search = '') {
  214. $stmt = OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?');
  215. $result = $stmt->execute(array($gid, '%' . $search . '%'));
  216. $count = $result->fetchOne();
  217. if($count !== false) {
  218. $count = intval($count);
  219. }
  220. return $count;
  221. }
  222. }