Database.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. $this->fixDI();
  180. // No magic!
  181. $qb = $this->dbConn->getQueryBuilder();
  182. $cursor = $qb->select('gid')
  183. ->from('group_user')
  184. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
  185. ->execute();
  186. $groups = [];
  187. while( $row = $cursor->fetch()) {
  188. $groups[] = $row["gid"];
  189. $this->groupCache[$row['gid']] = $row['gid'];
  190. }
  191. $cursor->closeCursor();
  192. return $groups;
  193. }
  194. /**
  195. * get a list of all groups
  196. * @param string $search
  197. * @param int $limit
  198. * @param int $offset
  199. * @return array an array of group names
  200. *
  201. * Returns a list with all groups
  202. */
  203. public function getGroups($search = '', $limit = null, $offset = null) {
  204. $parameters = [];
  205. $searchLike = '';
  206. if ($search !== '') {
  207. $parameters[] = '%' . $search . '%';
  208. $searchLike = ' WHERE LOWER(`gid`) LIKE LOWER(?)';
  209. }
  210. $stmt = \OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups`' . $searchLike . ' ORDER BY `gid` ASC', $limit, $offset);
  211. $result = $stmt->execute($parameters);
  212. $groups = array();
  213. while ($row = $result->fetchRow()) {
  214. $groups[] = $row['gid'];
  215. }
  216. return $groups;
  217. }
  218. /**
  219. * check if a group exists
  220. * @param string $gid
  221. * @return bool
  222. */
  223. public function groupExists($gid) {
  224. $this->fixDI();
  225. // Check cache first
  226. if (isset($this->groupCache[$gid])) {
  227. return true;
  228. }
  229. $qb = $this->dbConn->getQueryBuilder();
  230. $cursor = $qb->select('gid')
  231. ->from('groups')
  232. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  233. ->execute();
  234. $result = $cursor->fetch();
  235. $cursor->closeCursor();
  236. if ($result !== false) {
  237. $this->groupCache[$gid] = $gid;
  238. return true;
  239. }
  240. return false;
  241. }
  242. /**
  243. * get a list of all users in a group
  244. * @param string $gid
  245. * @param string $search
  246. * @param int $limit
  247. * @param int $offset
  248. * @return array an array of user ids
  249. */
  250. public function usersInGroup($gid, $search = '', $limit = null, $offset = null) {
  251. $parameters = [$gid];
  252. $searchLike = '';
  253. if ($search !== '') {
  254. $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%';
  255. $searchLike = ' AND `uid` LIKE ?';
  256. }
  257. $stmt = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike . ' ORDER BY `uid` ASC',
  258. $limit,
  259. $offset);
  260. $result = $stmt->execute($parameters);
  261. $users = array();
  262. while ($row = $result->fetchRow()) {
  263. $users[] = $row['uid'];
  264. }
  265. return $users;
  266. }
  267. /**
  268. * get the number of all users matching the search string in a group
  269. * @param string $gid
  270. * @param string $search
  271. * @return int|false
  272. * @throws \OC\DatabaseException
  273. */
  274. public function countUsersInGroup($gid, $search = '') {
  275. $parameters = [$gid];
  276. $searchLike = '';
  277. if ($search !== '') {
  278. $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%';
  279. $searchLike = ' AND `uid` LIKE ?';
  280. }
  281. $stmt = \OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike);
  282. $result = $stmt->execute($parameters);
  283. $count = $result->fetchOne();
  284. if($count !== false) {
  285. $count = intval($count);
  286. }
  287. return $count;
  288. }
  289. }