Version1141Date20220323143801.php 3.3 KB

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