RepairDavShares.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 bool */
  22. private $hintInvalidShares = false;
  23. public function __construct(
  24. private IConfig $config,
  25. private IDBConnection $dbc,
  26. private IGroupManager $groupManager,
  27. private LoggerInterface $logger,
  28. ) {
  29. }
  30. /**
  31. * @inheritDoc
  32. */
  33. public function getName() {
  34. return 'Repair DAV shares';
  35. }
  36. protected function repairUnencodedGroupShares() {
  37. $qb = $this->dbc->getQueryBuilder();
  38. $qb->select(['id', 'principaluri'])
  39. ->from('dav_shares')
  40. ->where($qb->expr()->like('principaluri', $qb->createNamedParameter(self::GROUP_PRINCIPAL_PREFIX . '%')));
  41. $updateQuery = $this->dbc->getQueryBuilder();
  42. $updateQuery->update('dav_shares')
  43. ->set('principaluri', $updateQuery->createParameter('updatedPrincipalUri'))
  44. ->where($updateQuery->expr()->eq('id', $updateQuery->createParameter('shareId')));
  45. $statement = $qb->execute();
  46. while ($share = $statement->fetch()) {
  47. $gid = substr($share['principaluri'], strlen(self::GROUP_PRINCIPAL_PREFIX));
  48. $decodedGid = urldecode($gid);
  49. $encodedGid = urlencode($gid);
  50. if ($gid === $encodedGid
  51. || !$this->groupManager->groupExists($gid)
  52. || ($gid !== $decodedGid && $this->groupManager->groupExists($decodedGid))
  53. ) {
  54. $this->hintInvalidShares = $this->hintInvalidShares || $gid !== $encodedGid;
  55. continue;
  56. }
  57. // Repair when
  58. // + the group name needs encoding
  59. // + AND it is not encoded yet
  60. // + AND there are no ambivalent groups
  61. try {
  62. $fixedPrincipal = self::GROUP_PRINCIPAL_PREFIX . $encodedGid;
  63. $logParameters = [
  64. 'app' => 'core',
  65. 'id' => $share['id'],
  66. 'old' => $share['principaluri'],
  67. 'new' => $fixedPrincipal,
  68. ];
  69. $updateQuery
  70. ->setParameter('updatedPrincipalUri', $fixedPrincipal)
  71. ->setParameter('shareId', $share['id'])
  72. ->execute();
  73. $this->logger->info('Repaired principal for dav share {id} from {old} to {new}', $logParameters);
  74. } catch (Exception $e) {
  75. $logParameters['message'] = $e->getMessage();
  76. $logParameters['exception'] = $e;
  77. $this->logger->info('Could not repair principal for dav share {id} from {old} to {new}: {message}', $logParameters);
  78. }
  79. }
  80. return true;
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. public function run(IOutput $output) {
  86. $versionFromBeforeUpdate = $this->config->getSystemValueString('version', '0.0.0');
  87. if (version_compare($versionFromBeforeUpdate, '20.0.8', '<')
  88. && $this->repairUnencodedGroupShares()
  89. ) {
  90. $output->info('Repaired DAV group shares');
  91. if ($this->hintInvalidShares) {
  92. $output->info('Invalid shares might be left in the database, running "occ dav:remove-invalid-shares" can remove them.');
  93. }
  94. }
  95. }
  96. }