Completed
Push — master ( 766ef1...3564a7 )
by Maxence
02:20
created

LocalFilesService::getSharedUsersFromAccess()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
rs 9.2
c 1
b 0
f 0
cc 4
eloc 7
nc 6
nop 2
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
		$shares = $this->shareManager->getAccessList($file);
133
134
		if (!array_key_exists('users', $shares)) {
135
			return;
136
		}
137
138
		foreach ($shares['users'] as $user) {
139
			if (in_array($user, $users) || $this->userManager->get($user) === null) {
140
				continue;
141
			}
142
143
			array_push($users, $user);
144
		}
145
146
	}
147
148
149
	/**
150
	 * same a getShareUsers, but we do it 'manually'
151
	 *
152
	 * @param DocumentAccess $access
153
	 * @param $users
154
	 */
155
	public function getSharedUsersFromAccess(DocumentAccess $access, &$users) {
156
157
		$result = $access->getUsers();
158
159
		if ($access->getOwnerId() !== '') {
160
			array_push($result, $access->getOwnerId());
161
		}
162
163
		foreach($result as $user) {
164
			if (!in_array($user, $users))
165
			{
166
				$users[] = $user;
167
			}
168
		}
169
170
		// TODO: get users from groups & circles.
171
172
	}
173
174
175
176
	/**
177
	 * @param Node $file
178
	 * @param FileShares $fileShares
179
	 */
180
	private function getSharesFromFile(Node $file, FileShares $fileShares) {
181
182
		if (strlen($file->getPath()) <= 1) {
183
			return;
184
		}
185
186
		// we get shares from parent first
187
		$this->getSharesFromFile($file->getParent(), $fileShares);
188
189
		$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...
190
		foreach ($shares as $share) {
191
			if ($share['parent'] !== null) {
192
				continue;
193
			}
194
195
			$this->parseUsersShares($share, $fileShares);
196
			$this->parseUsersGroups($share, $fileShares);
197
			$this->parseUsersCircles($share, $fileShares);
198
			$this->parseUsersLinks($share, $fileShares);
199
		}
200
	}
201
202
203
	/**
204
	 * @param Node $file
205
	 * @param FileShares $fileShares
206
	 */
207
	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...
208
//		$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...
209
//
210
//		echo $parent . "\n";
211
//
212
//		if (strlen($parent) <= 1) {
213
//			return;
214
//		}
215
216
	}
217
218
219
	/**
220
	 * @param array $share
221
	 * @param FileShares $fileShares
222
	 */
223
	private function parseUsersShares($share, FileShares $fileShares) {
224
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_USER) {
225
			return;
226
		}
227
228
		$fileShares->addUser($share['share_with']);
229
	}
230
231
232
	/**
233
	 * @param array $share
234
	 * @param FileShares $fileShares
235
	 */
236
	private function parseUsersGroups($share, FileShares $fileShares) {
237
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_GROUP) {
238
			return;
239
		}
240
241
		$fileShares->addGroup($share['share_with']);
242
	}
243
244
245
	/**
246
	 * @param array $share
247
	 * @param FileShares $fileShares
248
	 */
249
	private function parseUsersCircles($share, FileShares $fileShares) {
250
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_CIRCLE) {
251
			return;
252
		}
253
254
		$fileShares->addCircle($share['share_with']);
255
	}
256
257
258
	/**
259
	 * @param array $share
260
	 * @param FileShares $fileShares
261
	 */
262
	private function parseUsersLinks($share, FileShares $fileShares) {
263
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_LINK) {
264
			return;
265
		}
266
267
		$fileShares->addLink($share['share_with']);
268
	}
269
270
271
//
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...
272
//
273
//	/**
274
//	 * @param DocumentAccess $access
275
//	 *
276
//	 * @return array
277
//	 */
278
//	public function getAllSharesFromExternalFile(DocumentAccess $access) {
279
//		$result = $access->getUsers();
280
//
281
//		if ($access->getOwnerId() !== '') {
282
//			array_push($result, $access->getOwnerId());
283
//		}
284
//
285
//		// TODO: get users from groups & circles.
286
//		return $result;
287
//	}
288
//
289
//
290
//	/**
291
//	 * @param FilesDocument $document
292
//	 * @param Node $file
293
//	 */
294
//	public function updateDocumentAccessFromExternalFile(FilesDocument &$document, Node $file) {
295
//
296
//		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
297
//			return;
298
//		}
299
//
300
//		try {
301
//			$mount = $this->getExternalMount($file);
302
//		} catch (FileIsNotIndexableException $e) {
303
//			return;
304
//		}
305
//
306
//		$access = $document->getAccess();
307
//
308
//		if ($this->isMountFullGlobal($mount)) {
309
//			$access->addUsers(['__all']);
310
//		} else {
311
//			$access->addUsers($mount->getUsers());
312
//			$access->addGroups($mount->getGroups());
313
////		 	$access->addCircles($mount->getCircles());
314
//		}
315
//
316
//		// twist 'n tweak.
317
//		if (!$mount->isGlobal()) {
318
//			$access->setOwnerId($mount->getUsers()[0]);
319
//		}
320
//
321
//		$document->setAccess($access);
322
//	}
323
//
324
//
325
//	/**
326
//	 * @param ExternalMount $mount
327
//	 *
328
//	 * @return bool
329
//	 */
330
//	public function isMountFullGlobal(ExternalMount $mount) {
331
//		if (sizeof($mount->getGroups()) > 0) {
332
//			return false;
333
//		}
334
//
335
//		if (sizeof($mount->getUsers()) !== 1) {
336
//			return false;
337
//		}
338
//
339
//		if ($mount->getUsers()[0] === 'all') {
340
//			return true;
341
//		}
342
//
343
//		return false;
344
//	}
345
//
346
//
347
//	/**
348
//	 * @param Node $file
349
//	 *
350
//	 * @return ExternalMount
351
//	 * @throws FileIsNotIndexableException
352
//	 */
353
//	private function getExternalMount(Node $file) {
354
//
355
//		foreach ($this->externalMounts as $mount) {
356
//			if (strpos($file->getPath(), $mount->getPath()) === 0) {
357
//				return $mount;
358
//			}
359
//		}
360
//
361
//		throw new FileIsNotIndexableException();
362
//	}
363
//
364
//
365
//	/**
366
//	 * @param $userId
367
//	 *
368
//	 * @return ExternalMount[]
369
//	 */
370
//	private function getExternalMountsForUser($userId) {
371
//
372
//		$externalMounts = [];
373
//
374
//		// 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...
375
//		$mounts = \OC_Mount_Config::getAbsoluteMountPoints($userId);
376
//		foreach ($mounts as $mountPoint => $mount) {
377
//			$externalMount = new ExternalMount();
378
//			$externalMount->setId($mount['id'])
379
//						  ->setPath($mountPoint)
380
//						  ->setGroups($mount['applicable']['groups'])
381
//						  ->setUsers($mount['applicable']['users'])
382
//						  ->setGlobal((!$mount['personal']));
383
//			$externalMounts[] = $externalMount;
384
//		}
385
//
386
//		return $externalMounts;
387
//	}
388
389
}