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

LocalFilesService::getShareUsers()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 35
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
rs 8.439
cc 5
eloc 8
nc 4
nop 3
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
namespace OCA\Files_FullTextSearch\Service;
28
29
30
use OC\Share\Constants;
31
use OCA\Files_FullTextSearch\Db\SharesRequest;
32
use OCA\Files_FullTextSearch\Model\FilesDocument;
33
use OCA\Files_FullTextSearch\Model\FileShares;
34
use OCA\FullTextSearch\Model\DocumentAccess;
35
use OCP\Files\InvalidPathException;
36
use OCP\Files\IRootFolder;
37
use OCP\Files\Node;
38
use OCP\Files\NotFoundException;
39
use OCP\IUserManager;
40
use OCP\Share\IManager;
41
42
class LocalFilesService {
43
44
45
	const DOCUMENT_SOURCE = 'local';
46
47
	/** @var IRootFolder */
48
	private $rootFolder;
49
50
	/** @var IUserManager */
51
	private $userManager;
52
53
	/** @var IManager */
54
	private $shareManager;
55
56
	/** @var SharesRequest */
57
	private $sharesRequest;
58
59
	/** @var ConfigService */
60
	private $configService;
61
62
	/** @var MiscService */
63
	private $miscService;
64
65
//
66
//	/** @var ExternalMount[] */
67
//	private $externalMounts = [];
68
69
70
	/**
71
	 * ExternalFilesService constructor.
72
	 *
73
	 * @param IRootFolder $rootFolder
74
	 * @param IUserManager $userManager
75
	 * @param IManager $shareManager
76
	 * @param SharesRequest $sharesRequest
77
	 * @param ConfigService $configService
78
	 * @param MiscService $miscService
79
	 */
80 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...
81
		IRootFolder $rootFolder, IUserManager $userManager, IManager $shareManager,
82
		SharesRequest $sharesRequest, ConfigService $configService, MiscService $miscService
83
	) {
84
		$this->rootFolder = $rootFolder;
85
		$this->userManager = $userManager;
86
		$this->shareManager = $shareManager;
87
88
		$this->sharesRequest = $sharesRequest;
89
		$this->configService = $configService;
90
		$this->miscService = $miscService;
91
	}
92
93
94
	/**
95
	 * @param Node $file
96
	 * @param string $source
97
	 */
98
	public function getFileSource(Node $file, &$source) {
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...
99
		$source = self::DOCUMENT_SOURCE;
100
	}
101
102
103
	/**
104
	 * @param FilesDocument $document
105
	 * @param Node $file
106
	 */
107
	public function updateDocumentAccess(FilesDocument $document, Node $file) {
108
109
		$access = new DocumentAccess(
110
			$file->getOwner()
111
				 ->getUID()
112
		);
113
114
		$fileShares = new FileShares();
115
		$this->getSharesFromFile($file, $fileShares);
116
		$access->setUsers($fileShares->getUsers());
117
		$access->setGroups($fileShares->getGroups());
118
		$access->setCircles($fileShares->getCircles());
119
		$access->setLinks($fileShares->getLinks());
120
121
		$document->setAccess($access);
122
	}
123
124
125
	/**
126
	 * @param FilesDocument $document
127
	 * @param Node $file
128
	 * @param array $users
129
	 */
130
	public function getShareUsers(FilesDocument $document, Node $file, &$users) {
0 ignored issues
show
Unused Code introduced by
The parameter $document 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...
131
132
133
//		if ($file->getStorage()
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% 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...
134
//				 ->isLocal() === false) {
135
//			$shares = $this->externalFilesService->getAllSharesFromExternalFile($access);
136
//		} else {
137
//			$shares = $this->getAllSharesFromFile($file);
138
////		}
139
//
140
//	}
141
//
142
//
143
//	/**
144
//	 * @param Node $file
145
//	 *
146
//	 * @return array
147
//	 */
148
//	private function getAllSharesFromFile(Node $file) {
149
150
		$shares = $this->shareManager->getAccessList($file);
151
152
		if (!array_key_exists('users', $shares)) {
153
			return;
154
		}
155
156
		foreach ($shares['users'] as $user) {
157
			if (in_array($user, $users) || $this->userManager->get($user) === null) {
158
				continue;
159
			}
160
161
			array_push($users, $user);
162
		}
163
164
	}
165
166
167
	/**
168
	 * @param Node $file
169
	 * @param FileShares $fileShares
170
	 */
171
	private function getSharesFromFile(Node $file, FileShares $fileShares) {
172
173
		if (strlen($file->getPath()) <= 1) {
174
			return;
175
		}
176
177
		// we get shares from parent first
178
		$this->getSharesFromFile($file->getParent(), $fileShares);
179
180
		$shares = $this->sharesRequest->getFromFile($file);
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Files_FullTextSearch...sRequest::getFromFile() has been deprecated.

This method has been deprecated.

Loading history...
181
		foreach ($shares as $share) {
182
			if ($share['parent'] !== null) {
183
				continue;
184
			}
185
186
			$this->parseUsersShares($share, $fileShares);
187
			$this->parseUsersGroups($share, $fileShares);
188
			$this->parseUsersCircles($share, $fileShares);
189
			$this->parseUsersLinks($share, $fileShares);
190
		}
191
	}
