Completed
Push — master ( 0a312f...e14633 )
by Thomas
27:43 queued 11:29
created

RemoveInvalidShares::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\DAV\Repair;
23
24
use OCA\DAV\Connector\Sabre\Principal;
25
use OCP\IDBConnection;
26
use OCP\ILogger;
27
use OCP\Migration\IOutput;
28
use OCP\Migration\IRepairStep;
29
30
/**
31
 * Class RemoveInvalidShares - removes shared calendars and addressbook which
32
 * have no matching principal. Happened because of a bug in the calendar app.
33
 *
34
 * @package OCA\DAV\Repair
35
 */
36
class RemoveInvalidShares implements IRepairStep {
37
38
	/** @var IDBConnection */
39
	private $connection;
40
	/** @var Principal */
41
	private $principalBackend;
42
43
	/**
44
	 * RemoveInvalidShares constructor.
45
	 *
46
	 * @param IDBConnection $connection
47
	 * @param Principal $principalBackend
48
	 */
49
	public function __construct(IDBConnection $connection,
50
								Principal $principalBackend) {
51
		$this->connection = $connection;
52
		$this->principalBackend = $principalBackend;
53
	}
54
55
	/**
56
	 * Returns the step's name
57
	 *
58
	 * @return string
59
	 * @since 9.1.0
60
	 */
61
	public function getName() {
62
		return 'Remove invalid calendar and addressbook shares';
63
	}
64
65
	/**
66
	 * Run repair step.
67
	 * Must throw exception on error.
68
	 *
69
	 * @param IOutput $output
70
	 * @throws \Exception in case of failure
71
	 * @since 9.1.0
72
	 */
73
	public function run(IOutput $output) {
74
		$query = $this->connection->getQueryBuilder();
75
		$result = $query->selectDistinct('principaluri')
76
			->from('dav_shares')
77
			->execute();
78
79
		while($row = $result->fetch()) {
80
			$principaluri = $row['principaluri'];
81
			$p = $this->principalBackend->getPrincipalByPath($principaluri);
82
			if ($p === null) {
83
				$output->info(" ... for principal '$principaluri'");
84
				$this->deleteSharesForPrincipal($principaluri);
85
			}
86
		}
87
88
		$result->closeCursor();
89
	}
90
91
	/**
92
	 * @param string $principaluri
93
	 */
94
	private function deleteSharesForPrincipal($principaluri) {
95
		$delete = $this->connection->getQueryBuilder();
96
		$delete->delete('dav_shares')
97
			->where($delete->expr()->eq('principaluri', $delete->createNamedParameter($principaluri)));
98
		$delete->execute();
99
	}
100
}
101