Completed
Push — master ( 0c68cd...766ef1 )
by Maxence
02:02
created

ExternalFilesService::initExternalFilesForUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
/**
3
 * Files_FullTextSearch - Index the content of your files
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
/**
28
 * Created by PhpStorm.
29
 * User: maxence
30
 * Date: 12/13/17
31
 * Time: 4:11 PM
32
 */
33
34
namespace OCA\Files_FullTextSearch\Service;
35
36
37
use OCA\Files_FullTextSearch\Exceptions\FileIsNotIndexableException;
38
use OCA\Files_FullTextSearch\Exceptions\KnownFileSourceException;
39
use OCA\Files_FullTextSearch\Model\ExternalMount;
40
use OCA\Files_FullTextSearch\Model\FilesDocument;
41
use OCP\App;
42
use OCP\Files\IRootFolder;
43
use OCP\Files\Node;
44
use OCP\Files\NotFoundException;
45
use OCP\IUserManager;
46
use OCP\Share\IManager;
47
48
class ExternalFilesService {
49
50
51
	const DOCUMENT_SOURCE = 'external';
52
53
	/** @var IRootFolder */
54
	private $rootFolder;
55
56
	/** @var IUserManager */
57
	private $userManager;
58
59
	/** @var IManager */
60
	private $shareManager;
61
62
	/** @var ConfigService */
63
	private $configService;
64
65
	/** @var MiscService */
66
	private $miscService;
67
68
69
	/** @var ExternalMount[] */
70
	private $externalMounts = [];
71
72
73
	/**
74
	 * ExternalFilesService constructor.
75
	 *
76
	 * @param IRootFolder $rootFolder
77
	 * @param IUserManager $userManager
78
	 * @param IManager $shareManager
79
	 * @param ConfigService $configService
80
	 * @param MiscService $miscService
81
	 */
82 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
		IRootFolder $rootFolder, IUserManager $userManager, IManager $shareManager,
84
		ConfigService $configService, MiscService $miscService
85
	) {
86
		$this->rootFolder = $rootFolder;
87
		$this->userManager = $userManager;
88
		$this->shareManager = $shareManager;
89
90
		$this->configService = $configService;
91
		$this->miscService = $miscService;
92
	}
93
94
95
	/**
96
	 * @param string $userId
97
	 */
98
	public function initExternalFilesForUser($userId) {
99
		$this->externalMounts = [];
100
		if (!App::isEnabled('files_external')) {
101
			return;
102
		}
103
104
		if ($this->configService->getAppValue(ConfigService::FILES_EXTERNAL) !== '1') {
105
			return;
106
		}
107
108
		$this->externalMounts = $this->getExternalMountsForUser($userId);
109
	}
110
111
112
	/**
113
	 * @param Node $file
114
	 *
115
	 * @param string $source
116
	 *
117
	 * @throws FileIsNotIndexableException
118
	 * @throws NotFoundException
119
	 * @throws KnownFileSourceException
120
	 */
121
	public function getFileSource(Node $file, &$source) {
122
		if ($file->getStorage()
123
				 ->isLocal() === true) {
124
			return;
125
		}
126
127
		if (!$this->configService->optionIsSelected(ConfigService::FILES_EXTERNAL)) {
128
			throw new FileIsNotIndexableException();
129
		}
130
131
		$this->getExternalMount($file);
132
		$source = self::DOCUMENT_SOURCE;
133
134
		throw new KnownFileSourceException();
135
	}
136
137
138
	/**
139
	 * @param FilesDocument $document
140
	 * @param Node $file
141
	 * @param array $users
142
	 */
143
	public function getShareUsers(FilesDocument $document, Node $file, &$users) {
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $users is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
145
		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
146
			return;
147
		}
148
149
		$access = $document->getAccess();
150
		$result = $access->getUsers();
151
152
		if ($access->getOwnerId() !== '') {
153
			array_push($result, $access->getOwnerId());
154
		}
155
156
		// TODO: get users from groups & circles.
157
	}
158
159
160
	/**
161
	 * @param FilesDocument $document
162
	 * @param Node $file
163
	 */
164
	public function updateDocumentAccess(FilesDocument &$document, Node $file) {
165
166
		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
167
			return;
168
		}
169
170
		try {
171
			$mount = $this->getExternalMount($file);
172
		} catch (FileIsNotIndexableException $e) {
173
			return;
174
		}
175
176
		$access = $document->getAccess();
177
178
		if ($this->isMountFullGlobal($mount)) {
179
			$access->addUsers(['__all']);
180
		} else {
181
			$access->addUsers($mount->getUsers());
182
			$access->addGroups($mount->getGroups());
183
//		 	$access->addCircles($mount->getCircles());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
184
		}
185
186
		// twist 'n tweak.
187
		if (!$mount->isGlobal()) {
188
			$access->setOwnerId($mount->getUsers()[0]);
189
		}
190
191
		$document->setAccess($access);
192
	}
193
194
195
	/**
196
	 * @param ExternalMount $mount
197
	 *
198
	 * @return bool
199
	 */
200 View Code Duplication
	public function isMountFullGlobal(ExternalMount $mount) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
		if (sizeof($mount->getGroups()) > 0) {
202
			return false;
203
		}
204
205
		if (sizeof($mount->getUsers()) !== 1) {
206
			return false;
207
		}
208
209
		if ($mount->getUsers()[0] === 'all') {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $mount->getUsers()[0] === 'all';.
Loading history...
210
			return true;
211
		}
212
213
		return false;
214
	}
215
216
217
	/**
218
	 * @param Node $file
219
	 *
220
	 * @return ExternalMount
221
	 * @throws FileIsNotIndexableException
222
	 */
223 View Code Duplication
	private function getExternalMount(Node $file) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
225
		foreach ($this->externalMounts as $mount) {
226
			if (strpos($file->getPath(), $mount->getPath()) === 0) {
227
				return $mount;
228
			}
229
		}
230
231
		throw new FileIsNotIndexableException();
232
	}
233
234
235
	/**
236
	 * @param $userId
237
	 *
238
	 * @return ExternalMount[]
239
	 */
240
	private function getExternalMountsForUser($userId) {
241
242
		$externalMounts = [];
243
244
		// TODO: deprecated - use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages()
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 107 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
245
		$mounts = \OC_Mount_Config::getAbsoluteMountPoints($userId);
246
		foreach ($mounts as $mountPoint => $mount) {
247
			$externalMount = new ExternalMount();
248
			$externalMount->setId($mount['id'])
249
						  ->setPath($mountPoint)
250
						  ->setGroups($mount['applicable']['groups'])
251
						  ->setUsers($mount['applicable']['users'])
252
						  ->setGlobal((!$mount['personal']));
253
			$externalMounts[] = $externalMount;
254
		}
255
256
		return $externalMounts;
257
	}
258
259
}