RemoveRootShares.php 3.4 KB

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