You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoveLinkShares.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Repair;
  25. use Doctrine\DBAL\Driver\Statement;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\IConfig;
  28. use OCP\IDBConnection;
  29. use OCP\IGroupManager;
  30. use OCP\Migration\IOutput;
  31. use OCP\Migration\IRepairStep;
  32. use OCP\Notification\IManager;
  33. class RemoveLinkShares implements IRepairStep {
  34. /** @var IDBConnection */
  35. private $connection;
  36. /** @var IConfig */
  37. private $config;
  38. /** @var string[] */
  39. private $userToNotify = [];
  40. /** @var IGroupManager */
  41. private $groupManager;
  42. /** @var IManager */
  43. private $notificationManager;
  44. /** @var ITimeFactory */
  45. private $timeFactory;
  46. public function __construct(IDBConnection $connection,
  47. IConfig $config,
  48. IGroupManager $groupManager,
  49. IManager $notificationManager,
  50. ITimeFactory $timeFactory) {
  51. $this->connection = $connection;
  52. $this->config = $config;
  53. $this->groupManager = $groupManager;
  54. $this->notificationManager = $notificationManager;
  55. $this->timeFactory = $timeFactory;
  56. }
  57. public function getName(): string {
  58. return 'Remove potentially over exposing share links';
  59. }
  60. private function shouldRun(): bool {
  61. $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
  62. if (version_compare($versionFromBeforeUpdate, '14.0.11', '<')) {
  63. return true;
  64. }
  65. if (version_compare($versionFromBeforeUpdate, '15.0.8', '<')) {
  66. return true;
  67. }
  68. if (version_compare($versionFromBeforeUpdate, '16.0.0', '<=')) {
  69. return true;
  70. }
  71. return false;
  72. }
  73. /**
  74. * Delete the share
  75. *
  76. * @param int $id
  77. */
  78. private function deleteShare(int $id) {
  79. $qb = $this->connection->getQueryBuilder();
  80. $qb->delete('share')
  81. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  82. $qb->execute();
  83. }
  84. /**
  85. * Get the total of affected shares
  86. *
  87. * @return int
  88. */
  89. private function getTotal(): int {
  90. $sql = 'SELECT COUNT(*) AS `total`
  91. FROM `*PREFIX*share`
  92. WHERE `id` IN (
  93. SELECT `s1`.`id`
  94. FROM (
  95. SELECT *
  96. FROM `*PREFIX*share`
  97. WHERE `parent` IS NOT NULL
  98. AND `share_type` = 3
  99. ) AS s1
  100. JOIN `*PREFIX*share` AS s2
  101. ON `s1`.`parent` = `s2`.`id`
  102. WHERE (`s2`.`share_type` = 1 OR `s2`.`share_type` = 2)
  103. AND `s1`.`item_source` = `s2`.`item_source`
  104. )';
  105. $cursor = $this->connection->executeQuery($sql);
  106. $data = $cursor->fetchAll();
  107. $total = (int)$data[0]['total'];
  108. $cursor->closeCursor();
  109. return $total;
  110. }
  111. /**
  112. * Get the cursor to fetch all the shares
  113. *
  114. * @return \Doctrine\DBAL\Driver\Statement
  115. */
  116. private function getShares(): Statement {
  117. $sql = 'SELECT `s1`.`id`, `s1`.`uid_owner`, `s1`.`uid_initiator`
  118. FROM (
  119. SELECT *
  120. FROM `*PREFIX*share`
  121. WHERE `parent` IS NOT NULL
  122. AND `share_type` = 3
  123. ) AS s1
  124. JOIN `*PREFIX*share` AS s2
  125. ON `s1`.`parent` = `s2`.`id`
  126. WHERE (`s2`.`share_type` = 1 OR `s2`.`share_type` = 2)
  127. AND `s1`.`item_source` = `s2`.`item_source`';
  128. $cursor = $this->connection->executeQuery($sql);
  129. return $cursor;
  130. }
  131. /**
  132. * Process a single share
  133. *
  134. * @param array $data
  135. */
  136. private function processShare(array $data) {
  137. $id = $data['id'];
  138. $this->addToNotify($data['uid_owner']);
  139. $this->addToNotify($data['uid_initiator']);
  140. $this->deleteShare((int)$id);
  141. }
  142. /**
  143. * Update list of users to notify
  144. *
  145. * @param string $uid
  146. */
  147. private function addToNotify(string $uid) {
  148. if (!isset($this->userToNotify[$uid])) {
  149. $this->userToNotify[$uid] = true;
  150. }
  151. }
  152. /**
  153. * Send all notifications
  154. */
  155. private function sendNotification() {
  156. $time = $this->timeFactory->getDateTime();
  157. $notification = $this->notificationManager->createNotification();
  158. $notification->setApp('core')
  159. ->setDateTime($time)
  160. ->setObject('repair', 'exposing_links')
  161. ->setSubject('repair_exposing_links', []);
  162. $users = array_keys($this->userToNotify);
  163. foreach ($users as $user) {
  164. $notification->setUser($user);
  165. $this->notificationManager->notify($notification);
  166. }
  167. }
  168. private function repair(IOutput $output) {
  169. $total = $this->getTotal();
  170. $output->startProgress($total);
  171. $shareCursor = $this->getShares();
  172. while($data = $shareCursor->fetch()) {
  173. $this->processShare($data);
  174. $output->advance();
  175. }
  176. $output->finishProgress();
  177. $shareCursor->closeCursor();
  178. // Notifiy all admins
  179. $adminGroup = $this->groupManager->get('admin');
  180. $adminUsers = $adminGroup->getUsers();
  181. foreach ($adminUsers as $user) {
  182. $this->addToNotify($user->getUID());
  183. }
  184. $output->info('Sending notifications to admins and affected users');
  185. $this->sendNotification();
  186. }
  187. public function run(IOutput $output) {
  188. if ($this->shouldRun()) {
  189. $output->info('Removing potentially over exposing link shares');
  190. $this->repair($output);
  191. $output->info('Removed potentially over exposing link shares');
  192. } else {
  193. $output->info('No need to remove link shares.');
  194. }
  195. }
  196. }