Completed
Push — master ( 379ba1...581bac )
by Maxence
03:40
created

LocalFilesService::getShareUsersFromFile()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 6
eloc 10
nc 5
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 Exception;
31
use OC\Share\Constants;
32
use OCA\Files_FullTextSearch\Db\SharesRequest;
33
use OCA\Files_FullTextSearch\Model\FilesDocument;
34
use OCA\Files_FullTextSearch\Model\FileShares;
35
use OCA\FullTextSearch\Model\DocumentAccess;
36
use OCP\Files\IRootFolder;
37
use OCP\Files\Node;
38
use OCP\IGroupManager;
39
use OCP\IUserManager;
40
use OCP\Share\IManager;
41
42
class LocalFilesService {
43
44
	/** @var IRootFolder */
45
	private $rootFolder;
46
47
	/** @var IGroupManager */
48
	private $groupManager;
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
	/**
67
	 * ExternalFilesService constructor.
68
	 *
69
	 * @param IRootFolder $rootFolder
70
	 * @param IGroupManager $groupManager
71
	 * @param IUserManager $userManager
72
	 * @param IManager $shareManager
73
	 * @param SharesRequest $sharesRequest
74
	 * @param ConfigService $configService
75
	 * @param MiscService $miscService
76
	 */
77 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...
78
		IRootFolder $rootFolder, IGroupManager $groupManager, IUserManager $userManager,
79
		IManager $shareManager, SharesRequest $sharesRequest, ConfigService $configService,
80
		MiscService $miscService
81
	) {
82
		$this->rootFolder = $rootFolder;
83
		$this->groupManager = $groupManager;
84
		$this->userManager = $userManager;
85
		$this->shareManager = $shareManager;
86
87
		$this->sharesRequest = $sharesRequest;
88
		$this->configService = $configService;
89
		$this->miscService = $miscService;
90
	}
91
92
93
	/**
94
	 * @param Node $file
95
	 * @param string $source
96
	 */
97
	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...
98
		$source = ConfigService::FILES_LOCAL;
99
	}
100
101
102
	/**
103
	 * @param FilesDocument $document
104
	 * @param Node $file
105
	 */
106
	public function updateDocumentAccess(FilesDocument $document, Node $file) {
107
108
		$ownerId = '';
109
		if ($file->getOwner() !== null) {
110
			$ownerId = $file->getOwner()
111
							->getUID();
112
		}
113
114
		$access = new DocumentAccess($ownerId);
115
116
		$fileShares = new FileShares();
117
		$this->getSharesFromFile($file, $fileShares);
118
		$access->setUsers($fileShares->getUsers());
119
		$access->setGroups($fileShares->getGroups());
120
		$access->setCircles($fileShares->getCircles());
121
		$access->setLinks($fileShares->getLinks());
122
123
		$document->setAccess($access);
124
	}
125
126
127
	/**
128
	 * @param Node $file
129
	 * @param array $users
130
	 */
131
	public function getShareUsersFromFile(Node $file, &$users) {
132
		if ($file->getOwner() === null) {
133
			return;
134
		}
135
136
		$shares = $this->shareManager->getAccessList($file);
137
138
		if (!array_key_exists('users', $shares)) {
139
			return;
140
		}
141
142
		foreach ($shares['users'] as $user) {
143
			if (in_array($user, $users) || $this->userManager->get($user) === null) {
144
				continue;
145
			}
146
147
			array_push($users, $user);
148
		}
149
150
	}
151
152
153
	/**
154
	 * same a getShareUsers, but we do it 'manually'
155
	 *
156
	 * @param DocumentAccess $access
157
	 * @param array $users
158
	 */
159
	public function getSharedUsersFromAccess(DocumentAccess $access, &$users) {
160
161
		$result = array_merge(
162
			$access->getUsers(),
163
			$this->getSharedUsersFromAccessGroups($access),
164
			$this->getSharedUsersFromAccessCircles($access)
165
		);
166
167
		foreach ($result as $user) {
168
			if (!in_array($user, $users)) {
169
				$users[] = $user;
170
			}
171
		}
172
	}
173
174
175
	/**
176
	 * @param DocumentAccess $access
177
	 *
178
	 * @return array
179
	 */
180
	private function getSharedUsersFromAccessGroups(DocumentAccess $access) {
181
182
		$result = [];
183
		$users = [];
184
		foreach ($access->getGroups() as $groupName) {
185
			$group = $this->groupManager->get($groupName);
186
			$users = array_merge($users, $group->getUsers());
187
		}
188
189
		foreach ($users as $user) {
190
			$result[] = $user->getUID();
191
		}
192
193
		return $result;
194
	}
195
196
197
	/**
198
	 * // TODO: get users from circles.
199
	 *
200
	 * @param DocumentAccess $access
201
	 *
202
	 * @return array
203
	 */
204
	private function getSharedUsersFromAccessCircles(DocumentAccess $access) {
0 ignored issues
show
Unused Code introduced by
The parameter $access 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...
205
		$result = [];
206
207
		return $result;
208
	}
209
210
211
	/**
212
	 * @param Node $file
213
	 * @param FileShares $fileShares
214
	 */
215
	private function getSharesFromFile(Node $file, FileShares $fileShares) {
216
217
		if (strlen($file->getPath()) <= 1) {
218
			return;
219
		}
220
221
		// we get shares from parent first
222
		$this->getSharesFromFile($file->getParent(), $fileShares);
223
224
		$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...
225
		foreach ($shares as $share) {
226
			if ($share['parent'] !== null) {
227
				continue;
228
			}
229
230
			$this->parseUsersShares($share, $fileShares);
231
			$this->parseUsersGroups($share, $fileShares);
232
			$this->parseUsersCircles($share, $fileShares);
233
			$this->parseUsersLinks($share, $fileShares);
234
		}
235
	}
