RemoveRootShares.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Repair;
  24. use OCP\Files\IRootFolder;
  25. use OCP\IDBConnection;
  26. use OCP\IUser;
  27. use OCP\IUserManager;
  28. use OCP\Migration\IOutput;
  29. use OCP\Migration\IRepairStep;
  30. /**
  31. * Class RemoveRootShares
  32. *
  33. * @package OC\Repair
  34. */
  35. class RemoveRootShares implements IRepairStep {
  36. /** @var IDBConnection */
  37. protected $connection;
  38. /** @var IUserManager */
  39. protected $userManager;
  40. /** @var IRootFolder */
  41. protected $rootFolder;
  42. /**
  43. * RemoveRootShares constructor.
  44. *
  45. * @param IDBConnection $connection
  46. * @param IUserManager $userManager
  47. * @param IRootFolder $rootFolder
  48. */
  49. public function __construct(IDBConnection $connection,
  50. IUserManager $userManager,
  51. IRootFolder $rootFolder) {
  52. $this->connection = $connection;
  53. $this->userManager = $userManager;
  54. $this->rootFolder = $rootFolder;
  55. }
  56. /**
  57. * @return string
  58. */
  59. public function getName() {
  60. return 'Remove shares of a users root folder';
  61. }
  62. /**
  63. * @param IOutput $output
  64. */
  65. public function run(IOutput $output) {
  66. if ($this->rootSharesExist()) {
  67. $this->removeRootShares($output);
  68. }
  69. }
  70. /**
  71. * @param IOutput $output
  72. */
  73. private function removeRootShares(IOutput $output) {
  74. $function = function(IUser $user) use ($output) {
  75. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  76. $fileId = $userFolder->getId();
  77. $qb = $this->connection->getQueryBuilder();
  78. $qb->delete('share')
  79. ->where($qb->expr()->eq('file_source', $qb->createNamedParameter($fileId)))
  80. ->andWhere($qb->expr()->orX(
  81. $qb->expr()->eq('item_type', $qb->expr()->literal('file')),
  82. $qb->expr()->eq('item_type', $qb->expr()->literal('folder'))
  83. ));
  84. $qb->execute();
  85. $output->advance();
  86. };
  87. $output->startProgress($this->userManager->countSeenUsers());
  88. $this->userManager->callForSeenUsers($function);
  89. $output->finishProgress();
  90. }
  91. /**
  92. * Verify if this repair steps is required
  93. * It *should* not be necessary in most cases and it can be very
  94. * costly.
  95. *
  96. * @return bool
  97. */
  98. private function rootSharesExist() {
  99. $qb = $this->connection->getQueryBuilder();
  100. $qb2 = $this->connection->getQueryBuilder();
  101. $qb->select('fileid')
  102. ->from('filecache')
  103. ->where($qb->expr()->eq('path', $qb->expr()->literal('files')));
  104. $qb2->select('id')
  105. ->from('share')
  106. ->where($qb2->expr()->in('file_source', $qb2->createFunction($qb->getSQL())))
  107. ->andWhere($qb2->expr()->orX(
  108. $qb2->expr()->eq('item_type', $qb->expr()->literal('file')),
  109. $qb2->expr()->eq('item_type', $qb->expr()->literal('folder'))
  110. ))
  111. ->setMaxResults(1);
  112. $cursor = $qb2->execute();
  113. $data = $cursor->fetch();
  114. $cursor->closeCursor();
  115. if ($data === false) {
  116. return false;
  117. }
  118. return true;
  119. }
  120. }