Database.php 10 KB

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