123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- <?php
- namespace OC\Group;
- use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
- use OCP\DB\QueryBuilder\IQueryBuilder;
- use OCP\Group\Backend\ABackend;
- use OCP\Group\Backend\IAddToGroupBackend;
- use OCP\Group\Backend\ICountDisabledInGroup;
- use OCP\Group\Backend\ICountUsersBackend;
- use OCP\Group\Backend\ICreateGroupBackend;
- use OCP\Group\Backend\IDeleteGroupBackend;
- use OCP\Group\Backend\IGetDisplayNameBackend;
- use OCP\Group\Backend\IGroupDetailsBackend;
- use OCP\Group\Backend\IRemoveFromGroupBackend;
- use OCP\Group\Backend\ISearchableGroupBackend;
- use OCP\Group\Backend\ISetDisplayNameBackend;
- use OCP\Group\Backend\INamedBackend;
- use OCP\IDBConnection;
- use OCP\IUserManager;
- use OC\User\LazyUser;
- class Database extends ABackend implements
- IAddToGroupBackend,
- ICountDisabledInGroup,
- ICountUsersBackend,
- ICreateGroupBackend,
- IDeleteGroupBackend,
- IGetDisplayNameBackend,
- IGroupDetailsBackend,
- IRemoveFromGroupBackend,
- ISetDisplayNameBackend,
- ISearchableGroupBackend,
- INamedBackend {
-
- private $groupCache = [];
-
- private $dbConn;
-
- public function __construct(IDBConnection $dbConn = null) {
- $this->dbConn = $dbConn;
- }
-
- private function fixDI() {
- if ($this->dbConn === null) {
- $this->dbConn = \OC::$server->getDatabaseConnection();
- }
- }
-
- public function createGroup(string $gid): bool {
- $this->fixDI();
- try {
-
- $builder = $this->dbConn->getQueryBuilder();
- $result = $builder->insert('groups')
- ->setValue('gid', $builder->createNamedParameter($gid))
- ->setValue('displayname', $builder->createNamedParameter($gid))
- ->execute();
- } catch (UniqueConstraintViolationException $e) {
- $result = 0;
- }
-
- $this->groupCache[$gid] = [
- 'gid' => $gid,
- 'displayname' => $gid
- ];
- return $result === 1;
- }
-
- public function deleteGroup(string $gid): bool {
- $this->fixDI();
-
- $qb = $this->dbConn->getQueryBuilder();
- $qb->delete('groups')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->execute();
-
- $qb = $this->dbConn->getQueryBuilder();
- $qb->delete('group_user')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->execute();
-
- $qb = $this->dbConn->getQueryBuilder();
- $qb->delete('group_admin')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->execute();
-
- unset($this->groupCache[$gid]);
- return true;
- }
-
- public function inGroup($uid, $gid) {
- $this->fixDI();
-
- $qb = $this->dbConn->getQueryBuilder();
- $cursor = $qb->select('uid')
- ->from('group_user')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
- ->execute();
- $result = $cursor->fetch();
- $cursor->closeCursor();
- return $result ? true : false;
- }
-
- public function addToGroup(string $uid, string $gid): bool {
- $this->fixDI();
-
- if (!$this->inGroup($uid, $gid)) {
- $qb = $this->dbConn->getQueryBuilder();
- $qb->insert('group_user')
- ->setValue('uid', $qb->createNamedParameter($uid))
- ->setValue('gid', $qb->createNamedParameter($gid))
- ->execute();
- return true;
- } else {
- return false;
- }
- }
-
- public function removeFromGroup(string $uid, string $gid): bool {
- $this->fixDI();
- $qb = $this->dbConn->getQueryBuilder();
- $qb->delete('group_user')
- ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
- ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->execute();
- return true;
- }
-
- public function getUserGroups($uid) {
-
- if ($uid === null || $uid === '') {
- return [];
- }
- $this->fixDI();
-
- $qb = $this->dbConn->getQueryBuilder();
- $cursor = $qb->select('gu.gid', 'g.displayname')
- ->from('group_user', 'gu')
- ->leftJoin('gu', 'groups', 'g', $qb->expr()->eq('gu.gid', 'g.gid'))
- ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
- ->execute();
- $groups = [];
- while ($row = $cursor->fetch()) {
- $groups[] = $row['gid'];
- $this->groupCache[$row['gid']] = [
- 'gid' => $row['gid'],
- 'displayname' => $row['displayname'],
- ];
- }
- $cursor->closeCursor();
- return $groups;
- }
-
- public function getGroups(string $search = '', int $limit = -1, int $offset = 0) {
- $this->fixDI();
- $query = $this->dbConn->getQueryBuilder();
- $query->select('gid')
- ->from('groups')
- ->orderBy('gid', 'ASC');
- if ($search !== '') {
- $query->where($query->expr()->iLike('gid', $query->createNamedParameter(
- '%' . $this->dbConn->escapeLikeParameter($search) . '%'
- )));
- $query->orWhere($query->expr()->iLike('displayname', $query->createNamedParameter(
- '%' . $this->dbConn->escapeLikeParameter($search) . '%'
- )));
- }
- if ($limit > 0) {
- $query->setMaxResults($limit);
- }
- if ($offset > 0) {
- $query->setFirstResult($offset);
- }
- $result = $query->execute();
- $groups = [];
- while ($row = $result->fetch()) {
- $groups[] = $row['gid'];
- }
- $result->closeCursor();
- return $groups;
- }
-
- public function groupExists($gid) {
- $this->fixDI();
-
- if (isset($this->groupCache[$gid])) {
- return true;
- }
- $qb = $this->dbConn->getQueryBuilder();
- $cursor = $qb->select('gid', 'displayname')
- ->from('groups')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->execute();
- $result = $cursor->fetch();
- $cursor->closeCursor();
- if ($result !== false) {
- $this->groupCache[$gid] = [
- 'gid' => $gid,
- 'displayname' => $result['displayname'],
- ];
- return true;
- }
- return false;
- }
-
- public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0): array {
- return array_values(array_map(fn ($user) => $user->getUid(), $this->searchInGroup($gid, $search, $limit, $offset)));
- }
- public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
- $this->fixDI();
- $query = $this->dbConn->getQueryBuilder();
- $query->select('g.uid', 'u.displayname');
- $query->from('group_user', 'g')
- ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
- ->orderBy('g.uid', 'ASC');
- $query->leftJoin('g', 'users', 'u', $query->expr()->eq('g.uid', 'u.uid'));
- if ($search !== '') {
- $query->leftJoin('u', 'preferences', 'p', $query->expr()->andX(
- $query->expr()->eq('p.userid', 'u.uid'),
- $query->expr()->eq('p.appid', $query->expr()->literal('settings')),
- $query->expr()->eq('p.configkey', $query->expr()->literal('email'))
- ))
-
- ->andWhere(
- $query->expr()->orX(
- $query->expr()->ilike('g.uid', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')),
- $query->expr()->ilike('u.displayname', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')),
- $query->expr()->ilike('p.configvalue', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))
- )
- )
- ->orderBy('u.uid_lower', 'ASC');
- }
- if ($limit !== -1) {
- $query->setMaxResults($limit);
- }
- if ($offset !== 0) {
- $query->setFirstResult($offset);
- }
- $result = $query->executeQuery();
- $users = [];
- $userManager = \OCP\Server::get(IUserManager::class);
- while ($row = $result->fetch()) {
- $users[$row['uid']] = new LazyUser($row['uid'], $userManager, $row['displayname'] ?? null);
- }
- $result->closeCursor();
- return $users;
- }
-
- public function countUsersInGroup(string $gid, string $search = ''): int {
- $this->fixDI();
- $query = $this->dbConn->getQueryBuilder();
- $query->select($query->func()->count('*', 'num_users'))
- ->from('group_user')
- ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
- if ($search !== '') {
- $query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
- '%' . $this->dbConn->escapeLikeParameter($search) . '%'
- )));
- }
- $result = $query->execute();
- $count = $result->fetchOne();
- $result->closeCursor();
- if ($count !== false) {
- $count = (int)$count;
- } else {
- $count = 0;
- }
- return $count;
- }
-
- public function countDisabledInGroup(string $gid): int {
- $this->fixDI();
- $query = $this->dbConn->getQueryBuilder();
- $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')'))
- ->from('preferences', 'p')
- ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid'))
- ->where($query->expr()->eq('appid', $query->createNamedParameter('core')))
- ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled')))
- ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR))
- ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR));
- $result = $query->execute();
- $count = $result->fetchOne();
- $result->closeCursor();
- if ($count !== false) {
- $count = (int)$count;
- } else {
- $count = 0;
- }
- return $count;
- }
- public function getDisplayName(string $gid): string {
- if (isset($this->groupCache[$gid])) {
- $displayName = $this->groupCache[$gid]['displayname'];
- if (isset($displayName) && trim($displayName) !== '') {
- return $displayName;
- }
- }
- $this->fixDI();
- $query = $this->dbConn->getQueryBuilder();
- $query->select('displayname')
- ->from('groups')
- ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
- $result = $query->execute();
- $displayName = $result->fetchOne();
- $result->closeCursor();
- return (string) $displayName;
- }
- public function getGroupDetails(string $gid): array {
- $displayName = $this->getDisplayName($gid);
- if ($displayName !== '') {
- return ['displayName' => $displayName];
- }
- return [];
- }
- public function setDisplayName(string $gid, string $displayName): bool {
- if (!$this->groupExists($gid)) {
- return false;
- }
- $this->fixDI();
- $displayName = trim($displayName);
- if ($displayName === '') {
- $displayName = $gid;
- }
- $query = $this->dbConn->getQueryBuilder();
- $query->update('groups')
- ->set('displayname', $query->createNamedParameter($displayName))
- ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
- $query->execute();
- return true;
- }
-
- public function getBackendName(): string {
- return 'Database';
- }
- }
|