Passed
Push — master ( 1c5e43...8593ed )
by Roeland
17:02 queued 05:14
created

RemoveClassifiedEventActivity::getPrincipal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019 Joas Schilling <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
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
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\DAV\Migration;
24
25
use OCA\DAV\CalDAV\CalDavBackend;
26
use OCP\IDBConnection;
27
use OCP\Migration\IOutput;
28
use OCP\Migration\IRepairStep;
29
30
class RemoveClassifiedEventActivity implements IRepairStep {
31
32
	/** @var IDBConnection */
33
	private $connection;
34
35
	public function __construct(IDBConnection $connection) {
36
		$this->connection = $connection;
37
	}
38
39
	/**
40
	 * @inheritdoc
41
	 */
42
	public function getName() {
43
		return 'Remove activity entries of private events';
44
	}
45
46
	/**
47
	 * @inheritdoc
48
	 */
49
	public function run(IOutput $output) {
50
		if (!$this->connection->tableExists('activity')) {
51
			return;
52
		}
53
54
		$deletedEvents = $this->removePrivateEventActivity();
55
		$deletedEvents += $this->removeConfidentialUncensoredEventActivity();
56
57
		$output->info("Removed $deletedEvents activity entries");
58
	}
59
60
	protected function removePrivateEventActivity(): int {
61
		$deletedEvents = 0;
62
63
		$delete = $this->connection->getQueryBuilder();
64
		$delete->delete('activity')
65
			->where($delete->expr()->neq('affecteduser', $delete->createParameter('owner')))
66
			->andWhere($delete->expr()->eq('object_type', $delete->createParameter('type')))
67
			->andWhere($delete->expr()->eq('object_id', $delete->createParameter('calendar_id')))
68
			->andWhere($delete->expr()->like('subjectparams', $delete->createParameter('event_uid')));
69
70
		$query = $this->connection->getQueryBuilder();
71
		$query->select('c.principaluri', 'o.calendarid', 'o.uid')
72
			->from('calendarobjects', 'o')
73
			->leftJoin('o', 'calendars', 'c', $query->expr()->eq('c.id', 'o.calendarid'))
74
			->where($query->expr()->eq('o.classification', $query->createNamedParameter(CalDavBackend::CLASSIFICATION_PRIVATE)));
75
		$result = $query->execute();
76
77
		while ($row = $result->fetch()) {
78
			$delete->setParameter('owner', $this->getPrincipal($row['principaluri']))
79
				->setParameter('type', 'calendar')
80
				->setParameter('calendar_id', $row['calendarid'])
81
				->setParameter('event_uid', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '"') . '%');
82
			$deletedEvents += $delete->execute();
83
		}
84
		$result->closeCursor();
85
86
		return $deletedEvents;
87
	}
88
89
	protected function removeConfidentialUncensoredEventActivity(): int {
90
		$deletedEvents = 0;
91
92
		$delete = $this->connection->getQueryBuilder();
93
		$delete->delete('activity')
94
			->where($delete->expr()->neq('affecteduser', $delete->createParameter('owner')))
95
			->andWhere($delete->expr()->eq('object_type', $delete->createParameter('type')))
96
			->andWhere($delete->expr()->eq('object_id', $delete->createParameter('calendar_id')))
97
			->andWhere($delete->expr()->like('subjectparams', $delete->createParameter('event_uid')))
98
			->andWhere($delete->expr()->notLike('subjectparams', $delete->createParameter('filtered_name')));
99
100
		$query = $this->connection->getQueryBuilder();
101
		$query->select('c.principaluri', 'o.calendarid', 'o.uid')
102
			->from('calendarobjects', 'o')
103
			->leftJoin('o', 'calendars', 'c', $query->expr()->eq('c.id', 'o.calendarid'))
104
			->where($query->expr()->eq('o.classification', $query->createNamedParameter(CalDavBackend::CLASSIFICATION_CONFIDENTIAL)));
105
		$result = $query->execute();
106
107
		while ($row = $result->fetch()) {
108
			$delete->setParameter('owner', $this->getPrincipal($row['principaluri']))
109
				->setParameter('type', 'calendar')
110
				->setParameter('calendar_id', $row['calendarid'])
111
				->setParameter('event_uid', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '"') . '%')
112
				->setParameter('filtered_name', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '","name":"Busy"') . '%');
113
			$deletedEvents += $delete->execute();
114
		}
115
		$result->closeCursor();
116
117
		return $deletedEvents;
118
	}
119
120
	protected function getPrincipal(string $principalUri): string {
121
		$uri = explode('/', $principalUri);
122
		return $uri[2];
123
	}
124
}
125