aboutsummaryrefslogtreecommitdiffstats
path: root/apps/user_ldap/lib/Mapping/AbstractMapping.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/user_ldap/lib/Mapping/AbstractMapping.php')
-rw-r--r--apps/user_ldap/lib/Mapping/AbstractMapping.php70
1 files changed, 22 insertions, 48 deletions
diff --git a/apps/user_ldap/lib/Mapping/AbstractMapping.php b/apps/user_ldap/lib/Mapping/AbstractMapping.php
index 9026b8cfb78..fa10312a915 100644
--- a/apps/user_ldap/lib/Mapping/AbstractMapping.php
+++ b/apps/user_ldap/lib/Mapping/AbstractMapping.php
@@ -1,35 +1,18 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Aaron Wood <aaronjwood@gmail.com>
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author blizzz <blizzz@arthur-schiwon.de>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\User_LDAP\Mapping;
use Doctrine\DBAL\Exception;
-use OC\DB\QueryBuilder\QueryBuilder;
use OCP\DB\IPreparedStatement;
use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\IDBConnection;
+use OCP\Server;
+use Psr\Log\LoggerInterface;
/**
* Class AbstractMapping
@@ -38,11 +21,6 @@ use OCP\DB\QueryBuilder\IQueryBuilder;
*/
abstract class AbstractMapping {
/**
- * @var \OCP\IDBConnection $dbc
- */
- protected $dbc;
-
- /**
* returns the DB table name which holds the mappings
*
* @return string
@@ -50,10 +28,11 @@ abstract class AbstractMapping {
abstract protected function getTableName(bool $includePrefix = true);
/**
- * @param \OCP\IDBConnection $dbc
+ * @param IDBConnection $dbc
*/
- public function __construct(\OCP\IDBConnection $dbc) {
- $this->dbc = $dbc;
+ public function __construct(
+ protected IDBConnection $dbc,
+ ) {
}
/** @var array caches Names (value) by DN (key) */
@@ -191,12 +170,7 @@ abstract class AbstractMapping {
* Get the hash to store in database column ldap_dn_hash for a given dn
*/
protected function getDNHash(string $fdn): string {
- $hash = hash('sha256', $fdn, false);
- if (is_string($hash)) {
- return $hash;
- } else {
- throw new \RuntimeException('hash function did not return a string');
- }
+ return hash('sha256', $fdn, false);
}
/**
@@ -219,12 +193,12 @@ abstract class AbstractMapping {
$qb = $this->dbc->getQueryBuilder();
$qb->select('owncloud_name', 'ldap_dn_hash', 'ldap_dn')
->from($this->getTableName(false))
- ->where($qb->expr()->in('ldap_dn_hash', $qb->createNamedParameter($hashList, QueryBuilder::PARAM_STR_ARRAY)));
+ ->where($qb->expr()->in('ldap_dn_hash', $qb->createNamedParameter($hashList, IQueryBuilder::PARAM_STR_ARRAY)));
return $qb;
}
protected function collectResultsFromListOfIdsQuery(IQueryBuilder $qb, array &$results): void {
- $stmt = $qb->execute();
+ $stmt = $qb->executeQuery();
while ($entry = $stmt->fetch(\Doctrine\DBAL\FetchMode::ASSOCIATIVE)) {
$results[$entry['ldap_dn']] = $entry['owncloud_name'];
$this->cache[$entry['ldap_dn']] = $entry['owncloud_name'];
@@ -239,7 +213,7 @@ abstract class AbstractMapping {
public function getListOfIdsByDn(array $fdns): array {
$totalDBParamLimit = 65000;
$sliceSize = 1000;
- $maxSlices = $totalDBParamLimit / $sliceSize;
+ $maxSlices = $this->dbc->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE ? 9 : $totalDBParamLimit / $sliceSize;
$results = [];
$slice = 1;
@@ -261,7 +235,7 @@ abstract class AbstractMapping {
}
if (!empty($fdnsSlice)) {
- $qb->orWhere($qb->expr()->in('ldap_dn_hash', $qb->createNamedParameter($fdnsSlice, QueryBuilder::PARAM_STR_ARRAY)));
+ $qb->orWhere($qb->expr()->in('ldap_dn_hash', $qb->createNamedParameter($fdnsSlice, IQueryBuilder::PARAM_STR_ARRAY)));
}
if ($slice % $maxSlices === 0) {
@@ -282,7 +256,7 @@ abstract class AbstractMapping {
*
* @return string[]
*/
- public function getNamesBySearch(string $search, string $prefixMatch = "", string $postfixMatch = ""): array {
+ public function getNamesBySearch(string $search, string $prefixMatch = '', string $postfixMatch = ''): array {
$statement = $this->dbc->prepare('
SELECT `owncloud_name`
FROM `' . $this->getTableName() . '`
@@ -326,7 +300,7 @@ abstract class AbstractMapping {
return $this->getXbyY('directory_uuid', 'ldap_dn_hash', $this->getDNHash($dn));
}
- public function getList(int $offset = 0, int $limit = null, bool $invalidatedOnly = false): array {
+ public function getList(int $offset = 0, ?int $limit = null, bool $invalidatedOnly = false): array {
$select = $this->dbc->getQueryBuilder();
$select->selectAlias('ldap_dn', 'dn')
->selectAlias('owncloud_name', 'name')
@@ -356,7 +330,7 @@ abstract class AbstractMapping {
*/
public function map($fdn, $name, $uuid) {
if (mb_strlen($fdn) > 4000) {
- \OC::$server->getLogger()->error(
+ Server::get(LoggerInterface::class)->error(
'Cannot map, because the DN exceeds 4000 characters: {dn}',
[
'app' => 'user_ldap',
@@ -429,7 +403,7 @@ abstract class AbstractMapping {
* @param callable $preCallback
* @param callable $postCallback
* @return bool true on success, false when at least one row was not
- * deleted
+ * deleted
*/
public function clearCb(callable $preCallback, callable $postCallback): bool {
$picker = $this->dbc->getQueryBuilder();
@@ -457,7 +431,7 @@ abstract class AbstractMapping {
$query = $this->dbc->getQueryBuilder();
$query->select($query->func()->count('ldap_dn_hash'))
->from($this->getTableName());
- $res = $query->execute();
+ $res = $query->executeQuery();
$count = $res->fetchOne();
$res->closeCursor();
return (int)$count;
@@ -468,7 +442,7 @@ abstract class AbstractMapping {
$query->select($query->func()->count('ldap_dn_hash'))
->from($this->getTableName())
->where($query->expr()->like('directory_uuid', $query->createNamedParameter('invalidated_%')));
- $res = $query->execute();
+ $res = $query->executeQuery();
$count = $res->fetchOne();
$res->closeCursor();
return (int)$count;