Completed
Pull Request — master (#11)
by Joas
20:39
created

StorageWrapper::isCreatable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Morris Jobke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\FilesAccessControl;
23
24
use OC\Files\Storage\Wrapper\Wrapper;
25
26
class StorageWrapper extends Wrapper {
27
28
	/** @var Operation */
29
	protected $operation;
30
31
	/** @var string */
32
	public $mountPoint;
33
34
	/**
35
	 * @param array $parameters
36
	 */
37 38
	public function __construct($parameters) {
38 38
		parent::__construct($parameters);
39 38
		$this->operation = $parameters['operation'];
40 38
		$this->mountPoint = $parameters['mountPoint'];
41 38
	}
42
43
	/**
44
	 * @param string $path
45
	 */
46 2
	protected function checkFileAccess($path) {
47 2
		$this->operation->checkFileAccess($this, $path);
48 2
	}
49
50
	/*
51
	 * Storage wrapper methods
52
	 */
53
54
	/**
55
	 * see http://php.net/manual/en/function.mkdir.php
56
	 *
57
	 * @param string $path
58
	 * @return bool
59
	 */
60 4
	public function mkdir($path) {
61 4
		$this->checkFileAccess($path);
62 2
		return $this->storage->mkdir($path);
63
	}
64
65
	/**
66
	 * see http://php.net/manual/en/function.rmdir.php
67
	 *
68
	 * @param string $path
69
	 * @return bool
70
	 */
71 4
	public function rmdir($path) {
72 4
		$this->checkFileAccess($path);
73 2
		return $this->storage->rmdir($path);
74
	}
75
76
//	/**
77
//	 * see http://php.net/manual/en/function.opendir.php
78
//	 *
79
//	 * @param string $path
80
//	 * @return resource
81
//	 */
82
//	public function opendir($path) {
83
//		return $this->storage->opendir($path);
84
//	}
85
//
86
//	/**
87
//	 * see http://php.net/manual/en/function.is_dir.php
88
//	 *
89
//	 * @param string $path
90
//	 * @return bool
91
//	 */
92
//	public function is_dir($path) {
93
//		return $this->storage->is_dir($path);
94
//	}
95
//
96
//	/**
97
//	 * see http://php.net/manual/en/function.is_file.php
98
//	 *
99
//	 * @param string $path
100
//	 * @return bool
101
//	 */
102
//	public function is_file($path) {
103
//		return $this->storage->is_file($path);
104
//	}
105
//
106
//	/**
107
//	 * see http://php.net/manual/en/function.stat.php
108
//	 * only the following keys are required in the result: size and mtime
109
//	 *
110
//	 * @param string $path
111
//	 * @return array
112
//	 */
113
//	public function stat($path) {
114
//		return $this->storage->stat($path);
115
//	}
116
//
117
//	/**
118
//	 * see http://php.net/manual/en/function.filetype.php
119
//	 *
120
//	 * @param string $path
121
//	 * @return bool
122
//	 */
123
//	public function filetype($path) {
124
//		return $this->storage->filetype($path);
125
//	}
126
//
127
//	/**
128
//	 * see http://php.net/manual/en/function.filesize.php
129
//	 * The result for filesize when called on a folder is required to be 0
130
//	 *
131
//	 * @param string $path
132
//	 * @return int
133
//	 */
134
//	public function filesize($path) {
135
//		return $this->storage->filesize($path);
136
//	}
137
138
	/**
139
	 * check if a file can be created in $path
140
	 *
141
	 * @param string $path
142
	 * @return bool
143
	 */
144 4
	public function isCreatable($path) {
145 4
		$this->checkFileAccess($path);
146 2
		return $this->storage->isCreatable($path);
147
	}
148
149
	/**
150
	 * check if a file can be read
151
	 *
152
	 * @param string $path
153
	 * @return bool
154
	 */
155 4
	public function isReadable($path) {
156 4
		$this->checkFileAccess($path);
157 2
		return $this->storage->isReadable($path);
158
	}
159
160
	/**
161
	 * check if a file can be written to
162
	 *
163
	 * @param string $path
164
	 * @return bool
165
	 */
166 4
	public function isUpdatable($path) {
167 4
		$this->checkFileAccess($path);
168 2
		return $this->storage->isUpdatable($path);
169
	}
170
171
	/**
172
	 * check if a file can be deleted
173
	 *
174
	 * @param string $path
175
	 * @return bool
176
	 */
177 4
	public function isDeletable($path) {
178 4
		$this->checkFileAccess($path);
179 2
		return $this->storage->isDeletable($path);
180
	}
181
182
//	/**
183
//	 * check if a file can be shared
184
//	 *
185
//	 * @param string $path
186
//	 * @return bool
187
//	 */
188
//	public function isSharable($path) {
189
//		return $this->storage->isSharable($path);
190
//	}
191
//
192
//	/**
193
//	 * get the full permissions of a path.
194
//	 * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
195
//	 *
196
//	 * @param string $path
197
//	 * @return int
198
//	 */
199
//	public function getPermissions($path) {
200
//		return $this->storage->getPermissions($path);
201
//	}
202
//
203
//	/**
204
//	 * see http://php.net/manual/en/function.file_exists.php
205
//	 *
206
//	 * @param string $path
207
//	 * @return bool
208
//	 */
209
//	public function file_exists($path) {
210
//		return $this->storage->file_exists($path);
211
//	}
212
//
213
//	/**
214
//	 * see http://php.net/manual/en/function.filemtime.php
215
//	 *
216
//	 * @param string $path
217
//	 * @return int
218
//	 */
219
//	public function filemtime($path) {
220
//		return $this->storage->filemtime($path);
221
//	}
222
223
	/**
224
	 * see http://php.net/manual/en/function.file_get_contents.php
225
	 *
226
	 * @param string $path
227
	 * @return string
228
	 */
229 4
	public function file_get_contents($path) {
230 4
		$this->checkFileAccess($path);
231 2
		return $this->storage->file_get_contents($path);
232
	}
233
234
	/**
235
	 * see http://php.net/manual/en/function.file_put_contents.php
236
	 *
237
	 * @param string $path
238
	 * @param string $data
239
	 * @return bool
240
	 */
241
	public function file_put_contents($path, $data) {
242
		$this->checkFileAccess($path);
243
		return $this->storage->file_put_contents($path, $data);
244
	}
245
246
	/**
247
	 * see http://php.net/manual/en/function.unlink.php
248
	 *
249
	 * @param string $path
250
	 * @return bool
251
	 */
252 4
	public function unlink($path) {
253 4
		$this->checkFileAccess($path);
254 2
		return $this->storage->unlink($path);
255
	}
256
257
	/**
258
	 * see http://php.net/manual/en/function.rename.php
259
	 *
260
	 * @param string $path1
261
	 * @param string $path2
262
	 * @return bool
263
	 */
264
	public function rename($path1, $path2) {
265
		$this->checkFileAccess($path1);
266
		$this->checkFileAccess($path2);
267
		return $this->storage->rename($path1, $path2);
268
	}
269
270
	/**
271
	 * see http://php.net/manual/en/function.copy.php
272
	 *
273
	 * @param string $path1
274
	 * @param string $path2
275
	 * @return bool
276
	 */
277
	public function copy($path1, $path2) {
278
		$this->checkFileAccess($path1);
279
		$this->checkFileAccess($path2);
280
		return $this->storage->copy($path1, $path2);
281
	}
282
283
	/**
284
	 * see http://php.net/manual/en/function.fopen.php
285
	 *
286
	 * @param string $path
287
	 * @param string $mode
288
	 * @return resource
289
	 */
290
	public function fopen($path, $mode) {
291
		$this->checkFileAccess($path);
292
		return $this->storage->fopen($path, $mode);
293
	}
294
295
//	/**
296
//	 * get the mimetype for a file or folder
297
//	 * The mimetype for a folder is required to be "httpd/unix-directory"
298
//	 *
299
//	 * @param string $path
300
//	 * @return string
301
//	 */
302
//	public function getMimeType($path) {
303
//		return $this->storage->getMimeType($path);
304
//	}
305
//
306
//	/**
307
//	 * see http://php.net/manual/en/function.hash.php
308
//	 *
309
//	 * @param string $type
310
//	 * @param string $path
311
//	 * @param bool $raw
312
//	 * @return string
313
//	 */
314
//	public function hash($type, $path, $raw = false) {
315
//		return $this->storage->hash($type, $path, $raw);
316
//	}
317
//
318
//	/**
319
//	 * see http://php.net/manual/en/function.free_space.php
320
//	 *
321
//	 * @param string $path
322
//	 * @return int
323
//	 */
324
//	public function free_space($path) {
325
//		return $this->storage->free_space($path);
326
//	}
327
//
328
//	/**
329
//	 * search for occurrences of $query in file names
330
//	 *
331
//	 * @param string $query
332
//	 * @return array
333
//	 */
334
//	public function search($query) {
335
//		return $this->storage->search($query);
336
//	}
337
338
	/**
339
	 * see http://php.net/manual/en/function.touch.php
340
	 * If the backend does not support the operation, false should be returned
341
	 *
342
	 * @param string $path
343
	 * @param int $mtime
344
	 * @return bool
345
	 */
346
	public function touch($path, $mtime = null) {
347
		$this->checkFileAccess($path);
348
		return $this->storage->touch($path, $mtime);
349
	}
350
351
//	/**
352
//	 * get the path to a local version of the file.
353
//	 * The local version of the file can be temporary and doesn't have to be persistent across requests
354
//	 *
355
//	 * @param string $path
356
//	 * @return string
357
//	 */
358
//	public function getLocalFile($path) {
359
//		return $this->storage->getLocalFile($path);
360
//	}
361
//
362
//	/**
363
//	 * check if a file or folder has been updated since $time
364
//	 *
365
//	 * @param string $path
366
//	 * @param int $time
367
//	 * @return bool
368
//	 *
369
//	 * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
370
//	 * returning true for other changes in the folder is optional
371
//	 */
372
//	public function hasUpdated($path, $time) {
373
//		return $this->storage->hasUpdated($path, $time);
374
//	}
375
//
376
//	/**
377
//	 * get a cache instance for the storage
378
//	 *
379
//	 * @param string $path
380
//	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
381
//	 * @return \OC\Files\Cache\Cache
382
//	 */
383
//	public function getCache($path = '', $storage = null) {
384
//		if (!$storage) {
385
//			$storage = $this;
386
//		}
387
//		return $this->storage->getCache($path, $storage);
388
//	}
389
//
390
//	/**
391
//	 * get a scanner instance for the storage
392
//	 *
393
//	 * @param string $path
394
//	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
395
//	 * @return \OC\Files\Cache\Scanner
396
//	 */
397
//	public function getScanner($path = '', $storage = null) {
398
//		if (!$storage) {
399
//			$storage = $this;
400
//		}
401
//		return $this->storage->getScanner($path, $storage);
402
//	}
403
//
404
//
405
//	/**
406
//	 * get the user id of the owner of a file or folder
407
//	 *
408
//	 * @param string $path
409
//	 * @return string
410
//	 */
411
//	public function getOwner($path) {
412
//		return $this->storage->getOwner($path);
413
//	}
414
//
415
//	/**
416
//	 * get a watcher instance for the cache
417
//	 *
418
//	 * @param string $path
419
//	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
420
//	 * @return \OC\Files\Cache\Watcher
421
//	 */
422
//	public function getWatcher($path = '', $storage = null) {
423
//		if (!$storage) {
424
//			$storage = $this;
425
//		}
426
//		return $this->storage->getWatcher($path, $storage);
427
//	}
428
//
429
//	public function getPropagator($storage = null) {
430
//		if (!$storage) {
431
//			$storage = $this;
432
//		}
433
//		return $this->storage->getPropagator($storage);
434
//	}
435
//
436
//	public function getUpdater($storage = null) {
437
//		if (!$storage) {
438
//			$storage = $this;
439
//		}
440
//		return $this->storage->getUpdater($storage);
441
//	}
442
//
443
//	/**
444
//	 * @return \OC\Files\Cache\Storage
445
//	 */
446
//	public function getStorageCache() {
447
//		return $this->storage->getStorageCache();
448
//	}
449
//
450
//	/**
451
//	 * get the ETag for a file or folder
452
//	 *
453
//	 * @param string $path
454
//	 * @return string
455
//	 */
456
//	public function getETag($path) {
457
//		return $this->storage->getETag($path);
458
//	}
459
//
460
//	/**
461
//	 * Returns true
462
//	 *
463
//	 * @return true
464
//	 */
465
//	public function test() {
466
//		return $this->storage->test();
467
//	}
468
//
469
//	/**
470
//	 * Returns the wrapped storage's value for isLocal()
471
//	 *
472
//	 * @return bool wrapped storage's isLocal() value
473
//	 */
474
//	public function isLocal() {
475
//		return $this->storage->isLocal();
476
//	}
477
//
478
//	/**
479
//	 * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class
480
//	 *
481
//	 * @param string $class
482
//	 * @return bool
483
//	 */
484
//	public function instanceOfStorage($class) {
485
//		return is_a($this, $class) or $this->storage->instanceOfStorage($class);
486
//	}
487
//
488
//	/**
489
//	 * Pass any methods custom to specific storage implementations to the wrapped storage
490
//	 *
491
//	 * @param string $method
492
//	 * @param array $args
493
//	 * @return mixed
494
//	 */
495
//	public function __call($method, $args) {
496
//		return call_user_func_array(array($this->storage, $method), $args);
497
//	}
498
499
	/**
500
	 * A custom storage implementation can return an url for direct download of a give file.
501
	 *
502
	 * For now the returned array can hold the parameter url - in future more attributes might follow.
503
	 *
504
	 * @param string $path
505
	 * @return array
506
	 */
507 4
	public function getDirectDownload($path) {
508 4
		$this->checkFileAccess($path);
509 2
		return $this->storage->getDirectDownload($path);
510
	}
511
512
//	/**
513
//	 * Get availability of the storage
514
//	 *
515
//	 * @return array [ available, last_checked ]
516
//	 */
517
//	public function getAvailability() {
518
//		return $this->storage->getAvailability();
519
//	}
520
//
521
//	/**
522
//	 * Set availability of the storage
523
//	 *
524
//	 * @param bool $isAvailable
525
//	 */
526
//	public function setAvailability($isAvailable) {
527
//		$this->storage->setAvailability($isAvailable);
528
//	}
529
//
530
//	/**
531
//	 * @param string $path the path of the target folder
532
//	 * @param string $fileName the name of the file itself
533
//	 * @return void
534
//	 * @throws InvalidPathException
535
//	 */
536
//	public function verifyPath($path, $fileName) {
537
//		$this->storage->verifyPath($path, $fileName);
538
//	}
539
540
	/**
541
	 * @param \OCP\Files\Storage $sourceStorage
542
	 * @param string $sourceInternalPath
543
	 * @param string $targetInternalPath
544
	 * @return bool
545
	 */
546
	public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
547
		if ($sourceStorage === $this) {
548
			return $this->copy($sourceInternalPath, $targetInternalPath);
549
		}
550
551
		$this->checkFileAccess($targetInternalPath);
552
		return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
553
	}
554
555
	/**
556
	 * @param \OCP\Files\Storage $sourceStorage
557
	 * @param string $sourceInternalPath
558
	 * @param string $targetInternalPath
559
	 * @return bool
560
	 */
561
	public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
562
		if ($sourceStorage === $this) {
563
			return $this->rename($sourceInternalPath, $targetInternalPath);
564
		}
565
566
		$this->checkFileAccess($targetInternalPath);
567
		return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
568
	}
569
570
//	/**
571
//	 * @param string $path
572
//	 * @return array
573
//	 */
574
//	public function getMetaData($path) {
575
//		return $this->storage->getMetaData($path);
576
//	}
577
//
578
//	/**
579
//	 * @param string $path
580
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
581
//	 * @param \OCP\Lock\ILockingProvider $provider
582
//	 * @throws \OCP\Lock\LockedException
583
//	 */
584
//	public function acquireLock($path, $type, ILockingProvider $provider) {
585
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
586
//			$this->storage->acquireLock($path, $type, $provider);
587
//		}
588
//	}
589
//
590
//	/**
591
//	 * @param string $path
592
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
593
//	 * @param \OCP\Lock\ILockingProvider $provider
594
//	 */
595
//	public function releaseLock($path, $type, ILockingProvider $provider) {
596
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
597
//			$this->storage->releaseLock($path, $type, $provider);
598
//		}
599
//	}
600
//
601
//	/**
602
//	 * @param string $path
603
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
604
//	 * @param \OCP\Lock\ILockingProvider $provider
605
//	 */
606
//	public function changeLock($path, $type, ILockingProvider $provider) {
607
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
608
//			$this->storage->changeLock($path, $type, $provider);
609
//		}
610
//	}
611
}
612