236
237
238
	/**
239
	 * @param Node $file
240
	 * @param FileShares $fileShares
241
	 */
242
	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...
243
//		$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...
244
//
245
//		echo $parent . "\n";
246
//
247
//		if (strlen($parent) <= 1) {
248
//			return;
249
//		}
250
251
	}
252
253
254
	/**
255
	 * @param array $share
256
	 * @param FileShares $fileShares
257
	 */
258
	private function parseUsersShares($share, FileShares $fileShares) {
259
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_USER) {
260
			return;
261
		}
262
263
		$fileShares->addUser($share['share_with']);
264
	}
265
266
267
	/**
268
	 * @param array $share
269
	 * @param FileShares $fileShares
270
	 */
271
	private function parseUsersGroups($share, FileShares $fileShares) {
272
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_GROUP) {
273
			return;
274
		}
275
276
		$fileShares->addGroup($share['share_with']);
277
	}
278
279
280
	/**
281
	 * @param array $share
282
	 * @param FileShares $fileShares
283
	 */
284
	private function parseUsersCircles($share, FileShares $fileShares) {
285
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_CIRCLE) {
286
			return;
287
		}
288
289
		$fileShares->addCircle($share['share_with']);
290
	}
291
292
293
	/**
294
	 * @param array $share
295
	 * @param FileShares $fileShares
296
	 */
297
	private function parseUsersLinks($share, FileShares $fileShares) {
298
		if ((int)$share['share_type'] !== Constants::SHARE_TYPE_LINK) {
299
			return;
300
		}
301
302
		$fileShares->addLink($share['share_with']);
303
	}
304
305
306
//
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...
307
//
308
//	/**
309
//	 * @param DocumentAccess $access
310
//	 *
311
//	 * @return array
312
//	 */
313
//	public function getAllSharesFromExternalFile(DocumentAccess $access) {
314
//		$result = $access->getUsers();
315
//
316
//		if ($access->getOwnerId() !== '') {
317
//			array_push($result, $access->getOwnerId());
318
//		}
319
//
320
//		// TODO: get users from groups & circles.
321
//		return $result;
322
//	}
323
//
324
//
325
//	/**
326
//	 * @param FilesDocument $document
327
//	 * @param Node $file
328
//	 */
329
//	public function updateDocumentAccessFromExternalFile(FilesDocument &$document, Node $file) {
330
//
331
//		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
332
//			return;
333
//		}
334
//
335
//		try {
336
//			$mount = $this->getExternalMount($file);
337
//		} catch (FileIsNotIndexableException $e) {
338
//			return;
339
//		}
340
//
341
//		$access = $document->getAccess();
342
//
343
//		if ($this->isMountFullGlobal($mount)) {
344
//			$access->addUsers(['__all']);
345
//		} else {
346
//			$access->addUsers($mount->getUsers());
347
//			$access->addGroups($mount->getGroups());
348
////		 	$access->addCircles($mount->getCircles());
349
//		}
350
//
351
//		// twist 'n tweak.
352
//		if (!$mount->isGlobal()) {
353
//			$access->setOwnerId($mount->getUsers()[0]);
354
//		}
355
//
356
//		$document->setAccess($access);
357
//	}
358
//
359
//
360
//	/**
361
//	 * @param ExternalMount $mount
362
//	 *
363
//	 * @return bool
364
//	 */
365
//	public function isMountFullGlobal(ExternalMount $mount) {
366
//		if (sizeof($mount->getGroups()) > 0) {
367
//			return false;
368
//		}
369
//
370
//		if (sizeof($mount->getUsers()) !== 1) {
371
//			return false;
372
//		}
373
//
374
//		if ($mount->getUsers()[0] === 'all') {
375
//			return true;
376
//		}
377
//
378
//		return false;
379
//	}
380
//
381
//
382
//	/**
383
//	 * @param Node $file
384
//	 *
385
//	 * @return ExternalMount
386
//	 * @throws FileIsNotIndexableException
387
//	 */
388
//	private function getExternalMount(Node $file) {
389
//
390
//		foreach ($this->externalMounts as $mount) {
391
//			if (strpos($file->getPath(), $mount->getPath()) === 0) {
392
//				return $mount;
393
//			}
394
//		}
395
//
396
//		throw new FileIsNotIndexableException();
397
//	}
398
//
399
//
400
//	/**
401
//	 * @param $userId
402
//	 *
403
//	 * @return ExternalMount[]
404
//	 */
405
//	private function getExternalMountsForUser($userId) {
406
//
407
//		$externalMounts = [];
408
//
409
//		// 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...
410
//		$mounts = \OC_Mount_Config::getAbsoluteMountPoints($userId);
411
//		foreach ($mounts as $mountPoint => $mount) {
412
//			$externalMount = new ExternalMount();
413
//			$externalMount->setId($mount['id'])
414
//						  ->setPath($mountPoint)
415
//						  ->setGroups($mount['applicable']['groups'])
416
//						  ->setUsers($mount['applicable']['users'])
417
//						  ->setGlobal((!$mount['personal']));
418
//			$externalMounts[] = $externalMount;
419
//		}
420
//
421
//		return $externalMounts;
422
//	}
423
424
}