Database.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 OCP\DB\QueryBuilder\IQueryBuilder;
  43. use OCP\Group\Backend\ABackend;
  44. use OCP\Group\Backend\IAddToGroupBackend;
  45. use OCP\Group\Backend\ICountDisabledInGroup;
  46. use OCP\Group\Backend\ICountUsersBackend;
  47. use OCP\Group\Backend\ICreateGroupBackend;
  48. use OCP\Group\Backend\IDeleteGroupBackend;
  49. use OCP\Group\Backend\IRemoveFromGroupBackend;
  50. use OCP\IDBConnection;
  51. /**
  52. * Class for group management in a SQL Database (e.g. MySQL, SQLite)
  53. */
  54. class Database extends ABackend
  55. implements IAddToGroupBackend,
  56. ICountDisabledInGroup,
  57. ICountUsersBackend,
  58. ICreateGroupBackend,
  59. IDeleteGroupBackend,
  60. IRemoveFromGroupBackend {
  61. /** @var string[] */
  62. private $groupCache = [];
  63. /** @var IDBConnection */
  64. private $dbConn;
  65. /**
  66. * \OC\Group\Database constructor.
  67. *
  68. * @param IDBConnection|null $dbConn
  69. */
  70. public function __construct(IDBConnection $dbConn = null) {
  71. $this->dbConn = $dbConn;
  72. }
  73. /**
  74. * FIXME: This function should not be required!
  75. */
  76. private function fixDI() {
  77. if ($this->dbConn === null) {
  78. $this->dbConn = \OC::$server->getDatabaseConnection();
  79. }
  80. }
  81. /**
  82. * Try to create a new group
  83. * @param string $gid The name of the group to create
  84. * @return bool
  85. *
  86. * Tries to create a new group. If the group name already exists, false will
  87. * be returned.
  88. */
  89. public function createGroup(string $gid): bool {
  90. $this->fixDI();
  91. // Add group
  92. $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [
  93. 'gid' => $gid,
  94. ]);
  95. // Add to cache
  96. $this->groupCache[$gid] = $gid;
  97. return $result === 1;
  98. }
  99. /**
  100. * delete a group
  101. * @param string $gid gid of the group to delete
  102. * @return bool
  103. *
  104. * Deletes a group and removes it from the group_user-table
  105. */
  106. public function deleteGroup(string $gid): bool {
  107. $this->fixDI();
  108. // Delete the group
  109. $qb = $this->dbConn->getQueryBuilder();
  110. $qb->delete('groups')
  111. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  112. ->execute();
  113. // Delete the group-user relation
  114. $qb = $this->dbConn->getQueryBuilder();
  115. $qb->delete('group_user')
  116. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  117. ->execute();
  118. // Delete the group-groupadmin relation
  119. $qb = $this->dbConn->getQueryBuilder();
  120. $qb->delete('group_admin')
  121. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  122. ->execute();
  123. // Delete from cache
  124. unset($this->groupCache[$gid]);
  125. return true;
  126. }
  127. /**
  128. * is user in group?
  129. * @param string $uid uid of the user
  130. * @param string $gid gid of the group
  131. * @return bool
  132. *
  133. * Checks whether the user is member of a group or not.
  134. */
  135. public function inGroup( $uid, $gid ) {
  136. $this->fixDI();
  137. // check
  138. $qb = $this->dbConn->getQueryBuilder();
  139. $cursor = $qb->select('uid')
  140. ->from('group_user')
  141. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  142. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
  143. ->execute();
  144. $result = $cursor->fetch();
  145. $cursor->closeCursor();
  146. return $result ? true : false;
  147. }
  148. /**
  149. * Add a user to a group
  150. * @param string $uid Name of the user to add to group
  151. * @param string $gid Name of the group in which add the user
  152. * @return bool
  153. *
  154. * Adds a user to a group.
  155. */
  156. public function addToGroup(string $uid, string $gid): bool {
  157. $this->fixDI();
  158. // No duplicate entries!
  159. if( !$this->inGroup( $uid, $gid )) {
  160. $qb = $this->dbConn->getQueryBuilder();
  161. $qb->insert('group_user')
  162. ->setValue('uid', $qb->createNamedParameter($uid))
  163. ->setValue('gid', $qb->createNamedParameter($gid))
  164. ->execute();
  165. return true;
  166. }else{
  167. return false;
  168. }
  169. }
  170. /**
  171. * Removes a user from a group
  172. * @param string $uid Name of the user to remove from group
  173. * @param string $gid Name of the group from which remove the user
  174. * @return bool
  175. *
  176. * removes the user from a group.
  177. */
  178. public function removeFromGroup(string $uid, string $gid): bool {
  179. $this->fixDI();
  180. $qb = $this->dbConn->getQueryBuilder();
  181. $qb->delete('group_user')
  182. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
  183. ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  184. ->execute();
  185. return true;
  186. }
  187. /**
  188. * Get all groups a user belongs to
  189. * @param string $uid Name of the user
  190. * @return array an array of group names
  191. *
  192. * This function fetches all groups a user belongs to. It does not check
  193. * if the user exists at all.
  194. */
  195. public function getUserGroups( $uid ) {
  196. //guests has empty or null $uid
  197. if ($uid === null || $uid === '') {
  198. return [];
  199. }
  200. $this->fixDI();
  201. // No magic!
  202. $qb = $this->dbConn->getQueryBuilder();
  203. $cursor = $qb->select('gid')
  204. ->from('group_user')
  205. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
  206. ->execute();
  207. $groups = [];
  208. while( $row = $cursor->fetch()) {
  209. $groups[] = $row['gid'];
  210. $this->groupCache[$row['gid']] = $row['gid'];
  211. }
  212. $cursor->closeCursor();
  213. return $groups;
  214. }
  215. /**
  216. * get a list of all groups
  217. * @param string $search
  218. * @param int $limit
  219. * @param int $offset
  220. * @return array an array of group names
  221. *
  222. * Returns a list with all groups
  223. */
  224. public function getGroups($search = '', $limit = null, $offset = null) {
  225. $this->fixDI();
  226. $query = $this->dbConn->getQueryBuilder();
  227. $query->select('gid')
  228. ->from('groups')
  229. ->orderBy('gid', 'ASC');
  230. if ($search !== '') {
  231. $query->where($query->expr()->iLike('gid', $query->createNamedParameter(
  232. '%' . $this->dbConn->escapeLikeParameter($search) . '%'
  233. )));
  234. }
  235. $query->setMaxResults($limit)
  236. ->setFirstResult($offset);
  237. $result = $query->execute();
  238. $groups = [];
  239. while ($row = $result->fetch()) {
  240. $groups[] = $row['gid'];
  241. }
  242. $result->closeCursor();
  243. return $groups;
  244. }
  245. /**
  246. * check if a group exists
  247. * @param string $gid
  248. * @return bool
  249. */
  250. public function groupExists($gid) {
  251. $this->fixDI();
  252. // Check cache first
  253. if (isset($this->groupCache[$gid])) {
  254. return true;
  255. }
  256. $qb = $this->dbConn->getQueryBuilder();
  257. $cursor = $qb->select('gid')
  258. ->from('groups')
  259. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
  260. ->execute();
  261. $result = $cursor->fetch();
  262. $cursor->closeCursor();
  263. if ($result !== false) {
  264. $this->groupCache[$gid] = $gid;
  265. return true;
  266. }
  267. return false;
  268. }
  269. /**
  270. * get a list of all users in a group
  271. * @param string $gid
  272. * @param string $search
  273. * @param int $limit
  274. * @param int $offset
  275. * @return array an array of user ids
  276. */
  277. public function usersInGroup($gid, $search = '', $limit = null, $offset = null) {
  278. $this->fixDI();
  279. $query = $this->dbConn->getQueryBuilder();
  280. $query->select('uid')
  281. ->from('group_user')
  282. ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
  283. ->orderBy('uid', 'ASC');
  284. if ($search !== '') {
  285. $query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
  286. '%' . $this->dbConn->escapeLikeParameter($search) . '%'
  287. )));
  288. }
  289. $query->setMaxResults($limit)
  290. ->setFirstResult($offset);
  291. $result = $query->execute();
  292. $users = [];
  293. while ($row = $result->fetch()) {
  294. $users[] = $row['uid'];
  295. }
  296. $result->closeCursor();
  297. return $users;
  298. }
  299. /**
  300. * get the number of all users matching the search string in a group
  301. * @param string $gid
  302. * @param string $search
  303. * @return int
  304. */
  305. public function countUsersInGroup(string $gid, string $search = ''): int {
  306. $this->fixDI();
  307. $query = $this->dbConn->getQueryBuilder();
  308. $query->selectAlias($query->createFunction('COUNT(*)'), 'num_users')
  309. ->from('group_user')
  310. ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
  311. if ($search !== '') {
  312. $query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
  313. '%' . $this->dbConn->escapeLikeParameter($search) . '%'
  314. )));
  315. }
  316. $result = $query->execute();
  317. $count = $result->fetchColumn();
  318. $result->closeCursor();
  319. if ($count !== false) {
  320. $count = (int)$count;
  321. } else {
  322. $count = 0;
  323. }
  324. return $count;
  325. }
  326. /**
  327. * get the number of disabled users in a group
  328. *
  329. * @param string $search
  330. * @return int|bool
  331. */
  332. public function countDisabledInGroup(string $gid): int {
  333. $this->fixDI();
  334. $query = $this->dbConn->getQueryBuilder();
  335. $query->select($query->createFunction('COUNT(Distinct uid)'))
  336. ->from('preferences', 'p')
  337. ->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid')
  338. ->where($query->expr()->eq('appid', $query->createNamedParameter('core')))
  339. ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled')))
  340. ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR))
  341. ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR));
  342. $result = $query->execute();
  343. $count = $result->fetchColumn();
  344. $result->closeCursor();
  345. if ($count !== false) {
  346. $count = (int)$count;
  347. } else {
  348. $count = 0;
  349. }
  350. return $count;
  351. }
  352. }