summaryrefslogtreecommitdiffstats
path: root/lib/private/Repair/RemoveLinkShares.php
blob: 32cbd1f73f85f578806450499c492bea2542da94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php

declare(strict_types=1);

/**
 * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author Daniel Kesselberg <mail@danielkesselberg.de>
 * @author Joas Schilling <coding@schilljs.com>
 * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OC\Repair;

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OCP\Notification\IManager;

class RemoveLinkShares implements IRepairStep {
	/** @var IDBConnection */
	private $connection;
	/** @var IConfig */
	private $config;
	/** @var string[] */
	private $userToNotify = [];
	/** @var IGroupManager */
	private $groupManager;
	/** @var IManager */
	private $notificationManager;
	/** @var ITimeFactory */
	private $timeFactory;

	public function __construct(IDBConnection $connection,
								IConfig $config,
								IGroupManager $groupManager,
								IManager $notificationManager,
								ITimeFactory $timeFactory) {
		$this->connection = $connection;
		$this->config = $config;
		$this->groupManager = $groupManager;
		$this->notificationManager = $notificationManager;
		$this->timeFactory = $timeFactory;
	}


	public function getName(): string {
		return 'Remove potentially over exposing share links';
	}

	private function shouldRun(): bool {
		$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');

		if (version_compare($versionFromBeforeUpdate, '14.0.11', '<')) {
			return true;
		}
		if (version_compare($versionFromBeforeUpdate, '15.0.8', '<')) {
			return true;
		}
		if (version_compare($versionFromBeforeUpdate, '16.0.0', '<=')) {
			return true;
		}

		return false;
	}

	/**
	 * Delete the share
	 *
	 * @param int $id
	 */
	private function deleteShare(int $id): void {
		$qb = $this->connection->getQueryBuilder();
		$qb->delete('share')
			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
		$qb->execute();
	}

	/**
	 * Get the total of affected shares
	 *
	 * @return int
	 */
	private function getTotal(): int {
		$subSubQuery = $this->connection->getQueryBuilder();
		$subSubQuery->select('*')
			->from('share')
			->where($subSubQuery->expr()->isNotNull('parent'))
			->andWhere($subSubQuery->expr()->eq('share_type', $subSubQuery->expr()->literal(3, IQueryBuilder::PARAM_INT)));

		$subQuery = $this->connection->getQueryBuilder();
		$subQuery->select('s1.id')
			->from($subQuery->createFunction('(' . $subSubQuery->getSQL() . ')'), 's1')
			->join(
				's1', 'share', 's2',
				$subQuery->expr()->eq('s1.parent', 's2.id')
			)
			->where($subQuery->expr()->orX(
				$subQuery->expr()->eq('s2.share_type', $subQuery->expr()->literal(1, IQueryBuilder::PARAM_INT)),
				$subQuery->expr()->eq('s2.share_type', $subQuery->expr()->literal(2, IQueryBuilder::PARAM_INT))
			))
			->andWhere($subQuery->expr()->eq('s1.item_source', 's2.item_source'));

		$query = $this->connection->getQueryBuilder();
		$query->select($query->func()->count('*', 'total'))
			->from('share')
			->where($query->expr()->in('id', $query->createFunction('(' . $subQuery->getSQL() . ')')));

		$result = $query->execute();
		$data = $result->fetch();
		$result->closeCursor();

		return (int) $data['total'];
	}

	/**
	 * Get the cursor to fetch all the shares
	 */
	private function getShares(): IResult {
		$subQuery = $this->connection->getQueryBuilder();
		$subQuery->select('*')
			->from('share')
			->where($subQuery->expr()->isNotNull('parent'))
			->andWhere($subQuery->expr()->eq('share_type', $subQuery->expr()->literal(3, IQueryBuilder::PARAM_INT)));

		$query = $this->connection->getQueryBuilder();
		$query->select('s1.id', 's1.uid_owner', 's1.uid_initiator')
			->from($query->createFunction('(' . $subQuery->getSQL() . ')'), 's1')
			->join(
				's1', 'share', 's2',
				$query->expr()->eq('s1.parent', 's2.id')
			)
			->where($query->expr()->orX(
				$query->expr()->eq('s2.share_type', $query->expr()->literal(1, IQueryBuilder::PARAM_INT)),
				$query->expr()->eq('s2.share_type', $query->expr()->literal(2, IQueryBuilder::PARAM_INT))
			))
			->andWhere($query->expr()->eq('s1.item_source', 's2.item_source'));
		/** @var IResult $result */
		$result = $query->execute();
		return $result;
	}

	/**
	 * Process a single share
	 *
	 * @param array $data
	 */
	private function processShare(array $data): void {
		$id = $data['id'];

		$this->addToNotify($data['uid_owner']);
		$this->addToNotify($data['uid_initiator']);

		$this->deleteShare((int)$id);
	}

	/**
	 * Update list of users to notify
	 *
	 * @param string $uid
	 */
	private function addToNotify(string $uid): void {
		if (!isset($this->userToNotify[$uid])) {
			$this->userToNotify[$uid] = true;
		}
	}

	/**
	 * Send all notifications
	 */
	private function sendNotification(): void {
		$time = $this->timeFactory->getDateTime();

		$notification = $this->notificationManager->createNotification();
		$notification->setApp('core')
			->setDateTime($time)
			->setObject('repair', 'exposing_links')
			->setSubject('repair_exposing_links');

		$users = array_keys($this->userToNotify);
		foreach ($users as $user) {
			$notification->setUser((string) $user);
			$this->notificationManager->notify($notification);
		}
	}

	private function repair(IOutput $output, int $total): void {
		$output->startProgress($total);

		$shareResult = $this->getShares();
		while ($data = $shareResult->fetch()) {
			$this->processShare($data);
			$output->advance();
		}
		$output->finishProgress();
		$shareResult->closeCursor();

		// Notifiy all admins
		$adminGroup = $this->groupManager->get('admin');
		$adminUsers = $adminGroup->getUsers();
		foreach ($adminUsers as $user) {
			$this->addToNotify($user->getUID());
		}

		$output->info('Sending notifications to admins and affected users');
		$this->sendNotification();
	}

	public function run(IOutput $output): void {
		if ($this->shouldRun() === false || ($total = $this->getTotal()) === 0) {
			$output->info('No need to remove link shares.');
			return;
		}

		$output->info('Removing potentially over exposing link shares');
		$this->repair($output, $total);
		$output->info('Removed potentially over exposing link shares');
	}
}