1
0

RepairDavShares.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
  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 OC\Repair;
  25. use OCP\DB\Exception;
  26. use OCP\IConfig;
  27. use OCP\IDBConnection;
  28. use OCP\IGroupManager;
  29. use OCP\Migration\IOutput;
  30. use OCP\Migration\IRepairStep;
  31. use Psr\Log\LoggerInterface;
  32. use function strlen;
  33. use function substr;
  34. use function urldecode;
  35. use function urlencode;
  36. class RepairDavShares implements IRepairStep {
  37. protected const GROUP_PRINCIPAL_PREFIX = 'principals/groups/';
  38. /** @var IConfig */
  39. private $config;
  40. /** @var IDBConnection */
  41. private $dbc;
  42. /** @var IGroupManager */
  43. private $groupManager;
  44. /** @var LoggerInterface */
  45. private $logger;
  46. /** @var bool */
  47. private $hintInvalidShares = false;
  48. public function __construct(
  49. IConfig $config,
  50. IDBConnection $dbc,
  51. IGroupManager $groupManager,
  52. LoggerInterface $logger
  53. ) {
  54. $this->config = $config;
  55. $this->dbc = $dbc;
  56. $this->groupManager = $groupManager;
  57. $this->logger = $logger;
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. public function getName() {
  63. return 'Repair DAV shares';
  64. }
  65. protected function repairUnencodedGroupShares() {
  66. $qb = $this->dbc->getQueryBuilder();
  67. $qb->select(['id', 'principaluri'])
  68. ->from('dav_shares')
  69. ->where($qb->expr()->like('principaluri', $qb->createNamedParameter(self::GROUP_PRINCIPAL_PREFIX . '%')));
  70. $updateQuery = $this->dbc->getQueryBuilder();
  71. $updateQuery->update('dav_shares')
  72. ->set('principaluri', $updateQuery->createParameter('updatedPrincipalUri'))
  73. ->where($updateQuery->expr()->eq('id', $updateQuery->createParameter('shareId')));
  74. $statement = $qb->execute();
  75. while ($share = $statement->fetch()) {
  76. $gid = substr($share['principaluri'], strlen(self::GROUP_PRINCIPAL_PREFIX));
  77. $decodedGid = urldecode($gid);
  78. $encodedGid = urlencode($gid);
  79. if ($gid === $encodedGid
  80. || !$this->groupManager->groupExists($gid)
  81. || ($gid !== $decodedGid && $this->groupManager->groupExists($decodedGid))
  82. ) {
  83. $this->hintInvalidShares = $this->hintInvalidShares || $gid !== $encodedGid;
  84. continue;
  85. }
  86. // Repair when
  87. // + the group name needs encoding
  88. // + AND it is not encoded yet
  89. // + AND there are no ambivalent groups
  90. try {
  91. $fixedPrincipal = self::GROUP_PRINCIPAL_PREFIX . $encodedGid;
  92. $logParameters = [
  93. 'app' => 'core',
  94. 'id' => $share['id'],
  95. 'old' => $share['principaluri'],
  96. 'new' => $fixedPrincipal,
  97. ];
  98. $updateQuery
  99. ->setParameter('updatedPrincipalUri', $fixedPrincipal)
  100. ->setParameter('shareId', $share['id'])
  101. ->execute();
  102. $this->logger->info('Repaired principal for dav share {id} from {old} to {new}', $logParameters);
  103. } catch (Exception $e) {
  104. $logParameters['message'] = $e->getMessage();
  105. $logParameters['exception'] = $e;
  106. $this->logger->info('Could not repair principal for dav share {id} from {old} to {new}: {message}', $logParameters);
  107. }
  108. }
  109. return true;
  110. }
  111. /**
  112. * @inheritDoc
  113. */
  114. public function run(IOutput $output) {
  115. $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
  116. if (version_compare($versionFromBeforeUpdate, '20.0.8', '<')
  117. && $this->repairUnencodedGroupShares()
  118. ) {
  119. $output->info('Repaired DAV group shares');
  120. if ($this->hintInvalidShares) {
  121. $output->info('Invalid shares might be left in the database, running "occ dav:remove-invalid-shares" can remove them.');
  122. }
  123. }
  124. }
  125. }