Version1190Date20230706134108.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  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\Types;
  28. use OCP\IDBConnection;
  29. use OCP\Migration\IOutput;
  30. use OCP\Migration\SimpleMigrationStep;
  31. class Version1190Date20230706134108 extends SimpleMigrationStep {
  32. public function __construct(
  33. private IDBConnection $dbc,
  34. ) {
  35. }
  36. public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
  37. }
  38. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  39. /** @var ISchemaWrapper $schema */
  40. $schema = $schemaClosure();
  41. if (!$schema->hasTable('ldap_group_membership')) {
  42. $table = $schema->createTable('ldap_group_membership');
  43. $table->addColumn('id', Types::BIGINT, [
  44. 'autoincrement' => true,
  45. 'notnull' => true,
  46. ]);
  47. $table->addColumn('groupid', Types::STRING, [
  48. 'notnull' => true,
  49. 'length' => 255,
  50. 'default' => '',
  51. ]);
  52. $table->addColumn('userid', Types::STRING, [
  53. 'notnull' => true,
  54. 'length' => 64,
  55. 'default' => '',
  56. ]);
  57. $table->setPrimaryKey(['id']);
  58. $table->addUniqueIndex(['groupid', 'userid'], 'user_ldap_membership_unique');
  59. return $schema;
  60. } else {
  61. return null;
  62. }
  63. }
  64. public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
  65. /** @var ISchemaWrapper $schema */
  66. $schema = $schemaClosure();
  67. if (!$schema->hasTable('ldap_group_members')) {
  68. // Old table does not exist
  69. return;
  70. }
  71. $output->startProgress();
  72. $this->copyGroupMembershipData();
  73. $output->finishProgress();
  74. }
  75. protected function copyGroupMembershipData(): void {
  76. $insert = $this->dbc->getQueryBuilder();
  77. $insert->insert('ldap_group_membership')
  78. ->values([
  79. 'userid' => $insert->createParameter('userid'),
  80. 'groupid' => $insert->createParameter('groupid'),
  81. ]);
  82. $query = $this->dbc->getQueryBuilder();
  83. $query->select('*')
  84. ->from('ldap_group_members');
  85. $result = $query->executeQuery();
  86. while ($row = $result->fetch()) {
  87. $knownUsers = unserialize($row['owncloudusers']);
  88. if (!is_array($knownUsers)) {
  89. /* Unserialize failed or data was incorrect in database, ignore */
  90. continue;
  91. }
  92. $knownUsers = array_unique($knownUsers);
  93. foreach ($knownUsers as $knownUser) {
  94. try {
  95. $insert
  96. ->setParameter('groupid', $row['owncloudname'])
  97. ->setParameter('userid', $knownUser)
  98. ;
  99. $insert->executeStatement();
  100. } catch (\OCP\DB\Exception $e) {
  101. /*
  102. * If it fails on unique constaint violation it may just be left over value from previous half-migration
  103. * If it fails on something else, ignore as well, data will be filled by background job later anyway
  104. */
  105. }
  106. }
  107. }
  108. $result->closeCursor();
  109. }
  110. }