Skip to content

Commit 0505df3

Browse files
authored
Merge pull request #35755 from nextcloud/backport/31683/stable22
[stable22] limit ldap_dn columns to 4000
2 parents ea6aff5 + b19b0c8 commit 0505df3

File tree

7 files changed

+123
-8
lines changed

7 files changed

+123
-8
lines changed

apps/user_ldap/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154717' => $baseDir . '/../lib/Migration/Version1130Date20220110154717.php',
6969
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154718' => $baseDir . '/../lib/Migration/Version1130Date20220110154718.php',
7070
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154719' => $baseDir . '/../lib/Migration/Version1130Date20220110154719.php',
71+
'OCA\\User_LDAP\\Migration\\Version1141Date20220323143801' => $baseDir . '/../lib/Migration/Version1141Date20220323143801.php',
7172
'OCA\\User_LDAP\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
7273
'OCA\\User_LDAP\\PagedResults\\IAdapter' => $baseDir . '/../lib/PagedResults/IAdapter.php',
7374
'OCA\\User_LDAP\\PagedResults\\Php73' => $baseDir . '/../lib/PagedResults/Php73.php',

apps/user_ldap/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class ComposerStaticInitUser_LDAP
8383
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154717' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154717.php',
8484
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154718' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154718.php',
8585
'OCA\\User_LDAP\\Migration\\Version1130Date20220110154719' => __DIR__ . '/..' . '/../lib/Migration/Version1130Date20220110154719.php',
86+
'OCA\\User_LDAP\\Migration\\Version1141Date20220323143801' => __DIR__ . '/..' . '/../lib/Migration/Version1141Date20220323143801.php',
8687
'OCA\\User_LDAP\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
8788
'OCA\\User_LDAP\\PagedResults\\IAdapter' => __DIR__ . '/..' . '/../lib/PagedResults/IAdapter.php',
8889
'OCA\\User_LDAP\\PagedResults\\Php73' => __DIR__ . '/..' . '/../lib/PagedResults/Php73.php',

