RemoveLinkShares.php 6.9 KB

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