Version1190Date20230706134108.php 2.9 KB

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