apps/user_ldap/lib/Mapping/AbstractMapping.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,9 @@ public function getList(int $offset = 0, int $limit = null, bool $invalidatedOnl
358358
* @return bool
359359
*/
360360
public function map($fdn, $name, $uuid) {
361-
if (mb_strlen($fdn) > 4096) {
361+
if (mb_strlen($fdn) > 4000) {
362362
\OC::$server->getLogger()->error(
363-
'Cannot map, because the DN exceeds 4096 characters: {dn}',
363+
'Cannot map, because the DN exceeds 4000 characters: {dn}',
364364
[
365365
'app' => 'user_ldap',
366366
'dn' => $fdn,

apps/user_ldap/lib/Migration/Version1010Date20200630192842.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
4747
$table = $schema->createTable('ldap_user_mapping');
4848
$table->addColumn('ldap_dn', Types::STRING, [
4949
'notnull' => true,
50-
'length' => 255,
50+
'length' => 4000,
5151
'default' => '',
5252
]);
5353
$table->addColumn('owncloud_name', Types::STRING, [
@@ -73,7 +73,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
7373
$table = $schema->createTable('ldap_group_mapping');
7474
$table->addColumn('ldap_dn', Types::STRING, [
7575
'notnull' => true,
76-
'length' => 255,
76+
'length' => 4000,
7777
'default' => '',
7878
]);
7979
$table->addColumn('owncloud_name', Types::STRING, [

apps/user_ldap/lib/Migration/Version1130Date20211102154716.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
9595
}
9696
$column = $table->getColumn('ldap_dn');
9797
if ($tableName === 'ldap_user_mapping') {
98-
if ($column->getLength() < 4096) {
99-
$column->setLength(4096);
98+
if ($column->getLength() < 4000) {
99+
$column->setLength(4000);
100100
$changeSchema = true;
101101
}
102102

@@ -117,7 +117,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
117117
$table2 = $schema->createTable('ldap_group_mapping_backup');
118118
$table2->addColumn('ldap_dn', Types::STRING, [
119119
'notnull' => true,
120-
'length' => 4096,
120+
'length' => 4000,
121121
'default' => '',
122122
]);
123123
$table2->addColumn('owncloud_name', Types::STRING, [

apps/user_ldap/lib/Migration/Version1130Date20220110154718.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
5454
$table = $schema->createTable('ldap_group_mapping');
5555
$table->addColumn('ldap_dn', Types::STRING, [
5656
'notnull' => true,
57-
'length' => 4096,
57+
'length' => 4000,
5858
'default' => '',
5959
]);
6060
$table->addColumn('owncloud_name', Types::STRING, [
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Arthur Schiwon <[email protected]>
7+
*
8+
* @author Arthur Schiwon <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\User_LDAP\Migration;
28+
29+
use Closure;
30+
use OCP\DB\ISchemaWrapper;
31+
use OCP\DB\QueryBuilder\IQueryBuilder;
32+
use OCP\IDBConnection;
33+
use OCP\Migration\IOutput;
34+
use OCP\Migration\SimpleMigrationStep;
35+
36+
class Version1141Date20220323143801 extends SimpleMigrationStep {
37+
/** @var IDBConnection */
38+
private $dbc;
39+
40+
public function __construct(IDBConnection $dbc) {
41+
$this->dbc = $dbc;
42+
}
43+
44+
/**
45+
* @param IOutput $output
46+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
47+
* @param array $options
48+
*/
49+
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
50+
foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) {
51+
$qb = $this->dbc->getQueryBuilder();
52+
$qb->select('ldap_dn')
53+
->from($tableName)
54+
->where($qb->expr()->gt($qb->func()->octetLength('ldap_dn'), '4000', IQueryBuilder::PARAM_INT));
55+
56+
$dnsTooLong = [];
57+
$result = $qb->executeQuery();
58+
while (($dn = $result->fetchOne()) !== false) {
59+
$dnsTooLong[] = $dn;
60+
}
61+
$result->closeCursor();
62+
$this->shortenDNs($dnsTooLong, $tableName);
63+
}
64+
}
65+
66+
protected function shortenDNs(array $dns, string $table): void {
67+
$qb = $this->dbc->getQueryBuilder();
68+
$qb->update($table)
69+
->set('ldap_dn', $qb->createParameter('shortenedDn'))
70+
->where($qb->expr()->eq('ldap_dn', $qb->createParameter('originalDn')));
71+
72+
$pageSize = 1000;
73+
$page = 0;
74+
do {
75+
$subset = array_slice($dns, $page * $pageSize, $pageSize);
76+
try {
77+
$this->dbc->beginTransaction();
78+
foreach ($subset as $dn) {
79+
$shortenedDN = mb_substr($dn, 0, 4000);
80+
$qb->setParameter('shortenedDn', $shortenedDN);
81+
$qb->setParameter('originalDn', $dn);
82+
$qb->executeStatement();
83+
}
84+
$this->dbc->commit();
85+
} catch (\Throwable $t) {
86+
$this->dbc->rollBack();
87+
throw $t;
88+
}
89+
$page++;
90+
} while (count($subset) === $pageSize);
91+
}
92+
93+
/**
94+
* @param IOutput $output
95+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
96+
* @param array $options
97+
* @return null|ISchemaWrapper
98+
*/
99+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
100+
/** @var ISchemaWrapper $schema */
101+
$schema = $schemaClosure();
102+
103+
foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) {
104+
$table = $schema->getTable($tableName);
105+
$column = $table->getColumn('ldap_dn');
106+
if ($column->getLength() > 4000) {
107+
$column->setLength(4000);
108+
}
109+
}
110+
111+
return $schema;
112+
}
113+
}

0 commit comments

Comments
 (0)