Completed
Push — master ( ad597d...f2cae3 )
by Joas
07:56 queued 07:04
created

RemoveRootShares::countUsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 0
dl 10
loc 10
rs 9.4285
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
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
namespace OC\Repair;
23
24
use OCP\Files\IRootFolder;
25
use OCP\IDBConnection;
26
use OCP\IUser;
27
use OCP\IUserManager;
28
use OCP\Migration\IOutput;
29
use OCP\Migration\IRepairStep;
30
31
/**
32
 * Class RemoveRootShares
33
 *
34
 * @package OC\Repair
35
 */
36
class RemoveRootShares implements IRepairStep {
37
38
	/** @var IDBConnection */
39
	protected $connection;
40
41
	/** @var IUserManager */
42
	protected $userManager;
43
44
	/** @var IRootFolder */
45
	protected $rootFolder;
46
47
	/**
48
	 * RemoveRootShares constructor.
49
	 *
50
	 * @param IDBConnection $connection
51
	 * @param IUserManager $userManager
52
	 * @param IRootFolder $rootFolder
53
	 */
54
	public function __construct(IDBConnection $connection,
55
								IUserManager $userManager,
56
								IRootFolder $rootFolder) {
57
		$this->connection = $connection;
58
		$this->userManager = $userManager;
59
		$this->rootFolder = $rootFolder;
60
	}
61
62
	/**
63
	 * @return string
64
	 */
65
	public function getName() {
66
		return 'Remove shares of a users root folder';
67
	}
68
69
	/**
70
	 * @param IOutput $output
71
	 */
72
	public function run(IOutput $output) {
73
		if ($this->rootSharesExist()) {
74
			$this->removeRootShares($output);
75
		}
76
	}
77
78
	/**
79
	 * @param IOutput $output
80
	 */
81
	private function removeRootShares(IOutput $output) {
82
		$function = function(IUser $user) use ($output) {
83
			$userFolder = $this->rootFolder->getUserFolder($user->getUID());
84
			$fileId = $userFolder->getId();
85
86
			$qb = $this->connection->getQueryBuilder();
87
			$qb->delete('share')
88
				->where($qb->expr()->eq('file_source', $qb->createNamedParameter($fileId)))
89
				->andWhere($qb->expr()->orX(
90
					$qb->expr()->eq('item_type', $qb->expr()->literal('file')),
91
					$qb->expr()->eq('item_type', $qb->expr()->literal('folder'))
0 ignored issues
show
Unused Code introduced by
The call to IExpressionBuilder::orX() has too many arguments starting with $qb->expr()->eq('item_ty...r()->literal('folder')).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
92
				));
93
94
			$qb->execute();
95
96
			$output->advance();
97
		};
98
99
		$output->startProgress($this->userManager->countSeenUsers());
100
101
		$this->userManager->callForSeenUsers($function);
102
103
		$output->finishProgress();
104
	}
105
106
	/**
107
	 * Verify if this repair steps is required
108
	 * It *should* not be necessary in most cases and it can be very
109
	 * costly.
110
	 *
111
	 * @return bool
112
	 */
113
	private function rootSharesExist() {
114
		$qb = $this->connection->getQueryBuilder();
115
		$qb2 = $this->connection->getQueryBuilder();
116
117
		$qb->select('fileid')
118
			->from('filecache')
119
			->where($qb->expr()->eq('path', $qb->expr()->literal('files')));
120
121
		$qb2->select('id')
122
			->from('share')
123
			->where($qb2->expr()->in('file_source', $qb2->createFunction($qb->getSQL())))
124
			->andWhere($qb2->expr()->orX(
125
				$qb2->expr()->eq('item_type', $qb->expr()->literal('file')),
126
				$qb2->expr()->eq('item_type', $qb->expr()->literal('folder'))
0 ignored issues
show
Unused Code introduced by
The call to IExpressionBuilder::orX() has too many arguments starting with $qb2->expr()->eq('item_t...r()->literal('folder')).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
127
			))
128
			->setMaxResults(1);
129
130
		$cursor = $qb2->execute();
131
		$data = $cursor->fetch();
132
		$cursor->closeCursor();
133
134
		if ($data === false) {
135
			return false;
136
		}
137
138
		return true;
139
	}
140
}
141
142