1
0

MigrateOauthTables.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @copyright 2021 Louis Chemineau <louis@chmn.me>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Repair\Owncloud;
  22. use OCP\Migration\IOutput;
  23. use OCP\Migration\IRepairStep;
  24. use OC\DB\Connection;
  25. use OC\DB\SchemaWrapper;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. class MigrateOauthTables implements IRepairStep {
  28. /** @var Connection */
  29. protected $db;
  30. /**
  31. * @param Connection $db
  32. */
  33. public function __construct(Connection $db) {
  34. $this->db = $db;
  35. }
  36. /**
  37. * @return string
  38. */
  39. public function getName() {
  40. return 'Migrate oauth2_clients table to nextcloud schema';
  41. }
  42. public function run(IOutput $output) {
  43. $schema = new SchemaWrapper($this->db);
  44. if (!$schema->hasTable('oauth2_clients')) {
  45. $output->info("oauth2_clients table does not exist.");
  46. return;
  47. }
  48. $output->info("Update the oauth2_access_tokens table schema.");
  49. $schema = new SchemaWrapper($this->db);
  50. $table = $schema->getTable('oauth2_access_tokens');
  51. if (!$table->hasColumn('hashed_code')) {
  52. $table->addColumn('hashed_code', 'string', [
  53. 'notnull' => true,
  54. 'length' => 128,
  55. ]);
  56. }
  57. if (!$table->hasColumn('encrypted_token')) {
  58. $table->addColumn('encrypted_token', 'string', [
  59. 'notnull' => true,
  60. 'length' => 786,
  61. ]);
  62. }
  63. if (!$table->hasIndex('oauth2_access_hash_idx')) {
  64. $table->addUniqueIndex(['hashed_code'], 'oauth2_access_hash_idx');
  65. }
  66. if (!$table->hasIndex('oauth2_access_client_id_idx')) {
  67. $table->addIndex(['client_id'], 'oauth2_access_client_id_idx');
  68. }
  69. $output->info("Update the oauth2_clients table schema.");
  70. $schema = new SchemaWrapper($this->db);
  71. $table = $schema->getTable('oauth2_clients');
  72. if ($table->getColumn('name')->getLength() !== 64) {
  73. $table->getColumn('name')->setLength(64);
  74. }
  75. if ($table->hasColumn('allow_subdomains')) {
  76. $table->dropColumn('allow_subdomains');
  77. }
  78. if (!$schema->getTable('oauth2_clients')->hasColumn('client_identifier')) {
  79. $table->addColumn('client_identifier', 'string', [
  80. 'notnull' => true,
  81. 'length' => 64,
  82. 'default' => ''
  83. ]);
  84. $table->addIndex(['client_identifier'], 'oauth2_client_id_idx');
  85. }
  86. $this->db->migrateToSchema($schema->getWrappedSchema());
  87. if ($schema->getTable('oauth2_clients')->hasColumn('identifier')) {
  88. $output->info("Move identifier column's data to the new client_identifier column.");
  89. // 1. Fetch all [id, identifier] couple.
  90. $selectQuery = $this->db->getQueryBuilder();
  91. $selectQuery->select('id', 'identifier')->from('oauth2_clients');
  92. $result = $selectQuery->executeQuery();
  93. $identifiers = $result->fetchAll();
  94. $result->closeCursor();
  95. // 2. Insert them into the client_identifier column.
  96. foreach ($identifiers as ["id" => $id, "identifier" => $clientIdentifier]) {
  97. $insertQuery = $this->db->getQueryBuilder();
  98. $insertQuery->update('oauth2_clients')
  99. ->set('client_identifier', $insertQuery->createNamedParameter($clientIdentifier, IQueryBuilder::PARAM_STR))
  100. ->where($insertQuery->expr()->eq('id', $insertQuery->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
  101. ->executeStatement();
  102. }
  103. $output->info("Drop the identifier column.");
  104. $schema = new SchemaWrapper($this->db);
  105. $table = $schema->getTable('oauth2_clients');
  106. $table->dropColumn('identifier');
  107. $this->db->migrateToSchema($schema->getWrappedSchema());
  108. }
  109. }
  110. }