|
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\GroupFolderMount; |
|
35
|
|
|
use OCP\App; |
|
36
|
|
|
use OCP\Files\IRootFolder; |
|
37
|
|
|
use OCP\Files\Node; |
|
38
|
|
|
use OCP\IUserManager; |
|
39
|
|
|
use OCP\Share\IManager; |
|
40
|
|
|
|
|
41
|
|
|
class GroupFoldersService { |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
const DOCUMENT_SOURCE = 'group_folders'; |
|
45
|
|
|
|
|
46
|
|
|
/** @var IRootFolder */ |
|
47
|
|
|
private $rootFolder; |
|
48
|
|
|
|
|
49
|
|
|
/** @var IUserManager */ |
|
50
|
|
|
private $userManager; |
|
51
|
|
|
|
|
52
|
|
|
/** @var IManager */ |
|
53
|
|
|
private $shareManager; |
|
54
|
|
|
|
|
55
|
|
|
/** @var LocalFilesService */ |
|
56
|
|
|
private $localFilesService; |
|
57
|
|
|
|
|
58
|
|
|
/** @var ConfigService */ |
|
59
|
|
|
private $configService; |
|
60
|
|
|
|
|
61
|
|
|
/** @var MiscService */ |
|
62
|
|
|
private $miscService; |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** @var GroupFolderMount[] */ |
|
66
|
|
|
private $groupFolders = []; |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* ExternalFilesService constructor. |
|
71
|
|
|
* |
|
72
|
|
|
* @param IRootFolder $rootFolder |
|
73
|
|
|
* @param IUserManager $userManager |
|
74
|
|
|
* @param IManager $shareManager |
|
75
|
|
|
* @param LocalFilesService $localFilesService |
|
76
|
|
|
* @param ConfigService $configService |
|
77
|
|
|
* @param MiscService $miscService |
|
78
|
|
|
*/ |
|
79
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
80
|
|
|
IRootFolder $rootFolder, IUserManager $userManager, IManager $shareManager, |
|
81
|
|
|
LocalFilesService $localFilesService, ConfigService $configService, MiscService $miscService |
|
82
|
|
|
) { |
|
83
|
|
|
$this->rootFolder = $rootFolder; |
|
84
|
|
|
$this->userManager = $userManager; |
|
85
|
|
|
$this->shareManager = $shareManager; |
|
86
|
|
|
|
|
87
|
|
|
$this->localFilesService = $localFilesService; |
|
88
|
|
|
|
|
89
|
|
|
$this->configService = $configService; |
|
90
|
|
|
$this->miscService = $miscService; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* |
|
96
|
|
|
*/ |
|
97
|
|
|
public function initGroupShares() { |
|
98
|
|
|
$this->groupFolders = []; |
|
99
|
|
|
if (!App::isEnabled('groupfolders')) { |
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$this->groupFolders = $this->getGroupFoldersMounts(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param Node $file |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $source |
|
111
|
|
|
* |
|
112
|
|
|
* @throws KnownFileSourceException |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getFileSource(Node $file, &$source) { |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
try { |
|
118
|
|
|
$this->getGroupFolderMount($file); |
|
119
|
|
|
} catch (FileIsNotIndexableException $e) { |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$source = self::DOCUMENT_SOURCE; |
|
124
|
|
|
throw new KnownFileSourceException(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// |
|
|
|
|
|
|
128
|
|
|
// /** |
|
129
|
|
|
// * @param DocumentAccess $access |
|
130
|
|
|
// * |
|
131
|
|
|
// * @return array |
|
132
|
|
|
// */ |
|
133
|
|
|
// public function getAllSharesFromExternalFile(DocumentAccess $access) { |
|
134
|
|
|
// $result = $access->getUsers(); |
|
135
|
|
|
// |
|
136
|
|
|
// if ($access->getOwnerId() !== '') { |
|
137
|
|
|
// array_push($result, $access->getOwnerId()); |
|
138
|
|
|
// } |
|
139
|
|
|
// |
|
140
|
|
|
// // TODO: get users from groups & circles. |
|
141
|
|
|
// return $result; |
|
142
|
|
|
// } |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param FilesDocument $document |
|
147
|
|
|
* @param Node $file |
|
148
|
|
|
*/ |
|
149
|
|
|
public function updateDocumentAccess(FilesDocument &$document, Node $file) { |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
if ($document->getSource() !== self::DOCUMENT_SOURCE) { |
|
152
|
|
|
return; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// try { |
|
|
|
|
|
|
156
|
|
|
// $mount = $this->getGroupFolderMount($file); |
|
157
|
|
|
// } catch (FileIsNotIndexableException $e) { |
|
158
|
|
|
// return; |
|
159
|
|
|
// } |
|
160
|
|
|
|
|
161
|
|
|
$access = $document->getAccess(); |
|
162
|
|
|
echo json_encode($access); |
|
163
|
|
|
// if ($this->isMountFullGlobal($mount)) { |
|
|
|
|
|
|
164
|
|
|
// $access->addUsers(['__all']); |
|
165
|
|
|
// } else { |
|
166
|
|
|
// $access->addUsers($mount->getUsers()); |
|
167
|
|
|
// $access->addGroups($mount->getGroups()); |
|
168
|
|
|
//// $access->addCircles($mount->getCircles()); |
|
169
|
|
|
// } |
|
170
|
|
|
// |
|
171
|
|
|
// // twist 'n tweak. |
|
172
|
|
|
// if (!$mount->isGlobal()) { |
|
173
|
|
|
// $access->setOwnerId($mount->getUsers()[0]); |
|
174
|
|
|
// } |
|
175
|
|
|
|
|
176
|
|
|
$document->setAccess($access); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param FilesDocument $document |
|
182
|
|
|
* @param array $users |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getShareUsers(FilesDocument $document, &$users) { |
|
185
|
|
|
|
|
186
|
|
|
if ($document->getSource() !== self::DOCUMENT_SOURCE) { |
|
187
|
|
|
return; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$this->localFilesService->getSharedUsersFromAccess($document->getAccess(), $users); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param GroupFolderMount $mount |
|
196
|
|
|
* |
|
197
|
|
|
* @return bool |
|
198
|
|
|
*/ |
|
199
|
|
View Code Duplication |
public function isMountFullGlobal(GroupFolderMount $mount) { |
|
|
|
|
|
|
200
|
|
|
if (sizeof($mount->getGroups()) > 0) { |
|
201
|
|
|
return false; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
if (sizeof($mount->getUsers()) !== 1) { |
|
205
|
|
|
return false; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
if ($mount->getUsers()[0] === 'all') { |
|
|
|
|
|
|
209
|
|
|
return true; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return false; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param Node $file |
|
218
|
|
|
* |
|
219
|
|
|
* @return GroupFolderMount |
|
220
|
|
|
* @throws FileIsNotIndexableException |
|
221
|
|
|
*/ |
|
222
|
|
View Code Duplication |
private function getGroupFolderMount(Node $file) { |
|
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
foreach ($this->groupFolders as $mount) { |
|
225
|
|
|
if (strpos($file->getPath(), $mount->getPath()) === 0) { |
|
226
|
|
|
return $mount; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
throw new FileIsNotIndexableException(); |
|
231
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @return GroupFolderMount[] |
|
237
|
|
|
*/ |
|
238
|
|
|
private function getGroupFoldersMounts() { |
|
239
|
|
|
|
|
240
|
|
|
$groupFolders = []; |
|
241
|
|
|
|
|
242
|
|
|
// TODO: deprecated - use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages() |
|
|
|
|
|
|
243
|
|
|
$mounts = []; |
|
244
|
|
|
foreach ($mounts as $mountPoint => $mount) { |
|
245
|
|
|
$groupFolder = new GroupFolderMount(); |
|
246
|
|
|
// $externalMount->setId($mount['id']) |
|
|
|
|
|
|
247
|
|
|
// ->setPath($mountPoint) |
|
248
|
|
|
// ->setGroups($mount['applicable']['groups']) |
|
249
|
|
|
// ->setUsers($mount['applicable']['users']) |
|
250
|
|
|
// ->setGlobal((!$mount['personal'])); |
|
251
|
|
|
$groupFolders[] = $groupFolder; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return $groupFolders; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
} |
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.