RepairInvalidShares.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Repair;
  26. use OCP\Migration\IOutput;
  27. use OCP\Migration\IRepairStep;
  28. /**
  29. * Repairs shares with invalid data
  30. */
  31. class RepairInvalidShares implements IRepairStep {
  32. const CHUNK_SIZE = 200;
  33. /** @var \OCP\IConfig */
  34. protected $config;
  35. /** @var \OCP\IDBConnection */
  36. protected $connection;
  37. /**
  38. * @param \OCP\IConfig $config
  39. * @param \OCP\IDBConnection $connection
  40. */
  41. public function __construct($config, $connection) {
  42. $this->connection = $connection;
  43. $this->config = $config;
  44. }
  45. public function getName() {
  46. return 'Repair invalid shares';
  47. }
  48. /**
  49. * Past bugs would make it possible to set an expiration date on user shares even
  50. * though it is not supported. This functions removes the expiration date from such entries.
  51. */
  52. private function removeExpirationDateFromNonLinkShares(IOutput $out) {
  53. $builder = $this->connection->getQueryBuilder();
  54. $builder
  55. ->update('share')
  56. ->set('expiration', 'null')
  57. ->where($builder->expr()->isNotNull('expiration'))
  58. ->andWhere($builder->expr()->neq('share_type', $builder->expr()->literal(\OC\Share\Constants::SHARE_TYPE_LINK)));
  59. $updatedEntries = $builder->execute();
  60. if ($updatedEntries > 0) {
  61. $out->info('Removed invalid expiration date from ' . $updatedEntries . ' shares');
  62. }
  63. }
  64. /**
  65. * In the past link shares with public upload enabled were missing the delete permission.
  66. */
  67. private function addShareLinkDeletePermission(IOutput $out) {
  68. $oldPerms = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE;
  69. $newPerms = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
  70. $builder = $this->connection->getQueryBuilder();
  71. $builder
  72. ->update('share')
  73. ->set('permissions', $builder->expr()->literal($newPerms))
  74. ->where($builder->expr()->eq('share_type', $builder->expr()->literal(\OC\Share\Constants::SHARE_TYPE_LINK)))
  75. ->andWhere($builder->expr()->eq('permissions', $builder->expr()->literal($oldPerms)));
  76. $updatedEntries = $builder->execute();
  77. if ($updatedEntries > 0) {
  78. $out->info('Fixed link share permissions for ' . $updatedEntries . ' shares');
  79. }
  80. }
  81. /**
  82. * Remove shares where the parent share does not exist anymore
  83. */
  84. private function removeSharesNonExistingParent(IOutput $out) {
  85. $deletedEntries = 0;
  86. $query = $this->connection->getQueryBuilder();
  87. $query->select('s1.parent')
  88. ->from('share', 's1')
  89. ->where($query->expr()->isNotNull('s1.parent'))
  90. ->andWhere($query->expr()->isNull('s2.id'))
  91. ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id'))
  92. ->groupBy('s1.parent')
  93. ->setMaxResults(self::CHUNK_SIZE);
  94. $deleteQuery = $this->connection->getQueryBuilder();
  95. $deleteQuery->delete('share')
  96. ->where($deleteQuery->expr()->eq('parent', $deleteQuery->createParameter('parent')));
  97. $deletedInLastChunk = self::CHUNK_SIZE;
  98. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  99. $deletedInLastChunk = 0;
  100. $result = $query->execute();
  101. while ($row = $result->fetch()) {
  102. $deletedInLastChunk++;
  103. $deletedEntries += $deleteQuery->setParameter('parent', (int) $row['parent'])
  104. ->execute();
  105. }
  106. $result->closeCursor();
  107. }
  108. if ($deletedEntries) {
  109. $out->info('Removed ' . $deletedEntries . ' shares where the parent did not exist');
  110. }
  111. }
  112. public function run(IOutput $out) {
  113. $ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
  114. if (version_compare($ocVersionFromBeforeUpdate, '8.2.0.7', '<')) {
  115. // this situation was only possible before 8.2
  116. $this->removeExpirationDateFromNonLinkShares($out);
  117. }
  118. if (version_compare($ocVersionFromBeforeUpdate, '9.1.0.9', '<')) {
  119. // this situation was only possible before 9.1
  120. $this->addShareLinkDeletePermission($out);
  121. }
  122. $this->removeSharesNonExistingParent($out);
  123. }
  124. }