RepairDavShares.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Repair;
  8. use OCP\DB\Exception;
  9. use OCP\IConfig;
  10. use OCP\IDBConnection;
  11. use OCP\IGroupManager;
  12. use OCP\Migration\IOutput;
  13. use OCP\Migration\IRepairStep;
  14. use Psr\Log\LoggerInterface;
  15. use function strlen;
  16. use function substr;
  17. use function urldecode;
  18. use function urlencode;
  19. class RepairDavShares implements IRepairStep {
  20. protected const GROUP_PRINCIPAL_PREFIX = 'principals/groups/';
  21. /** @var IConfig */
  22. private $config;
  23. /** @var IDBConnection */
  24. private $dbc;
  25. /** @var IGroupManager */
  26. private $groupManager;
  27. /** @var LoggerInterface */
  28. private $logger;
  29. /** @var bool */
  30. private $hintInvalidShares = false;
  31. public function __construct(
  32. IConfig $config,
  33. IDBConnection $dbc,
  34. IGroupManager $groupManager,
  35. LoggerInterface $logger,
  36. ) {
  37. $this->config = $config;
  38. $this->dbc = $dbc;
  39. $this->groupManager = $groupManager;
  40. $this->logger = $logger;
  41. }
  42. /**
  43. * @inheritDoc
  44. */
  45. public function getName() {
  46. return 'Repair DAV shares';
  47. }
  48. protected function repairUnencodedGroupShares() {
  49. $qb = $this->dbc->getQueryBuilder();
  50. $qb->select(['id', 'principaluri'])
  51. ->from('dav_shares')
  52. ->where($qb->expr()->like('principaluri', $qb->createNamedParameter(self::GROUP_PRINCIPAL_PREFIX . '%')));
  53. $updateQuery = $this->dbc->getQueryBuilder();
  54. $updateQuery->update('dav_shares')
  55. ->set('principaluri', $updateQuery->createParameter('updatedPrincipalUri'))
  56. ->where($updateQuery->expr()->eq('id', $updateQuery->createParameter('shareId')));
  57. $statement = $qb->execute();
  58. while ($share = $statement->fetch()) {
  59. $gid = substr($share['principaluri'], strlen(self::GROUP_PRINCIPAL_PREFIX));
  60. $decodedGid = urldecode($gid);
  61. $encodedGid = urlencode($gid);
  62. if ($gid === $encodedGid
  63. || !$this->groupManager->groupExists($gid)
  64. || ($gid !== $decodedGid && $this->groupManager->groupExists($decodedGid))
  65. ) {
  66. $this->hintInvalidShares = $this->hintInvalidShares || $gid !== $encodedGid;
  67. continue;
  68. }
  69. // Repair when
  70. // + the group name needs encoding
  71. // + AND it is not encoded yet
  72. // + AND there are no ambivalent groups
  73. try {
  74. $fixedPrincipal = self::GROUP_PRINCIPAL_PREFIX . $encodedGid;
  75. $logParameters = [
  76. 'app' => 'core',
  77. 'id' => $share['id'],
  78. 'old' => $share['principaluri'],
  79. 'new' => $fixedPrincipal,
  80. ];
  81. $updateQuery
  82. ->setParameter('updatedPrincipalUri', $fixedPrincipal)
  83. ->setParameter('shareId', $share['id'])
  84. ->execute();
  85. $this->logger->info('Repaired principal for dav share {id} from {old} to {new}', $logParameters);
  86. } catch (Exception $e) {
  87. $logParameters['message'] = $e->getMessage();
  88. $logParameters['exception'] = $e;
  89. $this->logger->info('Could not repair principal for dav share {id} from {old} to {new}: {message}', $logParameters);
  90. }
  91. }
  92. return true;
  93. }
  94. /**
  95. * @inheritDoc
  96. */
  97. public function run(IOutput $output) {
  98. $versionFromBeforeUpdate = $this->config->getSystemValueString('version', '0.0.0');
  99. if (version_compare($versionFromBeforeUpdate, '20.0.8', '<')
  100. && $this->repairUnencodedGroupShares()
  101. ) {
  102. $output->info('Repaired DAV group shares');
  103. if ($this->hintInvalidShares) {
  104. $output->info('Invalid shares might be left in the database, running "occ dav:remove-invalid-shares" can remove them.');
  105. }
  106. }
  107. }
  108. }