Completed
Push — master ( 3564a7...c8469e )
by Maxence
02:04
created

GroupFoldersService::isMountFullGlobal()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
rs 9.2
cc 4
eloc 8
nc 4
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
namespace OCA\Files_FullTextSearch\Service;
29
30
31
use OCA\Files_FullTextSearch\Exceptions\FileIsNotIndexableException;
32
use OCA\Files_FullTextSearch\Exceptions\KnownFileSourceException;
33
use OCA\Files_FullTextSearch\Model\FilesDocument;
34
use OCA\Files_FullTextSearch\Model\MountPoint;
35
use OCA\GroupFolders\Folder\FolderManager;
36
use OCA\GroupFolders\Mount\MountProvider;
37
use OCP\App;
38
use OCP\Files\IRootFolder;
39
use OCP\Files\Node;
40
use OCP\Files\Storage\IStorageFactory;
41
use OCP\IUserManager;
42
use OCP\Share\IManager;
43
44
class GroupFoldersService {
45
46
47
	const DOCUMENT_SOURCE = 'group_folders';
48
49
	/** @var IRootFolder */
50
	private $rootFolder;
51
52
	/** @var IUserManager */
53
	private $userManager;
54
55
	/** @var IManager */
56
	private $shareManager;
57
58
	/** @var FolderManager */
59
	private $folderManager;
60
61
	/** @var LocalFilesService */
62
	private $localFilesService;
63
64
	/** @var ConfigService */
65
	private $configService;
66
67
	/** @var MiscService */
68
	private $miscService;
69
70
71
	/** @var MountPoint[] */
72
	private $groupFolders = [];
73
74
75
	/**
76
	 * ExternalFilesService constructor.
77
	 *
78
	 * @param IRootFolder $rootFolder
79
	 * @param IUserManager $userManager
80
	 * @param IManager $shareManager
81
	 * @param FolderManager $folderManager
82
	 * @param LocalFilesService $localFilesService
83
	 * @param ConfigService $configService
84
	 * @param MiscService $miscService
85
	 */
86 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...
87
		IRootFolder $rootFolder, IUserManager $userManager, IManager $shareManager,
88
		FolderManager $folderManager, LocalFilesService $localFilesService,
89
		ConfigService $configService,
90
		MiscService $miscService
91
	) {
92
		$this->rootFolder = $rootFolder;
93
		$this->userManager = $userManager;
94
		$this->shareManager = $shareManager;
95
		$this->folderManager = $folderManager;
96
97
		$this->localFilesService = $localFilesService;
98
99
		$this->configService = $configService;
100
		$this->miscService = $miscService;
101
	}
102
103
104
	/**
105
	 * @param string $userId
106
	 */
107
	public function initGroupSharesForUser($userId) {
108
		$this->groupFolders = [];
109
		if (!App::isEnabled('groupfolders')) {
110
			return;
111
		}
112
113
114
		$this->groupFolders = $this->getMountPoints($userId);
115
	}
116
117
118
	/**
119
	 * @param Node $file
120
	 *
121
	 * @param string $source
122
	 *
123
	 * @throws KnownFileSourceException
124
	 */
125
	public function getFileSource(Node $file, &$source) {
126
127
		try {
128
			$this->getMountPoint($file);
129
		} catch (FileIsNotIndexableException $e) {
130
			return;
131
		}
132
133
		$source = self::DOCUMENT_SOURCE;
134
		throw new KnownFileSourceException();
135
	}
136
137
138
	/**
139
	 * @param FilesDocument $document
140
	 * @param Node $file
141
	 */
142
	public function updateDocumentAccess(FilesDocument &$document, Node $file) {
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...
143
144
		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
145
			return;
146
		}
147
148
//		try {
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% 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...
149
//			$mount = $this->getGroupFolderMount($file);
150
//		} catch (FileIsNotIndexableException $e) {
151
//			return;
152
//		}
153
154
		$access = $document->getAccess();
155
		echo json_encode($access);
156
//		if ($this->isMountFullGlobal($mount)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
157
//			$access->addUsers(['__all']);
158
//		} else {
159
//			$access->addUsers($mount->getUsers());
160
//			$access->addGroups($mount->getGroups());
161
////		 	$access->addCircles($mount->getCircles());
162
//		}
163
//
164
//		// twist 'n tweak.
165
//		if (!$mount->isGlobal()) {
166
//			$access->setOwnerId($mount->getUsers()[0]);
167
//		}
168
169
		$document->setAccess($access);
170
	}
171
172
173
	/**
174
	 * @param FilesDocument $document
175
	 * @param array $users
176
	 */
177
	public function getShareUsers(FilesDocument $document, &$users) {
178
179
		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
180
			return;
181
		}
182
183
		$this->localFilesService->getSharedUsersFromAccess($document->getAccess(), $users);
184
	}
185
186
187
	/**
188
	 * @param Node $file
189
	 *
190
	 * @return MountPoint
191
	 * @throws FileIsNotIndexableException
192
	 */
193
	private function getMountPoint(Node $file) {
194
195
		foreach ($this->groupFolders as $mount) {
196
			echo '######    ' . $file->getPath() . '  ' . $mount->getPath() . "\n";
197
			if (strpos($file->getPath(), $mount->getPath()) === 0) {
198
				return $mount;
199
			}
200
		}
201
202
		throw new FileIsNotIndexableException();
203
204
	}
205
206
207
	/**
208
	 * @param string $userId
209
	 *
210
	 * @return MountPoint[]
211
	 */
212
	private function getMountPoints($userId) {
213
214
		$mountPoints = [];
215
		$mounts = $this->folderManager->getAllFolders();
216
		foreach ($mounts as $path => $mount) {
217
			$mountPoint = new MountPoint();
218
			$mountPoint->setId($mount['id'])
219
					   ->setPath('/' . $userId . '/files/' . $mount['mount_point'])
220
					   ->setGroups(array_keys($mount['applicable']['groups']));
221
			$mountPoints[] = $mountPoint;
222
		}
223
224
		return $mountPoints;
225
	}
226
227
228
}