Version1120Date20210917155206.php 4.7 KB

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