Completed
Push — stable13 ( 81b02e...968127 )
by Morris
11:09 queued 10s
created

removeConfidentialUncensoredEventActivity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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