-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathStorageWrapper.php
More file actions
396 lines (357 loc) · 11.1 KB
/
Copy pathStorageWrapper.php
File metadata and controls
396 lines (357 loc) · 11.1 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\FilesAccessControl;
use OC\Files\Storage\Storage;
use OC\Files\Storage\Wrapper\Wrapper;
use OCP\Constants;
use OCP\Files\Cache\ICache;
use OCP\Files\ForbiddenException;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IWriteStreamStorage;
class StorageWrapper extends Wrapper implements IWriteStreamStorage {
protected readonly Operation $operation;
public readonly string $mountPoint;
protected readonly int $mask;
protected readonly IMountPoint $mount;
/**
* @param array $parameters
*/
public function __construct($parameters) {
parent::__construct($parameters);
$this->operation = $parameters['operation'];
$this->mountPoint = $parameters['mountPoint'];
$this->mount = $parameters['mount'];
$this->mask = Constants::PERMISSION_ALL
& ~Constants::PERMISSION_READ
& ~Constants::PERMISSION_CREATE
& ~Constants::PERMISSION_UPDATE
& ~Constants::PERMISSION_DELETE;
}
/**
* @see Operation::checkFileAccess()
* @throws ForbiddenException
*/
protected function checkFileAccess(string $path, ?bool $isDir = null, ?int $permissions = null): ?int {
return $this->operation->checkFileAccess($path, $this->mount, is_bool($isDir) ? $isDir : $this->is_dir($path), null, $permissions ?? Constants::PERMISSION_ALL);
}
/*
* Storage wrapper methods
*/
/**
* see http://php.net/manual/en/function.mkdir.php
*
* @param string $path
* @return bool
* @throws ForbiddenException
*/
#[\Override]
public function mkdir($path): bool {
$this->checkFileAccess($path, true, Constants::PERMISSION_CREATE);
return $this->storage->mkdir($path);
}
/**
* see http://php.net/manual/en/function.rmdir.php
*
* @param string $path
* @return bool
* @throws ForbiddenException
*/
#[\Override]
public function rmdir($path): bool {
$this->checkFileAccess($path, true, Constants::PERMISSION_DELETE);
return $this->storage->rmdir($path);
}
/**
* check if a file can be created in $path
*
* @param string $path
* @return bool
*/
#[\Override]
public function isCreatable($path): bool {
try {
$this->checkFileAccess($path, null, Constants::PERMISSION_CREATE);
} catch (ForbiddenException) {
return false;
}
return $this->storage->isCreatable($path);
}
/**
* check if a file can be read
*
* @param string $path
* @return bool
*/
#[\Override]
public function isReadable($path): bool {
try {
$this->checkFileAccess($path, null, Constants::PERMISSION_READ);
} catch (ForbiddenException) {
return false;
}
return $this->storage->isReadable($path);
}
/**
* check if a file can be written to
*
* @param string $path
* @return bool
*/
#[\Override]
public function isUpdatable($path): bool {
try {
$this->checkFileAccess($path, null, Constants::PERMISSION_UPDATE);
} catch (ForbiddenException) {
return false;
}
return $this->storage->isUpdatable($path);
}
/**
* check if a file can be deleted
*
* @param string $path
* @return bool
*/
#[\Override]
public function isDeletable($path): bool {
try {
$this->checkFileAccess($path, null, Constants::PERMISSION_DELETE);
} catch (ForbiddenException) {
return false;
}
return $this->storage->isDeletable($path);
}
#[\Override]
public function getPermissions($path): int {
try {
$permissions = $this->checkFileAccess($path, null, 0);
} catch (ForbiddenException) {
return $this->mask;
}
if ($permissions !== null) {
// override with permissions granted by the operation, if any
return $permissions & $this->storage->getPermissions($path);
}
return $this->storage->getPermissions($path);
}
/**
* see http://php.net/manual/en/function.file_get_contents.php
*
* @param string $path
* @return string|false
* @throws ForbiddenException
*/
#[\Override]
public function file_get_contents($path): string|false {
$this->checkFileAccess($path, false, Constants::PERMISSION_READ);
return $this->storage->file_get_contents($path);
}
/**
* see http://php.net/manual/en/function.file_put_contents.php
*
* @param string $path
* @param mixed $data
* @return int|float|false
* @throws ForbiddenException
*/
#[\Override]
public function file_put_contents(string $path, mixed $data): int|float|false {
$this->checkFileAccess($path, false, Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE);
return $this->storage->file_put_contents($path, $data);
}
/**
* see http://php.net/manual/en/function.unlink.php
*
* @param string $path
* @return bool
* @throws ForbiddenException
*/
#[\Override]
public function unlink($path): bool {
$this->checkFileAccess($path, false, Constants::PERMISSION_DELETE);
return $this->storage->unlink($path);
}
/**
* see http://php.net/manual/en/function.rename.php
*
* @param string $source
* @param string $target
* @return bool
* @throws ForbiddenException
*/
#[\Override]
public function rename($source, $target): bool {
$isDir = $this->is_dir($source);
$this->checkFileAccess($source, $isDir, Constants::PERMISSION_READ | Constants::PERMISSION_DELETE);
$this->checkFileAccess($target, $isDir, Constants::PERMISSION_CREATE);
return $this->storage->rename($source, $target);
}
/**
* see http://php.net/manual/en/function.copy.php
*
* @param string $path1
* @param string $path2
* @return bool
* @throws ForbiddenException
*/
#[\Override]
public function copy($source, $target): bool {
$isDir = $this->is_dir($source);
$this->checkFileAccess($source, $isDir, Constants::PERMISSION_READ);
$this->checkFileAccess($target, $isDir, Constants::PERMISSION_CREATE);
return $this->storage->copy($source, $target);
}
/**
* see http://php.net/manual/en/function.fopen.php
*
* @param string $path
* @param string $mode
* @return resource|false
* @throws ForbiddenException
*/
#[\Override]
public function fopen($path, $mode) {
$hasPlus = str_contains($mode, '+');
$isRead = str_contains($mode, 'r');
$isExclusive = str_contains($mode, 'x');
$isWac = str_contains($mode, 'w') || str_contains($mode, 'a') || str_contains($mode, 'c');
$checkPermissions = match (true) {
$isWac => Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | ($hasPlus ? Constants::PERMISSION_READ : 0),
$isExclusive => Constants::PERMISSION_CREATE | ($hasPlus ? Constants::PERMISSION_READ : 0),
$isRead => Constants::PERMISSION_READ | ($hasPlus ? Constants::PERMISSION_UPDATE : 0),
default => Constants::PERMISSION_ALL,
};
$this->checkFileAccess($path, false, $checkPermissions);
return $this->storage->fopen($path, $mode);
}
/**
* see http://php.net/manual/en/function.touch.php
* If the backend does not support the operation, false should be returned
*
* @param string $path
* @param int $mtime
* @return bool
* @throws ForbiddenException
*/
#[\Override]
public function touch($path, $mtime = null): bool {
$this->checkFileAccess($path, false, Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE);
return $this->storage->touch($path, $mtime);
}
/**
* get a cache instance for the storage
*
* @param string $path
* @param Storage (optional) the storage to pass to the cache
* @return ICache
*/
#[\Override]
public function getCache($path = '', $storage = null): ICache {
if (!$storage) {
$storage = $this;
}
$cache = $this->storage->getCache($path, $storage);
return new CacheWrapper($cache, $this->mount, $this->operation);
}
/**
* A custom storage implementation can return a url for direct download of a give file.
*
* For now the returned array can hold the parameter url - in future more attributes might follow.
*
* @param string $path
* @return array{expiration: ?int, url: ?string}|false
* @throws ForbiddenException
*/
#[\Override]
public function getDirectDownload($path): array|false {
$this->checkFileAccess($path, false, Constants::PERMISSION_READ);
return $this->storage->getDirectDownload($path);
}
/**
* A custom storage implementation can return a url for direct download of a give file.
*
* For now the returned array can hold the parameter url - in future more attributes might follow.
*
* @param string $fileId
* @return array{expiration: ?int, url: ?string}|false
* @throws ForbiddenException
*/
#[\Override]
public function getDirectDownloadById(string $fileId): array|false {
// We first check if something is providing direct download, to save ID to path resolution
$data = $this->storage->getDirectDownloadById($fileId);
if ($data === false) {
return false;
}
// We would have actually a result, so lets see if the user should be able to access it
$path = $this->getCache()->getPathById((int)$fileId);
if ($path !== null) {
$this->checkFileAccess($path, false, Constants::PERMISSION_READ);
}
return $data;
}
/**
* @param IStorage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool
* @throws ForbiddenException
*/
#[\Override]
public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath): bool {
if ($sourceStorage === $this) {
return $this->copy($sourceInternalPath, $targetInternalPath);
}
$this->checkFileAccess($targetInternalPath, $sourceStorage->is_dir($sourceInternalPath), Constants::PERMISSION_CREATE);
return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
/**
* @param IStorage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool
* @throws ForbiddenException
*/
#[\Override]
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath): bool {
if ($sourceStorage === $this) {
return $this->rename($sourceInternalPath, $targetInternalPath);
}
$this->checkFileAccess($targetInternalPath, $sourceStorage->is_dir($sourceInternalPath), Constants::PERMISSION_CREATE);
return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
/**
* @throws ForbiddenException
*/
#[\Override]
public function writeStream(string $path, $stream, ?int $size = null): int {
if (!$this->isPartFile($path)) {
$this->checkFileAccess($path, false, Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE);
}
$result = parent::writeStream($path, $stream, $size);
if (!$this->isPartFile($path)) {
return $result;
}
// Required for object storage since part file is not in the storage so we cannot check it before moving it to the storage
// As an alternative we might be able to check on the cache update/insert/delete though the Cache wrapper
try {
$this->checkFileAccess($path, false, Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE);
} catch (\Exception $e) {
$this->storage->unlink($path);
throw $e;
}
return $result;
}
private function isPartFile(string $path): bool {
$extension = pathinfo($path, PATHINFO_EXTENSION);
return $extension === 'part';
}
public function getMount(): IMountPoint {
return $this->mount;
}
}