Version1120Date20210917155206.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\User_LDAP\Migration;
  8. use Closure;
  9. use OC\Hooks\PublicEmitter;
  10. use OCP\DB\Exception;
  11. use OCP\DB\ISchemaWrapper;
  12. use OCP\DB\QueryBuilder\IQueryBuilder;
  13. use OCP\IDBConnection;
  14. use OCP\IUserManager;
  15. use OCP\Migration\IOutput;
  16. use OCP\Migration\SimpleMigrationStep;
  17. use Psr\Log\LoggerInterface;
  18. class Version1120Date20210917155206 extends SimpleMigrationStep {
  19. public function __construct(
  20. private IDBConnection $dbc,
  21. private IUserManager $userManager,
  22. private LoggerInterface $logger,
  23. ) {
  24. }
  25. public function getName() {
  26. return 'Adjust LDAP user and group id column lengths to match server lengths';
  27. }
  28. /**
  29. * @param IOutput $output
  30. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  31. * @param array $options
  32. */
  33. public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
  34. // ensure that there is no user or group id longer than 64char in LDAP table
  35. $this->handleIDs('ldap_group_mapping', false);
  36. $this->handleIDs('ldap_user_mapping', true);
  37. }
  38. /**
  39. * @param IOutput $output
  40. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  41. * @param array $options
  42. * @return null|ISchemaWrapper
  43. */
  44. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  45. /** @var ISchemaWrapper $schema */
  46. $schema = $schemaClosure();
  47. $changeSchema = false;
  48. foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) {
  49. $table = $schema->getTable($tableName);
  50. $column = $table->getColumn('owncloud_name');
  51. if ($column->getLength() > 64) {
  52. $column->setLength(64);
  53. $changeSchema = true;
  54. }
  55. }
  56. return $changeSchema ? $schema : null;
  57. }
  58. protected function handleIDs(string $table, bool $emitHooks) {
  59. $select = $this->getSelectQuery($table);
  60. $update = $this->getUpdateQuery($table);
  61. $result = $select->executeQuery();
  62. while ($row = $result->fetch()) {
  63. $newId = hash('sha256', $row['owncloud_name'], false);
  64. if ($emitHooks) {
  65. $this->emitUnassign($row['owncloud_name'], true);
  66. }
  67. $update->setParameter('uuid', $row['directory_uuid']);
  68. $update->setParameter('newId', $newId);
  69. try {
  70. $update->executeStatement();
  71. if ($emitHooks) {
  72. $this->emitUnassign($row['owncloud_name'], false);
  73. $this->emitAssign($newId);
  74. }
  75. } catch (Exception $e) {
  76. $this->logger->error('Failed to shorten owncloud_name "{oldId}" to "{newId}" (UUID: "{uuid}" of {table})',
  77. [
  78. 'app' => 'user_ldap',
  79. 'oldId' => $row['owncloud_name'],
  80. 'newId' => $newId,
  81. 'uuid' => $row['directory_uuid'],
  82. 'table' => $table,
  83. 'exception' => $e,
  84. ]
  85. );
  86. }
  87. }
  88. $result->closeCursor();
  89. }
  90. protected function getSelectQuery(string $table): IQueryBuilder {
  91. $qb = $this->dbc->getQueryBuilder();
  92. $qb->select('owncloud_name', 'directory_uuid')
  93. ->from($table)
  94. ->where($qb->expr()->gt($qb->func()->octetLength('owncloud_name'), $qb->createNamedParameter('64'), IQueryBuilder::PARAM_INT));
  95. return $qb;
  96. }
  97. protected function getUpdateQuery(string $table): IQueryBuilder {
  98. $qb = $this->dbc->getQueryBuilder();
  99. $qb->update($table)
  100. ->set('owncloud_name', $qb->createParameter('newId'))
  101. ->where($qb->expr()->eq('directory_uuid', $qb->createParameter('uuid')));
  102. return $qb;
  103. }
  104. protected function emitUnassign(string $oldId, bool $pre): void {
  105. if ($this->userManager instanceof PublicEmitter) {
  106. $this->userManager->emit('\OC\User', $pre ? 'pre' : 'post' . 'UnassignedUserId', [$oldId]);
  107. }
  108. }
  109. protected function emitAssign(string $newId): void {
  110. if ($this->userManager instanceof PublicEmitter) {
  111. $this->userManager->emit('\OC\User', 'assignedUserId', [$newId]);
  112. }
  113. }
  114. }