Version1120Date20210917155206.php 4.0 KB

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