Database.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Aaron Wood <aaronjwood@gmail.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. /*
  24. *
  25. * The following SQL statement is just a help for developers and will not be
  26. * executed!
  27. *
  28. * CREATE TABLE `groups` (
  29. * `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  30. * PRIMARY KEY (`gid`)
  31. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  32. *
  33. * CREATE TABLE `group_user` (
  34. * `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  35. * `uid` varchar(64) COLLATE utf8_unicode_ci NOT NULL
  36. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  37. *
  38. */
  39. namespace OC\Group;
  40. /**
  41. * Class for group management in a SQL Database (e.g. MySQL, SQLite)
  42. */
  43. class Database extends \OC\Group\Backend {
  44. /** @var string[] */
  45. private $groupCache = [];
  46. /** @var \OCP\IDBConnection */
  47. private $dbConn;
  48. /**
  49. * \OC\Group\Database constructor.
  50. *
  51. * @param \OCP\IDBConnection|null $dbConn
  52. */
  53. public function __construct(\OCP\IDBConnection $dbConn = null) {
  54. $this->dbConn = $dbConn;
  55. }
  56. /**
  57. * FIXME: This function should not be required!
  58. */
  59. private function fixDI() {
  60. if ($this->dbConn === null) {
  61. $this->dbConn = \OC::$server->getDatabaseConnection();
  62. }
  63. }
  64. /**
  65. * Try to create a new group
  66. * @param string $gid The name of the group to create
  67. * @return bool
  68. *
  69. * Tries to create a new group. If the group name already exists, false will
  70. * be returned.
  71. */
  72. public function createGroup( $gid ) {
  73. $this->fixDI();
  74. // Add group
  75. $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [
  76. 'gid' => $gid,
  77. ]);
  78. // Add to cache
  79. $this->groupCache[$gid] = $gid;
  80. return $result === 1;
  81. }
  82. /**
  83. * delete a group
  84. * @param string $gid gid of the group to delete
  85. * @return bool
  86. *
  87. * Deletes a group and removes it from the group_user-table
  88. */
  89. public function deleteGroup( $gid ) {
  90. $this->fixDI();
  91. // Delete the group
  92. $qb = $this->dbConn->getQueryBuilder();
  93. $qb->delete('groups')
  94. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  95. ->execute();
  96. // Delete the group-user relation
  97. $qb = $this->dbConn->getQueryBuilder();
  98. $qb->delete('group_user')
  99. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  100. ->execute();
  101. // Delete the group-groupadmin relation
  102. $qb = $this->dbConn->getQueryBuilder();
  103. $qb->delete('group_admin')
  104. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  105. ->execute();
  106. // Delete from cache
  107. unset($this->groupCache[$gid]);
  108. return true;
  109. }
  110. /**
  111. * is user in group?
  112. * @param string $uid uid of the user
  113. * @param string $gid gid of the group
  114. * @return bool
  115. *
  116. * Checks whether the user is member of a group or not.
  117. */
  118. public function inGroup( $uid, $gid ) {
  119. $this->fixDI();
  120. // check
  121. $qb = $this->dbConn->getQueryBuilder();
  122. $cursor = $qb->select('uid')
  123. ->from('group_user')
  124. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  125. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
  126. ->execute();
  127. $result = $cursor->fetch();
  128. $cursor->closeCursor();
  129. return $result ? true : false;
  130. }
  131. /**
  132. * Add a user to a group
  133. * @param string $uid Name of the user to add to group
  134. * @param string $gid Name of the group in which add the user
  135. * @return bool
  136. *
  137. * Adds a user to a group.
  138. */
  139. public function addToGroup( $uid, $gid ) {
  140. $this->fixDI();
  141. // No duplicate entries!
  142. if( !$this->inGroup( $uid, $gid )) {
  143. $qb = $this->dbConn->getQueryBuilder();
  144. $qb->insert('group_user')
  145. ->setValue('uid', $qb->createNamedParameter($uid))
  146. ->setValue('gid', $qb->createNamedParameter($gid))
  147. ->execute();
  148. return true;
  149. }else{
  150. return false;
  151. }
  152. }
  153. /**
  154. * Removes a user from a group
  155. * @param string $uid Name of the user to remove from group
  156. * @param string $gid Name of the group from which remove the user
  157. * @return bool
  158. *
  159. * removes the user from a group.
  160. */
  161. public function removeFromGroup( $uid, $gid ) {
  162. $this->fixDI();
  163. $qb = $this->dbConn->getQueryBuilder();
  164. $qb->delete('group_user')
  165. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
  166. ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  167. ->execute();
  168. return true;
  169. }
  170. /**
  171. * Get all groups a user belongs to
  172. * @param string $uid Name of the user
  173. * @return array an array of group names
  174. *
  175. * This function fetches all groups a user belongs to. It does not check
  176. * if the user exists at all.
  177. */
  178. public function getUserGroups( $uid ) {
  179. //guests has empty or null $uid
  180. if ($uid === null || $uid === '') {
  181. return [];
  182. }
  183. $this->fixDI();
  184. // No magic!
  185. $qb = $this->dbConn->getQueryBuilder();
  186. $cursor = $qb->select('gid')
  187. ->from('group_user')
  188. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
  189. ->execute();
  190. $groups = [];
  191. while( $row = $cursor->fetch()) {
  192. $groups[] = $row["gid"];
  193. $this->groupCache[$row['gid']] = $row['gid'];
  194. }
  195. $cursor->closeCursor();
  196. return $groups;
  197. }
  198. /**
  199. * get a list of all groups
  200. * @param string $search
  201. * @param int $limit
  202. * @param int $offset
  203. * @return array an array of group names
  204. *
  205. * Returns a list with all groups
  206. */
  207. public function getGroups($search = '', $limit = null, $offset = null) {
  208. $parameters = [];
  209. $searchLike = '';
  210. if ($search !== '') {
  211. $parameters[] = '%' . $search . '%';
  212. $searchLike = ' WHERE LOWER(`gid`) LIKE LOWER(?)';
  213. }
  214. $stmt = \OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups`' . $searchLike . ' ORDER BY `gid` ASC', $limit, $offset);
  215. $result = $stmt->execute($parameters);
  216. $groups = array();
  217. while ($row = $result->fetchRow()) {
  218. $groups[] = $row['gid'];
  219. }
  220. return $groups;
  221. }
  222. /**
  223. * check if a group exists
  224. * @param string $gid
  225. * @return bool
  226. */
  227. public function groupExists($gid) {
  228. $this->fixDI();
  229. // Check cache first
  230. if (isset($this->groupCache[$gid])) {
  231. return true;
  232. }
  233. $qb = $this->dbConn->getQueryBuilder();
  234. $cursor = $qb->select('gid')
  235. ->from('groups')
  236. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  237. ->execute();
  238. $result = $cursor->fetch();
  239. $cursor->closeCursor();
  240. if ($result !== false) {
  241. $this->groupCache[$gid] = $gid;
  242. return true;
  243. }
  244. return false;
  245. }
  246. /**
  247. * get a list of all users in a group
  248. * @param string $gid
  249. * @param string $search
  250. * @param int $limit
  251. * @param int $offset
  252. * @return array an array of user ids
  253. */
  254. public function usersInGroup($gid, $search = '', $limit = null, $offset = null) {
  255. $parameters = [$gid];
  256. $searchLike = '';
  257. if ($search !== '') {
  258. $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%';
  259. $searchLike = ' AND `uid` LIKE ?';
  260. }
  261. $stmt = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike . ' ORDER BY `uid` ASC',
  262. $limit,
  263. $offset);
  264. $result = $stmt->execute($parameters);
  265. $users = array();
  266. while ($row = $result->fetchRow()) {
  267. $users[] = $row['uid'];
  268. }
  269. return $users;
  270. }
  271. /**
  272. * get the number of all users matching the search string in a group
  273. * @param string $gid
  274. * @param string $search
  275. * @return int|false
  276. * @throws \OC\DatabaseException
  277. */
  278. public function countUsersInGroup($gid, $search = '') {
  279. $parameters = [$gid];
  280. $searchLike = '';
  281. if ($search !== '') {
  282. $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%';
  283. $searchLike = ' AND `uid` LIKE ?';
  284. }
  285. $stmt = \OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike);
  286. $result = $stmt->execute($parameters);
  287. $count = $result->fetchOne();
  288. if($count !== false) {
  289. $count = intval($count);
  290. }
  291. return $count;
  292. }
  293. }