192
193
194
	/**
195
	 * @param Node $file
196
	 * @param FileShares $fileShares
197
	 */
198
	private function getSharesFromParent(Node $file, FileShares $fileShares) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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 $fileShares 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...
199
//		$parent = basename($file->getPath());
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
200
//
201
//		echo $parent . "\n";
202
//
203
//		if (strlen($parent) <= 1) {
204
//			return;
205
//		}
206
207
	}
208
209
210
	/**
211
	 * @param array $share
212
	 * @param FileShares $fileShares
213
	 */
214
	private function parseUsersShares($share, FileShares $fileShares) {
215
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_USER) {
216
			return;
217
		}
218
219
		$fileShares->addUser($share['share_with']);
220
	}
221
222
223
	/**
224
	 * @param array $share
225
	 * @param FileShares $fileShares
226
	 */
227
	private function parseUsersGroups($share, FileShares $fileShares) {
228
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_GROUP) {
229
			return;
230
		}
231
232
		$fileShares->addGroup($share['share_with']);
233
	}
234
235
236
	/**
237
	 * @param array $share
238
	 * @param FileShares $fileShares
239
	 */
240
	private function parseUsersCircles($share, FileShares $fileShares) {
241
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_CIRCLE) {
242
			return;
243
		}
244
245
		$fileShares->addCircle($share['share_with']);
246
	}
247
248
249
	/**
250
	 * @param array $share
251
	 * @param FileShares $fileShares
252
	 */
253
	private function parseUsersLinks($share, FileShares $fileShares) {
254
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_LINK) {
255
			return;
256
		}
257
258
		$fileShares->addLink($share['share_with']);
259
	}
260
261
262
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
263
//
264
//	/**
265
//	 * @param DocumentAccess $access
266
//	 *
267
//	 * @return array
268
//	 */
269
//	public function getAllSharesFromExternalFile(DocumentAccess $access) {
270
//		$result = $access->getUsers();
271
//
272
//		if ($access->getOwnerId() !== '') {
273
//			array_push($result, $access->getOwnerId());
274
//		}
275
//
276
//		// TODO: get users from groups & circles.
277
//		return $result;
278
//	}
279
//
280
//
281
//	/**
282
//	 * @param FilesDocument $document
283
//	 * @param Node $file
284
//	 */
285
//	public function updateDocumentAccessFromExternalFile(FilesDocument &$document, Node $file) {
286
//
287
//		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
288
//			return;
289
//		}
290
//
291
//		try {
292
//			$mount = $this->getExternalMount($file);
293
//		} catch (FileIsNotIndexableException $e) {
294
//			return;
295
//		}
296
//
297
//		$access = $document->getAccess();
298
//
299
//		if ($this->isMountFullGlobal($mount)) {
300
//			$access->addUsers(['__all']);
301
//		} else {
302
//			$access->addUsers($mount->getUsers());
303
//			$access->addGroups($mount->getGroups());
304
////		 	$access->addCircles($mount->getCircles());
305
//		}
306
//
307
//		// twist 'n tweak.
308
//		if (!$mount->isGlobal()) {
309
//			$access->setOwnerId($mount->getUsers()[0]);
310
//		}
311
//
312
//		$document->setAccess($access);
313
//	}
314
//
315
//
316
//	/**
317
//	 * @param ExternalMount $mount
318
//	 *
319
//	 * @return bool
320
//	 */
321
//	public function isMountFullGlobal(ExternalMount $mount) {
322
//		if (sizeof($mount->getGroups()) > 0) {
323
//			return false;
324
//		}
325
//
326
//		if (sizeof($mount->getUsers()) !== 1) {
327
//			return false;
328
//		}
329
//
330
//		if ($mount->getUsers()[0] === 'all') {
331
//			return true;
332
//		}
333
//
334
//		return false;
335
//	}
336
//
337
//
338
//	/**
339
//	 * @param Node $file
340
//	 *
341
//	 * @return ExternalMount
342
//	 * @throws FileIsNotIndexableException
343
//	 */
344
//	private function getExternalMount(Node $file) {
345
//
346
//		foreach ($this->externalMounts as $mount) {
347
//			if (strpos($file->getPath(), $mount->getPath()) === 0) {
348
//				return $mount;
349
//			}
350
//		}
351
//
352
//		throw new FileIsNotIndexableException();
353
//	}
354
//
355
//
356
//	/**
357
//	 * @param $userId
358
//	 *
359
//	 * @return ExternalMount[]
360
//	 */
361
//	private function getExternalMountsForUser($userId) {
362
//
363
//		$externalMounts = [];
364
//
365
//		// TODO: deprecated - use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages()
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 109 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...
366
//		$mounts = \OC_Mount_Config::getAbsoluteMountPoints($userId);
367
//		foreach ($mounts as $mountPoint => $mount) {
368
//			$externalMount = new ExternalMount();
369
//			$externalMount->setId($mount['id'])
370
//						  ->setPath($mountPoint)
371
//						  ->setGroups($mount['applicable']['groups'])
372
//						  ->setUsers($mount['applicable']['users'])
373
//						  ->setGlobal((!$mount['personal']));
374
//			$externalMounts[] = $externalMount;
375
//		}
376
//
377
//		return $externalMounts;
378
//	}
379
380
}