Version1120Date20210917155206.php 4.7 KB

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