|
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 Exception; |
|
32
|
|
|
use OC\App\AppManager; |
|
33
|
|
|
use OCA\Files_FullTextSearch\Exceptions\FileIsNotIndexableException; |
|
34
|
|
|
use OCA\Files_FullTextSearch\Exceptions\GroupFolderNotFoundException; |
|
35
|
|
|
use OCA\Files_FullTextSearch\Exceptions\KnownFileSourceException; |
|
36
|
|
|
use OCA\Files_FullTextSearch\Model\FilesDocument; |
|
37
|
|
|
use OCA\Files_FullTextSearch\Model\MountPoint; |
|
38
|
|
|
use OCA\FullTextSearch\Model\Index; |
|
39
|
|
|
use OCA\GroupFolders\Folder\FolderManager; |
|
40
|
|
|
use OCP\Files\Node; |
|
41
|
|
|
use OCP\IDBConnection; |
|
42
|
|
|
use OCP\IGroupManager; |
|
43
|
|
|
use OCP\Share\IManager; |
|
44
|
|
|
|
|
45
|
|
|
class GroupFoldersService { |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** @var IManager */ |
|
49
|
|
|
private $shareManager; |
|
50
|
|
|
|
|
51
|
|
|
/** @var IGroupManager */ |
|
52
|
|
|
private $groupManager; |
|
53
|
|
|
|
|
54
|
|
|
/** @var FolderManager */ |
|
55
|
|
|
private $folderManager; |
|
56
|
|
|
|
|
57
|
|
|
/** @var LocalFilesService */ |
|
58
|
|
|
private $localFilesService; |
|
59
|
|
|
|
|
60
|
|
|
/** @var ConfigService */ |
|
61
|
|
|
private $configService; |
|
62
|
|
|
|
|
63
|
|
|
/** @var MiscService */ |
|
64
|
|
|
private $miscService; |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** @var MountPoint[] */ |
|
68
|
|
|
private $groupFolders = []; |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* ExternalFilesService constructor. |
|
73
|
|
|
* |
|
74
|
|
|
* @param $userId |
|
75
|
|
|
* @param IDBConnection $dbConnection |
|
76
|
|
|
* @param AppManager $appManager |
|
77
|
|
|
* @param IManager $shareManager |
|
78
|
|
|
* @param IGroupManager $groupManager |
|
79
|
|
|
* @param LocalFilesService $localFilesService |
|
80
|
|
|
* @param ConfigService $configService |
|
81
|
|
|
* @param MiscService $miscService |
|
82
|
|
|
*/ |
|
83
|
|
|
public function __construct( |
|
84
|
|
|
$userId, IDBConnection $dbConnection, AppManager $appManager, IManager $shareManager, |
|
85
|
|
|
IGroupManager $groupManager, LocalFilesService $localFilesService, |
|
86
|
|
|
ConfigService $configService, MiscService $miscService |
|
87
|
|
|
) { |
|
88
|
|
|
|
|
89
|
|
|
if ($appManager->isEnabledForUser('groupfolders', $userId)) { |
|
90
|
|
|
try { |
|
91
|
|
|
$this->folderManager = new FolderManager($dbConnection); |
|
92
|
|
|
} catch (Exception $e) { |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->shareManager = $shareManager; |
|
98
|
|
|
$this->groupManager = $groupManager; |
|
99
|
|
|
$this->localFilesService = $localFilesService; |
|
100
|
|
|
$this->configService = $configService; |
|
101
|
|
|
$this->miscService = $miscService; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $userId |
|
107
|
|
|
*/ |
|
108
|
|
|
public function initGroupSharesForUser($userId) { |
|
109
|
|
|
if ($this->folderManager === null) { |
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->groupFolders = $this->getMountPoints($userId); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param Node $file |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $source |
|
121
|
|
|
* |
|
122
|
|
|
* @throws KnownFileSourceException |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getFileSource(Node $file, &$source) { |
|
125
|
|
|
if ($this->folderManager === null) { |
|
126
|
|
|
return; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
try { |
|
130
|
|
|
$this->getMountPoint($file); |
|
131
|
|
|
} catch (FileIsNotIndexableException $e) { |
|
132
|
|
|
return; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$source = ConfigService::FILES_GROUP_FOLDERS; |
|
136
|
|
|
throw new KnownFileSourceException(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param FilesDocument $document |
|
142
|
|
|
* @param Node $file |
|
143
|
|
|
*/ |
|
144
|
|
|
public function updateDocumentAccess(FilesDocument &$document, Node $file) { |
|
145
|
|
|
|
|
146
|
|
|
if ($document->getSource() !== ConfigService::FILES_GROUP_FOLDERS) { |
|
147
|
|
|
return; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
try { |
|
151
|
|
|
$mount = $this->getMountPoint($file); |
|
152
|
|
|
} catch (FileIsNotIndexableException $e) { |
|
153
|
|
|
return; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
$access = $document->getAccess(); |
|
158
|
|
|
$access->addGroups($mount->getGroups()); |
|
159
|
|
|
|
|
160
|
|
|
$document->getIndex() |
|
161
|
|
|
->addOption('group_folder_id', $mount->getId()); |
|
162
|
|
|
$document->setAccess($access); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param FilesDocument $document |
|
168
|
|
|
* @param array $users |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getShareUsers(FilesDocument $document, &$users) { |
|
171
|
|
|
if ($document->getSource() !== ConfigService::FILES_GROUP_FOLDERS) { |
|
172
|
|
|
return; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$this->localFilesService->getSharedUsersFromAccess($document->getAccess(), $users); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param Node $file |
|
181
|
|
|
* |
|
182
|
|
|
* @return MountPoint |
|
183
|
|
|
* @throws FileIsNotIndexableException |
|
184
|
|
|
*/ |
|
185
|
|
View Code Duplication |
private function getMountPoint(Node $file) { |
|
|
|
|
|
|
186
|
|
|
foreach ($this->groupFolders as $mount) { |
|
187
|
|
|
if (strpos($file->getPath(), $mount->getPath()) === 0) { |
|
188
|
|
|
return $mount; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
throw new FileIsNotIndexableException(); |
|
193
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param string $userId |
|
199
|
|
|
* |
|
200
|
|
|
* @return MountPoint[] |
|
201
|
|
|
*/ |
|
202
|
|
|
private function getMountPoints($userId) { |
|
203
|
|
|
|
|
204
|
|
|
$mountPoints = []; |
|
205
|
|
|
$mounts = $this->folderManager->getAllFolders(); |
|
206
|
|
|
|
|
207
|
|
|
foreach ($mounts as $path => $mount) { |
|
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
|
|
|
/** |
|
220
|
|
|
* @param Index $index |
|
221
|
|
|
* |
|
222
|
|
|
* @return string|void |
|
223
|
|
|
*/ |
|
224
|
|
|
public function impersonateOwner(Index $index) { |
|
225
|
|
|
if ($index->getSource() !== ConfigService::FILES_GROUP_FOLDERS) { |
|
226
|
|
|
return; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
if ($this->folderManager === null) { |
|
230
|
|
|
return; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
$groupFolderId = $index->getOption('group_folder_id', 0); |
|
234
|
|
|
try { |
|
235
|
|
|
$mount = $this->getGroupFolderById($groupFolderId); |
|
236
|
|
|
} catch (GroupFolderNotFoundException $e) { |
|
237
|
|
|
return; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
$index->setOwnerId($this->getRandomUserFromGroups(array_keys($mount['groups']))); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param int $groupFolderId |
|
246
|
|
|
* |
|
247
|
|
|
* @return array |
|
248
|
|
|
* @throws GroupFolderNotFoundException |
|
249
|
|
|
*/ |
|
250
|
|
|
private function getGroupFolderById($groupFolderId) { |
|
251
|
|
|
if ($groupFolderId === 0) { |
|
252
|
|
|
throw new GroupFolderNotFoundException(); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
$mounts = $this->folderManager->getAllFolders(); |
|
256
|
|
|
foreach ($mounts as $path => $mount) { |
|
257
|
|
|
if ($mount['id'] === $groupFolderId) { |
|
258
|
|
|
return $mount; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
throw new GroupFolderNotFoundException(); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @param array $groups |
|
268
|
|
|
* |
|
269
|
|
|
* @return string |
|
270
|
|
|
*/ |
|
271
|
|
|
private function getRandomUserFromGroups($groups) { |
|
272
|
|
|
foreach ($groups as $groupName) { |
|
273
|
|
|
$group = $this->groupManager->get($groupName); |
|
274
|
|
|
$users = $group->getUsers(); |
|
275
|
|
|
if (sizeof($users) > 0) { |
|
276
|
|
|
return array_keys($users)[0]; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
return ''; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
} |
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.