aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Group/Database.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Group/Database.php')
-rw-r--r--lib/private/Group/Database.php104
1 files changed, 43 insertions, 61 deletions
diff --git a/lib/private/Group/Database.php b/lib/private/Group/Database.php
index c4915eefeae..0cb571a3935 100644
--- a/lib/private/Group/Database.php
+++ b/lib/private/Group/Database.php
@@ -1,31 +1,9 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Johannes Leuker <j.leuker@hosting.de>
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Loki3000 <github@labcms.ru>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author tgrant <tom.grant760@gmail.com>
- * @author Tom Grant <TomG736@users.noreply.github.com>
- *
- * @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,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\Group;
@@ -37,7 +15,7 @@ use OCP\Group\Backend\IAddToGroupBackend;
use OCP\Group\Backend\IBatchMethodsBackend;
use OCP\Group\Backend\ICountDisabledInGroup;
use OCP\Group\Backend\ICountUsersBackend;
-use OCP\Group\Backend\ICreateGroupBackend;
+use OCP\Group\Backend\ICreateNamedGroupBackend;
use OCP\Group\Backend\IDeleteGroupBackend;
use OCP\Group\Backend\IGetDisplayNameBackend;
use OCP\Group\Backend\IGroupDetailsBackend;
@@ -55,7 +33,7 @@ class Database extends ABackend implements
IAddToGroupBackend,
ICountDisabledInGroup,
ICountUsersBackend,
- ICreateGroupBackend,
+ ICreateNamedGroupBackend,
IDeleteGroupBackend,
IGetDisplayNameBackend,
IGroupDetailsBackend,
@@ -66,15 +44,15 @@ class Database extends ABackend implements
INamedBackend {
/** @var array<string, array{gid: string, displayname: string}> */
private $groupCache = [];
- private ?IDBConnection $dbConn;
/**
* \OC\Group\Database constructor.
*
* @param IDBConnection|null $dbConn
*/
- public function __construct(?IDBConnection $dbConn = null) {
- $this->dbConn = $dbConn;
+ public function __construct(
+ private ?IDBConnection $dbConn = null,
+ ) {
}
/**
@@ -86,35 +64,28 @@ class Database extends ABackend implements
}
}
- /**
- * 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.
- */
- public function createGroup(string $gid): bool {
+ public function createGroup(string $name): ?string {
$this->fixDI();
+ $gid = $this->computeGid($name);
try {
// Add group
$builder = $this->dbConn->getQueryBuilder();
$result = $builder->insert('groups')
->setValue('gid', $builder->createNamedParameter($gid))
- ->setValue('displayname', $builder->createNamedParameter($gid))
+ ->setValue('displayname', $builder->createNamedParameter($name))
->execute();
} catch (UniqueConstraintViolationException $e) {
- $result = 0;
+ return null;
}
// Add to cache
$this->groupCache[$gid] = [
'gid' => $gid,
- 'displayname' => $gid
+ 'displayname' => $name
];
- return $result === 1;
+ return $gid;
}
/**
@@ -131,19 +102,19 @@ class Database extends ABackend implements
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('groups')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->execute();
+ ->executeStatement();
// Delete the group-user relation
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('group_user')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->execute();
+ ->executeStatement();
// Delete the group-groupadmin relation
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('group_admin')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->execute();
+ ->executeStatement();
// Delete from cache
unset($this->groupCache[$gid]);
@@ -168,7 +139,7 @@ class Database extends ABackend implements
->from('group_user')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
- ->execute();
+ ->executeQuery();
$result = $cursor->fetch();
$cursor->closeCursor();
@@ -193,7 +164,7 @@ class Database extends ABackend implements
$qb->insert('group_user')
->setValue('uid', $qb->createNamedParameter($uid))
->setValue('gid', $qb->createNamedParameter($gid))
- ->execute();
+ ->executeStatement();
return true;
} else {
return false;
@@ -215,7 +186,7 @@ class Database extends ABackend implements
$qb->delete('group_user')
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->execute();
+ ->executeStatement();
return true;
}
@@ -223,7 +194,7 @@ class Database extends ABackend implements
/**
* Get all groups a user belongs to
* @param string $uid Name of the user
- * @return array an array of group names
+ * @return list<string> an array of group names
*
* This function fetches all groups a user belongs to. It does not check
* if the user exists at all.
@@ -242,7 +213,7 @@ class Database extends ABackend implements
->from('group_user', 'gu')
->leftJoin('gu', 'groups', 'g', $qb->expr()->eq('gu.gid', 'g.gid'))
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
- ->execute();
+ ->executeQuery();
$groups = [];
while ($row = $cursor->fetch()) {
@@ -289,7 +260,7 @@ class Database extends ABackend implements
if ($offset > 0) {
$query->setFirstResult($offset);
}
- $result = $query->execute();
+ $result = $query->executeQuery();
$groups = [];
while ($row = $result->fetch()) {
@@ -321,7 +292,7 @@ class Database extends ABackend implements
$cursor = $qb->select('gid', 'displayname')
->from('groups')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
- ->execute();
+ ->executeQuery();
$result = $cursor->fetch();
$cursor->closeCursor();
@@ -354,8 +325,8 @@ class Database extends ABackend implements
$qb = $this->dbConn->getQueryBuilder();
$qb->select('gid', 'displayname')
- ->from('groups')
- ->where($qb->expr()->in('gid', $qb->createParameter('ids')));
+ ->from('groups')
+ ->where($qb->expr()->in('gid', $qb->createParameter('ids')));
foreach (array_chunk($notFoundGids, 1000) as $chunk) {
$qb->setParameter('ids', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
$result = $qb->executeQuery();
@@ -452,7 +423,7 @@ class Database extends ABackend implements
)));
}
- $result = $query->execute();
+ $result = $query->executeQuery();
$count = $result->fetchOne();
$result->closeCursor();
@@ -484,7 +455,7 @@ class Database extends ABackend implements
->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();
+ $result = $query->executeQuery();
$count = $result->fetchOne();
$result->closeCursor();
@@ -513,11 +484,11 @@ class Database extends ABackend implements
->from('groups')
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
- $result = $query->execute();
+ $result = $query->executeQuery();
$displayName = $result->fetchOne();
$result->closeCursor();
- return (string) $displayName;
+ return (string)$displayName;
}
public function getGroupDetails(string $gid): array {
@@ -536,6 +507,8 @@ class Database extends ABackend implements
$notFoundGids = [];
$details = [];
+ $this->fixDI();
+
// In case the data is already locally accessible, not need to do SQL query
// or do a SQL query but with a smaller in clause
foreach ($gids as $gid) {
@@ -582,7 +555,7 @@ class Database extends ABackend implements
$query->update('groups')
->set('displayname', $query->createNamedParameter($displayName))
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
- $query->execute();
+ $query->executeStatement();
return true;
}
@@ -595,4 +568,13 @@ class Database extends ABackend implements
public function getBackendName(): string {
return 'Database';
}
+
+ /**
+ * Compute group ID from display name (GIDs are limited to 64 characters in database)
+ */
+ private function computeGid(string $displayName): string {
+ return mb_strlen($displayName) > 64
+ ? hash('sha256', $displayName)
+ : $displayName;
+ }
}