2016-05-17 17:06:44 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-07-21 18:07:57 +03:00
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Loki3000 <github@labcms.ru>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2016-05-17 17:06:44 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-05-17 17:06:44 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* The following SQL statement is just a help for developers and will not be
|
|
|
|
* executed!
|
|
|
|
*
|
|
|
|
* CREATE TABLE `groups` (
|
|
|
|
* `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
* PRIMARY KEY (`gid`)
|
|
|
|
* ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
|
|
|
*
|
|
|
|
* CREATE TABLE `group_user` (
|
|
|
|
* `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
* `uid` varchar(64) COLLATE utf8_unicode_ci NOT NULL
|
|
|
|
* ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Group;
|
|
|
|
|
2018-11-12 19:36:35 +03:00
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
2018-05-16 13:32:56 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2018-04-23 15:39:30 +03:00
|
|
|
use OCP\Group\Backend\ABackend;
|
|
|
|
use OCP\Group\Backend\IAddToGroupBackend;
|
2018-05-16 13:32:56 +03:00
|
|
|
use OCP\Group\Backend\ICountDisabledInGroup;
|
2018-04-23 15:39:30 +03:00
|
|
|
use OCP\Group\Backend\ICountUsersBackend;
|
|
|
|
use OCP\Group\Backend\ICreateGroupBackend;
|
|
|
|
use OCP\Group\Backend\IDeleteGroupBackend;
|
2019-09-20 12:04:36 +03:00
|
|
|
use OCP\Group\Backend\IGetDisplayNameBackend;
|
|
|
|
use OCP\Group\Backend\IGroupDetailsBackend;
|
2018-04-23 15:39:30 +03:00
|
|
|
use OCP\Group\Backend\IRemoveFromGroupBackend;
|
2019-09-20 12:33:02 +03:00
|
|
|
use OCP\Group\Backend\ISetDisplayNameBackend;
|
2021-02-09 16:50:38 +03:00
|
|
|
use OCP\Group\Backend\INamedBackend;
|
2018-03-12 15:23:03 +03:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
2016-05-17 17:06:44 +03:00
|
|
|
/**
|
|
|
|
* Class for group management in a SQL Database (e.g. MySQL, SQLite)
|
|
|
|
*/
|
2020-04-10 17:56:50 +03:00
|
|
|
class Database extends ABackend implements
|
|
|
|
IAddToGroupBackend,
|
2020-04-09 10:22:29 +03:00
|
|
|
ICountDisabledInGroup,
|
|
|
|
ICountUsersBackend,
|
|
|
|
ICreateGroupBackend,
|
|
|
|
IDeleteGroupBackend,
|
|
|
|
IGetDisplayNameBackend,
|
|
|
|
IGroupDetailsBackend,
|
|
|
|
IRemoveFromGroupBackend,
|
2021-02-09 16:50:38 +03:00
|
|
|
ISetDisplayNameBackend,
|
|
|
|
INamedBackend {
|
2016-05-17 17:06:44 +03:00
|
|
|
|
|
|
|
/** @var string[] */
|
|
|
|
private $groupCache = [];
|
|
|
|
|
2018-03-12 15:23:03 +03:00
|
|
|
/** @var IDBConnection */
|
2016-05-17 17:06:44 +03:00
|
|
|
private $dbConn;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \OC\Group\Database constructor.
|
|
|
|
*
|
2018-03-12 15:23:03 +03:00
|
|
|
* @param IDBConnection|null $dbConn
|
2016-05-17 17:06:44 +03:00
|
|
|
*/
|
2018-03-12 15:23:03 +03:00
|
|
|
public function __construct(IDBConnection $dbConn = null) {
|
2016-05-17 17:06:44 +03:00
|
|
|
$this->dbConn = $dbConn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FIXME: This function should not be required!
|
|
|
|
*/
|
|
|
|
private function fixDI() {
|
|
|
|
if ($this->dbConn === null) {
|
|
|
|
$this->dbConn = \OC::$server->getDatabaseConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to create a new group
|
|
|
|
* @param string $gid The name of the group to create
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* Tries to create a new group. If the group name already exists, false will
|
|
|
|
* be returned.
|
|
|
|
*/
|
2018-04-23 15:39:30 +03:00
|
|
|
public function createGroup(string $gid): bool {
|
2016-05-17 17:06:44 +03:00
|
|
|
$this->fixDI();
|
|
|
|
|
2018-11-12 19:36:35 +03:00
|
|
|
try {
|
|
|
|
// Add group
|
|
|
|
$builder = $this->dbConn->getQueryBuilder();
|
|
|
|
$result = $builder->insert('groups')
|
|
|
|
->setValue('gid', $builder->createNamedParameter($gid))
|
2019-09-27 15:28:54 +03:00
|
|
|
->setValue('displayname', $builder->createNamedParameter($gid))
|
2018-11-12 19:36:35 +03:00
|
|
|
->execute();
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (UniqueConstraintViolationException $e) {
|
2018-11-12 19:36:35 +03:00
|
|
|
$result = 0;
|
|
|
|
}
|
2016-05-17 17:06:44 +03:00
|
|
|
|
|
|
|
// Add to cache
|
2020-05-01 21:02:16 +03:00
|
|
|
$this->groupCache[$gid] = [
|
|
|
|
'gid' => $gid,
|
|
|
|
'displayname' => $gid
|
|
|
|
];
|
2016-05-17 17:06:44 +03:00
|
|
|
|
|
|
|
return $result === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete a group
|
|
|
|
* @param string $gid gid of the group to delete
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* Deletes a group and removes it from the group_user-table
|
|
|
|
*/
|
2018-04-23 15:39:30 +03:00
|
|
|
public function deleteGroup(string $gid): bool {
|
2016-05-17 17:06:44 +03:00
|
|
|
$this->fixDI();
|
|
|
|
|
|
|
|
// Delete the group
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->delete('groups')
|
|
|
|
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
// Delete the group-user relation
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->delete('group_user')
|
|
|
|
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
// Delete the group-groupadmin relation
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->delete('group_admin')
|
|
|
|
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
// Delete from cache
|
|
|
|
unset($this->groupCache[$gid]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* is user in group?
|
|
|
|
* @param string $uid uid of the user
|
|
|
|
* @param string $gid gid of the group
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* Checks whether the user is member of a group or not.
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function inGroup($uid, $gid) {
|
2016-05-17 17:06:44 +03:00
|
|
|
$this->fixDI();
|
|
|
|
|
|
|
|
// check
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a user to a group
|
|
|
|
* @param string $uid Name of the user to add to group
|
|
|
|
* @param string $gid Name of the group in which add the user
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* Adds a user to a group.
|
|
|
|
*/
|
2018-04-23 15:39:30 +03:00
|
|
|
public function addToGroup(string $uid, string $gid): bool {
|
2016-05-17 17:06:44 +03:00
|
|
|
$this->fixDI();
|
|
|
|
|
|
|
|
// No duplicate entries!
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!$this->inGroup($uid, $gid)) {
|
2016-05-17 17:06:44 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$qb->insert('group_user')
|
|
|
|
->setValue('uid', $qb->createNamedParameter($uid))
|
|
|
|
->setValue('gid', $qb->createNamedParameter($gid))
|
|
|
|
->execute();
|
|
|
|
return true;
|
2020-04-10 15:19:56 +03:00
|
|
|
} else {
|
2016-05-17 17:06:44 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a user from a group
|
|
|
|
* @param string $uid Name of the user to remove from group
|
|
|
|
* @param string $gid Name of the group from which remove the user
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* removes the user from a group.
|
|
|
|
*/
|
2018-04-23 15:39:30 +03:00
|
|
|
public function removeFromGroup(string $uid, string $gid): bool {
|
2016-05-17 17:06:44 +03:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all groups a user belongs to
|
|
|
|
* @param string $uid Name of the user
|
|
|
|
* @return array an array of group names
|
|
|
|
*
|
|
|
|
* This function fetches all groups a user belongs to. It does not check
|
|
|
|
* if the user exists at all.
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function getUserGroups($uid) {
|
2017-01-10 16:37:18 +03:00
|
|
|
//guests has empty or null $uid
|
2017-01-10 16:43:43 +03:00
|
|
|
if ($uid === null || $uid === '') {
|
2017-01-10 13:29:35 +03:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2016-05-17 17:06:44 +03:00
|
|
|
$this->fixDI();
|
2017-01-10 14:21:23 +03:00
|
|
|
|
2016-05-17 17:06:44 +03:00
|
|
|
// No magic!
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
2020-05-01 21:02:16 +03:00
|
|
|
$cursor = $qb->select('gu.gid', 'g.displayname')
|
|
|
|
->from('group_user', 'gu')
|
|
|
|
->leftJoin('gu', 'groups', 'g', $qb->expr()->eq('gu.gid', 'g.gid'))
|
2016-05-17 17:06:44 +03:00
|
|
|
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$groups = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
while ($row = $cursor->fetch()) {
|
2018-03-12 15:23:03 +03:00
|
|
|
$groups[] = $row['gid'];
|
2020-05-01 21:02:16 +03:00
|
|
|
$this->groupCache[$row['gid']] = [
|
|
|
|
'gid' => $row['gid'],
|
|
|
|
'displayname' => $row['displayname'],
|
|
|
|
];
|
2016-05-17 17:06:44 +03:00
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get a list of all groups
|
|
|
|
* @param string $search
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return array an array of group names
|
|
|
|
*
|
|
|
|
* Returns a list with all groups
|
|
|
|
*/
|
|
|
|
public function getGroups($search = '', $limit = null, $offset = null) {
|
2018-03-15 13:09:18 +03:00
|
|
|
$this->fixDI();
|
|
|
|
|
2018-03-12 15:23:03 +03:00
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
|
|
|
$query->select('gid')
|
|
|
|
->from('groups')
|
|
|
|
->orderBy('gid', 'ASC');
|
|
|
|
|
2016-05-17 17:06:44 +03:00
|
|
|
if ($search !== '') {
|
2018-03-12 15:23:03 +03:00
|
|
|
$query->where($query->expr()->iLike('gid', $query->createNamedParameter(
|
|
|
|
'%' . $this->dbConn->escapeLikeParameter($search) . '%'
|
|
|
|
)));
|
2020-06-10 12:44:07 +03:00
|
|
|
$query->orWhere($query->expr()->iLike('displayname', $query->createNamedParameter(
|
|
|
|
'%' . $this->dbConn->escapeLikeParameter($search) . '%'
|
|
|
|
)));
|
2016-05-17 17:06:44 +03:00
|
|
|
}
|
|
|
|
|
2018-03-12 15:23:03 +03:00
|
|
|
$query->setMaxResults($limit)
|
|
|
|
->setFirstResult($offset);
|
|
|
|
$result = $query->execute();
|
|
|
|
|
|
|
|
$groups = [];
|
|
|
|
while ($row = $result->fetch()) {
|
2016-05-17 17:06:44 +03:00
|
|
|
$groups[] = $row['gid'];
|
|
|
|
}
|
2018-03-12 15:23:03 +03:00
|
|
|
$result->closeCursor();
|
|
|
|
|
2016-05-17 17:06:44 +03:00
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if a group exists
|
|
|
|
* @param string $gid
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function groupExists($gid) {
|
|
|
|
$this->fixDI();
|
|
|
|
|
|
|
|
// Check cache first
|
|
|
|
if (isset($this->groupCache[$gid])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
2020-05-01 21:02:16 +03:00
|
|
|
$cursor = $qb->select('gid', 'displayname')
|
2016-05-17 17:06:44 +03:00
|
|
|
->from('groups')
|
|
|
|
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
|
|
|
|
->execute();
|
|
|
|
$result = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
if ($result !== false) {
|
2020-05-01 21:02:16 +03:00
|
|
|
$this->groupCache[$gid] = [
|
|
|
|
'gid' => $gid,
|
|
|
|
'displayname' => $result['displayname'],
|
|
|
|
];
|
2016-05-17 17:06:44 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get a list of all users in a group
|
|
|
|
* @param string $gid
|
|
|
|
* @param string $search
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return array an array of user ids
|
|
|
|
*/
|
2019-10-02 10:43:35 +03:00
|
|
|
public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
|
2018-03-15 13:09:18 +03:00
|
|
|
$this->fixDI();
|
|
|
|
|
2018-03-12 15:23:03 +03:00
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
2020-05-28 18:26:07 +03:00
|
|
|
$query->select('g.uid')
|
|
|
|
->from('group_user', 'g')
|
2018-03-12 15:23:03 +03:00
|
|
|
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
|
2020-05-28 18:26:07 +03:00
|
|
|
->orderBy('g.uid', 'ASC');
|
2018-03-12 15:23:03 +03:00
|
|
|
|
2016-05-17 17:06:44 +03:00
|
|
|
if ($search !== '') {
|
2020-05-28 18:26:07 +03:00
|
|
|
$query->leftJoin('g', 'users', 'u', $query->expr()->eq('g.uid', 'u.uid'))
|
|
|
|
->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')))
|
|
|
|
)
|
|
|
|
// sqlite doesn't like re-using a single named parameter here
|
|
|
|
->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');
|
2016-05-17 17:06:44 +03:00
|
|
|
}
|
|
|
|
|
2019-10-02 10:43:35 +03:00
|
|
|
if ($limit !== -1) {
|
|
|
|
$query->setMaxResults($limit);
|
|
|
|
}
|
|
|
|
if ($offset !== 0) {
|
|
|
|
$query->setFirstResult($offset);
|
|
|
|
}
|
|
|
|
|
2018-03-12 15:23:03 +03:00
|
|
|
$result = $query->execute();
|
|
|
|
|
|
|
|
$users = [];
|
|
|
|
while ($row = $result->fetch()) {
|
2016-05-17 17:06:44 +03:00
|
|
|
$users[] = $row['uid'];
|
|
|
|
}
|
2018-03-12 15:23:03 +03:00
|
|
|
$result->closeCursor();
|
|
|
|
|
2016-05-17 17:06:44 +03:00
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the number of all users matching the search string in a group
|
|
|
|
* @param string $gid
|
|
|
|
* @param string $search
|
2018-04-23 15:39:30 +03:00
|
|
|
* @return int
|
2016-05-17 17:06:44 +03:00
|
|
|
*/
|
2018-04-23 15:39:30 +03:00
|
|
|
public function countUsersInGroup(string $gid, string $search = ''): int {
|
2018-03-15 13:09:18 +03:00
|
|
|
$this->fixDI();
|
|
|
|
|
2018-03-12 15:23:03 +03:00
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
2018-10-19 17:44:28 +03:00
|
|
|
$query->select($query->func()->count('*', 'num_users'))
|
2018-03-12 15:23:03 +03:00
|
|
|
->from('group_user')
|
2018-03-27 15:50:09 +03:00
|
|
|
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
|
2018-03-12 15:23:03 +03:00
|
|
|
|
2016-05-17 17:06:44 +03:00
|
|
|
if ($search !== '') {
|
2018-03-12 15:23:03 +03:00
|
|
|
$query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
|
|
|
|
'%' . $this->dbConn->escapeLikeParameter($search) . '%'
|
|
|
|
)));
|
2016-05-17 17:06:44 +03:00
|
|
|
}
|
|
|
|
|
2018-03-12 15:23:03 +03:00
|
|
|
$result = $query->execute();
|
2021-01-03 17:28:31 +03:00
|
|
|
$count = $result->fetchOne();
|
2018-03-12 15:23:03 +03:00
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
if ($count !== false) {
|
2018-01-26 17:01:27 +03:00
|
|
|
$count = (int)$count;
|
2018-04-23 15:39:30 +03:00
|
|
|
} else {
|
|
|
|
$count = 0;
|
2016-05-17 17:06:44 +03:00
|
|
|
}
|
2018-04-23 15:39:30 +03:00
|
|
|
|
2016-05-17 17:06:44 +03:00
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
2018-05-16 13:32:56 +03:00
|
|
|
/**
|
|
|
|
* get the number of disabled users in a group
|
|
|
|
*
|
|
|
|
* @param string $search
|
2020-03-06 18:33:01 +03:00
|
|
|
*
|
|
|
|
* @return int
|
2018-05-16 13:32:56 +03:00
|
|
|
*/
|
|
|
|
public function countDisabledInGroup(string $gid): int {
|
|
|
|
$this->fixDI();
|
2019-09-20 12:04:36 +03:00
|
|
|
|
2018-05-16 13:32:56 +03:00
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
2018-10-16 15:55:41 +03:00
|
|
|
$query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')'))
|
2018-05-16 13:32:56 +03:00
|
|
|
->from('preferences', 'p')
|
2018-10-16 15:55:41 +03:00
|
|
|
->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid'))
|
2018-05-16 13:32:56 +03:00
|
|
|
->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));
|
2019-09-20 12:04:36 +03:00
|
|
|
|
2018-05-16 13:32:56 +03:00
|
|
|
$result = $query->execute();
|
2021-01-03 17:28:31 +03:00
|
|
|
$count = $result->fetchOne();
|
2018-05-16 13:32:56 +03:00
|
|
|
$result->closeCursor();
|
2019-09-20 12:04:36 +03:00
|
|
|
|
2018-05-16 13:32:56 +03:00
|
|
|
if ($count !== false) {
|
|
|
|
$count = (int)$count;
|
|
|
|
} else {
|
|
|
|
$count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
2019-09-20 12:04:36 +03:00
|
|
|
public function getDisplayName(string $gid): string {
|
2020-05-01 21:02:16 +03:00
|
|
|
if (isset($this->groupCache[$gid])) {
|
|
|
|
return $this->groupCache[$gid]['displayname'];
|
|
|
|
}
|
|
|
|
|
2019-09-23 11:12:30 +03:00
|
|
|
$this->fixDI();
|
|
|
|
|
2019-09-20 12:04:36 +03:00
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
|
|
|
$query->select('displayname')
|
|
|
|
->from('groups')
|
|
|
|
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
|
|
|
|
|
|
|
|
$result = $query->execute();
|
2021-01-03 17:28:31 +03:00
|
|
|
$displayName = $result->fetchOne();
|
2019-09-20 12:04:36 +03:00
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return (string) $displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGroupDetails(string $gid): array {
|
|
|
|
$displayName = $this->getDisplayName($gid);
|
|
|
|
if ($displayName !== '') {
|
|
|
|
return ['displayName' => $displayName];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-09-20 12:33:02 +03:00
|
|
|
public function setDisplayName(string $gid, string $displayName): bool {
|
|
|
|
if (!$this->groupExists($gid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-23 11:12:30 +03:00
|
|
|
$this->fixDI();
|
|
|
|
|
2019-09-27 15:28:54 +03:00
|
|
|
$displayName = trim($displayName);
|
|
|
|
if ($displayName === '') {
|
|
|
|
$displayName = $gid;
|
|
|
|
}
|
|
|
|
|
2019-09-20 12:33:02 +03:00
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
|
|
|
$query->update('groups')
|
|
|
|
->set('displayname', $query->createNamedParameter($displayName))
|
|
|
|
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-09 16:50:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Backend name to be shown in group management
|
|
|
|
* @return string the name of the backend to be shown
|
|
|
|
* @since 21.0.0
|
|
|
|
*/
|
|
|
|
public function getBackendName(): string {
|
|
|
|
return 'Database';
|
|
|
|
}
|
2016-05-17 17:06:44 +03:00
|
|
|
}
|