-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathMountProvider.php
More file actions
324 lines (280 loc) Β· 9.21 KB
/
MountProvider.php
File metadata and controls
324 lines (280 loc) Β· 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\GroupFolders\Mount;
use OC\Files\Storage\Wrapper\Jail;
use OC\Files\Storage\Wrapper\PermissionsMask;
use OCA\GroupFolders\ACL\ACLManager;
use OCA\GroupFolders\ACL\ACLManagerFactory;
use OCA\GroupFolders\ACL\ACLStorageWrapper;
use OCA\GroupFolders\Folder\FolderManager;
use OCP\Constants;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Config\IMountProvider;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\Folder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IStorageFactory;
use OCP\ICache;
use OCP\IDBConnection;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
/**
* @psalm-import-type InternalFolder from FolderManager
*/
class MountProvider implements IMountProvider {
private ?Folder $root = null;
private ?int $rootStorageId = null;
public function __construct(
private FolderManager $folderManager,
private \Closure $rootProvider,
private ACLManagerFactory $aclManagerFactory,
private IUserSession $userSession,
private IRequest $request,
private IMountProviderCollection $mountProviderCollection,
private IDBConnection $connection,
private ICache $cache,
private bool $allowRootShare,
private bool $enableEncryption,
) {
}
private function getRootStorageId(): int {
if ($this->rootStorageId === null) {
$cached = $this->cache->get('root_storage_id');
if ($cached !== null) {
$this->rootStorageId = $cached;
} else {
$id = $this->getRootFolder()->getStorage()->getCache()->getNumericStorageId();
$this->cache->set('root_storage_id', $id);
$this->rootStorageId = $id;
}
}
return $this->rootStorageId;
}
/**
* @return list<InternalFolder>
*/
public function getFoldersForUser(IUser $user): array {
return $this->folderManager->getFoldersForUser($user, $this->getRootStorageId());
}
public function getMountsForUser(IUser $user, IStorageFactory $loader): array {
$folders = $this->getFoldersForUser($user);
$mountPoints = array_map(fn (array $folder): string => 'files/' . $folder['mount_point'], $folders);
$conflicts = $this->findConflictsForUser($user, $mountPoints);
$foldersWithAcl = array_filter($folders, fn (array $folder): bool => $folder['acl']);
$aclRootPaths = array_map(fn (array $folder): string => $this->getJailPath($folder['folder_id']), $foldersWithAcl);
$aclManager = $this->aclManagerFactory->getACLManager($user, $this->getRootStorageId());
$rootRules = $aclManager->getRelevantRulesForPath($aclRootPaths);
return array_filter(array_map(function (array $folder) use ($user, $loader, $conflicts, $aclManager, $rootRules): ?IMountPoint {
// check for existing files in the user home and rename them if needed
$originalFolderName = $folder['mount_point'];
if (in_array($originalFolderName, $conflicts)) {
/** @var IStorage $userStorage */
$userStorage = $this->mountProviderCollection->getHomeMountForUser($user)->getStorage();
$userCache = $userStorage->getCache();
$i = 1;
$folderName = $folder['mount_point'] . ' (' . $i++ . ')';
while ($userCache->inCache("files/$folderName")) {
$folderName = $originalFolderName . ' (' . $i++ . ')';
}
$userStorage->rename("files/$originalFolderName", "files/$folderName");
$userCache->move("files/$originalFolderName", "files/$folderName");
$userStorage->getPropagator()->propagateChange("files/$folderName", time());
}
return $this->getMount(
$folder['folder_id'],
'/' . $user->getUID() . '/files/' . $folder['mount_point'],
$folder['permissions'],
$folder['quota'],
$folder['rootCacheEntry'],
$loader,
$folder['acl'],
$user,
$aclManager,
$rootRules,
$folder['root_id'],
);
}, $folders));
}
private function getCurrentUID(): ?string {
try {
// wopi requests are not logged in, instead we need to get the editor user from the access token
if (strpos($this->request->getRawPathInfo(), 'apps/richdocuments/wopi') && class_exists('OCA\Richdocuments\Db\WopiMapper')) {
$wopiMapper = \OCP\Server::get('OCA\Richdocuments\Db\WopiMapper');
$token = $this->request->getParam('access_token');
if ($token) {
$wopi = $wopiMapper->getPathForToken($token);
return $wopi->getEditorUid();
}
}
} catch (\Exception) {
}
$user = $this->userSession->getUser();
return $user ? $user->getUID() : null;
}
public function getMount(
int $id,
string $mountPoint,
int $permissions,
int $quota,
?ICacheEntry $cacheEntry = null,
?IStorageFactory $loader = null,
bool $acl = false,
?IUser $user = null,
?ACLManager $aclManager = null,
array $rootRules = [],
?int $rootId = null,
): ?IMountPoint {
if (!$cacheEntry) {
// trigger folder creation
$folder = $this->getFolder($id);
if ($folder === null) {
return null;
}
$cacheEntry = $this->getRootFolder()->getStorage()->getCache()->get($folder->getId());
if ($cacheEntry === false) {
return null;
}
}
$storage = $this->getRootFolder()->getStorage();
$storage->setOwner($user?->getUID());
$rootPath = $this->getJailPath($id);
// apply acl before jail
if ($acl && $user) {
$inShare = !\OC::$CLI && ($this->getCurrentUID() === null || $this->getCurrentUID() !== $user->getUID());
$aclManager ??= $this->aclManagerFactory->getACLManager($user, $this->getRootStorageId());
$aclRootPermissions = $aclManager->getPermissionsForPathFromRules($rootPath, $rootRules);
$storage = new ACLStorageWrapper([
'storage' => $storage,
'acl_manager' => $aclManager,
'in_share' => $inShare,
]);
$cacheEntry['permissions'] &= $aclRootPermissions;
}
$quotaStorage = $this->getGroupFolderStorage($id, $storage, $user, $rootPath, $quota, $cacheEntry);
$maskedStore = new PermissionsMask([
'storage' => $quotaStorage,
'mask' => $permissions,
]);
if (!$this->allowRootShare) {
$maskedStore = new RootPermissionsMask([
'storage' => $maskedStore,
'mask' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE,
]);
}
return new GroupMountPoint(
$id,
$maskedStore,
$mountPoint,
null,
$loader,
rootId: $rootId,
);
}
public function getTrashMount(
int $id,
string $mountPoint,
int $quota,
IStorageFactory $loader,
IUser $user,
?ICacheEntry $cacheEntry = null,
): IMountPoint {
$storage = $this->getRootFolder()->getStorage();
$storage->setOwner($user->getUID());
$trashPath = $this->getRootFolder()->getInternalPath() . '/trash/' . $id;
$trashStorage = $this->getGroupFolderStorage($id, $storage, $user, $trashPath, $quota, $cacheEntry);
return new GroupMountPoint(
$id,
$trashStorage,
$mountPoint,
null,
$loader
);
}
public function getGroupFolderStorage(
int $id,
IStorage $rootStorage,
?IUser $user,
string $rootPath,
int $quota,
?ICacheEntry $rootCacheEntry,
): IStorage {
if ($this->enableEncryption) {
$baseStorage = new GroupFolderEncryptionJail([
'storage' => $rootStorage,
'root' => $rootPath,
]);
$quotaStorage = new GroupFolderStorage([
'storage' => $baseStorage,
'quota' => $quota,
'folder_id' => $id,
'rootCacheEntry' => $rootCacheEntry,
'userSession' => $this->userSession,
'mountOwner' => $user,
]);
} else {
$baseStorage = new Jail([
'storage' => $rootStorage,
'root' => $rootPath,
]);
$quotaStorage = new GroupFolderNoEncryptionStorage([
'storage' => $baseStorage,
'quota' => $quota,
'folder_id' => $id,
'rootCacheEntry' => $rootCacheEntry,
'userSession' => $this->userSession,
'mountOwner' => $user,
]);
}
return $quotaStorage;
}
public function getJailPath(int $folderId): string {
return $this->getRootFolder()->getInternalPath() . '/' . $folderId;
}
private function getRootFolder(): Folder {
if (is_null($this->root)) {
$rootProvider = $this->rootProvider;
$this->root = $rootProvider();
}
return $this->root;
}
public function getFolder(int $id, bool $create = true): ?Node {
try {
return $this->getRootFolder()->get((string)$id);
} catch (NotFoundException) {
if ($create) {
return $this->getRootFolder()->newFolder((string)$id);
} else {
return null;
}
}
}
/**
* @param string[] $mountPoints
* @return string[] An array of paths.
*/
private function findConflictsForUser(IUser $user, array $mountPoints): array {
$userHome = $this->mountProviderCollection->getHomeMountForUser($user);
$pathHashes = array_map('md5', $mountPoints);
$query = $this->connection->getQueryBuilder();
$query->select('path')
->from('filecache')
->where($query->expr()->eq('storage', $query->createNamedParameter($userHome->getNumericStorageId(), IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->in('path_hash', $query->createParameter('chunk')));
$paths = [];
foreach (array_chunk($pathHashes, 1000) as $chunk) {
$query->setParameter('chunk', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
$paths = array_merge($paths, $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN));
}
return array_map(function (string $path): string {
return substr($path, 6); // strip leading "files/"
}, $paths);
}
}