Completed
Push — master ( c8469e...07cb67 )
by Maxence
01:51
created

GroupFoldersService::updateDocumentAccess()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.4285
c 3
b 0
f 0
cc 3
eloc 10
nc 3
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
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
	public function __construct(
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) {
143
144
		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
145
			return;
146
		}
147
148
		try {
149
			$mount = $this->getMountPoint($file);
150
		} catch (FileIsNotIndexableException $e) {
151
			return;
152
		}
153
154
		$access = $document->getAccess();
155
		$access->addGroups($mount->getGroups());
156
157
		$document->setAccess($access);
158
	}
159
160
161
	/**
162
	 * @param FilesDocument $document
163
	 * @param array $users
164
	 */
165
	public function getShareUsers(FilesDocument $document, &$users) {
166
167
		if ($document->getSource() !== self::DOCUMENT_SOURCE) {
168
			return;
169
		}
170
171
		$this->localFilesService->getSharedUsersFromAccess($document->getAccess(), $users);
172
	}
173
174
175
	/**
176
	 * @param Node $file
177
	 *
178
	 * @return MountPoint
179
	 * @throws FileIsNotIndexableException
180
	 */
181 View Code Duplication
	private function getMountPoint(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...
182
183
		foreach ($this->groupFolders as $mount) {
184
			if (strpos($file->getPath(), $mount->getPath()) === 0) {
185
				return $mount;
186
			}
187
		}
188
189
		throw new FileIsNotIndexableException();
190
191
	}
192
193
194
	/**
195
	 * @param string $userId
196
	 *
197
	 * @return MountPoint[]
198
	 */
199
	private function getMountPoints($userId) {
200
201
		$mountPoints = [];
202
		$mounts = $this->folderManager->getAllFolders();
203
204
205
		foreach ($mounts as $path => $mount) {
206
			echo '---------------- ' . json_encode($mount) . "\n";
207
208
			$mountPoint = new MountPoint();
209
			$mountPoint->setId($mount['id'])
210
					   ->setPath('/' . $userId . '/files/' . $mount['mount_point'])
211
					   ->setGroups(array_keys($mount['groups']));
212
			$mountPoints[] = $mountPoint;
213
		}
214
215
		return $mountPoints;
216
	}
217
218
219
}