Completed
Push — master ( 188b96...0947dd )
by Morris
43s queued 10s
created

RemoveInvalidShares::configure()   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 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @author Thomas Müller <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2018, ownCloud GmbH
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
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, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\DAV\Command;
24
25
use OCA\DAV\Connector\Sabre\Principal;
26
use OCP\IDBConnection;
27
use Symfony\Component\Console\Command\Command;
28
use Symfony\Component\Console\Input\InputInterface;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
/**
32
 * Class RemoveInvalidShares - removes shared calendars and addressbook which
33
 * have no matching principal. Happened because of a bug in the calendar app.
34
 */
35
class RemoveInvalidShares extends Command {
36
37
	/** @var IDBConnection */
38
	private $connection;
39
	/** @var Principal */
40
	private $principalBackend;
41
42
	public function __construct(IDBConnection $connection,
43
								Principal $principalBackend) {
44
		parent::__construct();
45
46
		$this->connection = $connection;
47
		$this->principalBackend = $principalBackend;
48
	}
49
50
	protected function configure() {
51
		$this
52
			->setName('dav:remove-invalid-shares')
53
			->setDescription('Remove invalid dav shares');
54
	}
55
56
	protected function execute(InputInterface $input, OutputInterface $output) {
57
		$query = $this->connection->getQueryBuilder();
58
		$result = $query->selectDistinct('principaluri')
59
			->from('dav_shares')
60
			->execute();
61
62
		while($row = $result->fetch()) {
63
			$principaluri = $row['principaluri'];
64
			$p = $this->principalBackend->getPrincipalByPath($principaluri);
65
			if ($p === null) {
66
				$this->deleteSharesForPrincipal($principaluri);
67
			}
68
		}
69
70
		$result->closeCursor();
71
	}
72
73
	/**
74
	 * @param string $principaluri
75
	 */
76
	private function deleteSharesForPrincipal($principaluri) {
77
		$delete = $this->connection->getQueryBuilder();
78
		$delete->delete('dav_shares')
79
			->where($delete->expr()->eq('principaluri', $delete->createNamedParameter($principaluri)));
80
		$delete->execute();
81
	}
82
}
83