|
| 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