From 6365582631df05517c39bff27bb67579190a0b41 Mon Sep 17 00:00:00 2001 From: Lidiya Gelemeev <l_gele02@uni-muenster.de> Date: Wed, 12 Feb 2025 13:50:44 +0100 Subject: [PATCH] Fixes the active users fetching --- lib/Db/ExtMapper.php | 2 ++ lib/Db/OC/AccountsMapper.php | 34 ++++++++++++++++++++++++++++++---- lib/Db/OC/FilecacheMapper.php | 10 +++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/Db/ExtMapper.php b/lib/Db/ExtMapper.php index 2086674..c06cb3f 100644 --- a/lib/Db/ExtMapper.php +++ b/lib/Db/ExtMapper.php @@ -9,6 +9,7 @@ use OCP\AppFramework\Db\{ MultipleObjectsReturnedException, QBMapper }; +use OCA\SccuotNC\Db\OC\AccountsMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; @@ -157,4 +158,5 @@ abstract class ExtMapper extends QBMapper { } return $offset; } + } diff --git a/lib/Db/OC/AccountsMapper.php b/lib/Db/OC/AccountsMapper.php index 2383f42..f7ef7ec 100644 --- a/lib/Db/OC/AccountsMapper.php +++ b/lib/Db/OC/AccountsMapper.php @@ -9,6 +9,7 @@ use OCP\DB\Exception; use OCP\IDBConnection; use OCP\DB\QueryBuilder\IQueryBuilder; use OCA\SccuotNC\Service\ConfigService; +use PDO; class AccountsMapper extends ExtMapper { const PREFERENCES_TABLE = 'preferences'; @@ -20,8 +21,19 @@ class AccountsMapper extends ExtMapper { } public function queryTotalCount(string $state): int { - $users = $this->configService->config()->getUsersForUserValue('core', 'enabled', $state); - return count($users); + $users = $this->configService->config()->getUsersForUserValue('core', 'enabled', 'false'); + if ($state === 'false') { + return count($users); + } + $qb = $this->db->getQueryBuilder(); + $qb->selectAlias($qb->createFunction('COUNT(uid)'), 'c') + ->from('accounts'); + + $stmt = $qb->execute(); + $result = $stmt->fetch(); + $stmt->closeCursor(); + + return $result['c'] - count($users); } /** @@ -45,7 +57,7 @@ class AccountsMapper extends ExtMapper { * @throws \Exception */ public function queryUserStorages(): array { - $activeUsers = $this->configService->config()->getUsersForUserValue('core', 'enabled', 'true'); + $activeUsers = $this->getActiveUsers(); $data = [ 'storages' => [] ]; @@ -76,7 +88,7 @@ class AccountsMapper extends ExtMapper { } public function queryUserQuotas($defaultQuotas) { - $activeUsers = $this->configService->config()->getUsersForUserValue('core', 'enabled', 'true'); + $activeUsers = $this->getActiveUsers(); $data = [ 'defaultQuotas' => $defaultQuotas, 'quotas' => [] @@ -121,4 +133,18 @@ class AccountsMapper extends ExtMapper { return false; } + protected function getActiveUsers(): array { + $deactivatedUsers = $this->configService->config()->getUsersForUserValue('core', 'enabled', 'false'); + $qb = $this->db->getQueryBuilder(); + $qb->select('uid') + ->from('accounts'); + + $stmt = $qb->execute(); + $result = $stmt->fetchAll(PDO::FETCH_COLUMN); + $stmt->closeCursor(); + + $activeUsers = array_diff($result, $deactivatedUsers); + return $activeUsers; + } + } diff --git a/lib/Db/OC/FilecacheMapper.php b/lib/Db/OC/FilecacheMapper.php index a7d9080..72da569 100644 --- a/lib/Db/OC/FilecacheMapper.php +++ b/lib/Db/OC/FilecacheMapper.php @@ -141,7 +141,15 @@ class FilecacheMapper extends ExtMapper { $qb->selectDistinct('userid') ->from(AccountsMapper::PREFERENCES_TABLE) ->where($qb->expr()->eq('configkey', $qb->expr()->literal('enabled'))) - ->andWhere($qb->expr()->eq('configvalue', $qb->expr()->literal($this->accountsState))); + ->andWhere($qb->expr()->eq('configvalue', $qb->expr()->literal('false'))); + + $query->andWhere($query->expr()->eq('configkey', $query->expr()->literal('%quota%'))); + if ($this->accountsState == 'true') { + $query->andWhere($query->expr()->notIn('p.userid', $query->createFunction($qb->getSQL()))); + } else { + $query->andWhere($query->expr()->in('p.userid', $query->createFunction($qb->getSQL()))); + } + } } -- GitLab