Completed
Pull Request — master (#31)
by Joas
08:24
created

StorageWrapper::isCreatable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
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
		try {
146 4
			$this->checkFileAccess($path);
147 4
		} catch (\OCP\Files\ForbiddenException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\ForbiddenException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
148 2
			return false;
149
		}
150 2
		return $this->storage->isCreatable($path);
151
	}
152
153
	/**
154
	 * check if a file can be read
155
	 *
156
	 * @param string $path
157
	 * @return bool
158
	 */
159 4
	public function isReadable($path) {
160
		try {
161 4
			$this->checkFileAccess($path);
162 4
		} catch (\OCP\Files\ForbiddenException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\ForbiddenException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
163 2
			return false;
164
		}
165 2
		return $this->storage->isReadable($path);
166
	}
167
168
	/**
169
	 * check if a file can be written to
170
	 *
171
	 * @param string $path
172
	 * @return bool
173
	 */
174 4
	public function isUpdatable($path) {
175
		try {
176 4
			$this->checkFileAccess($path);
177 4
		} catch (\OCP\Files\ForbiddenException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\ForbiddenException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
178 2
			return false;
179
		}
180 2
		return $this->storage->isUpdatable($path);
181
	}
182
183
	/**
184
	 * check if a file can be deleted
185
	 *
186
	 * @param string $path
187
	 * @return bool
188
	 */
189 4
	public function isDeletable($path) {
190
		try {
191 4
			$this->checkFileAccess($path);
192 4
		} catch (\OCP\Files\ForbiddenException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\ForbiddenException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
193 2
			return false;
194
		}
195 2
		return $this->storage->isDeletable($path);
196
	}
197
198
//	/**
199
//	 * check if a file can be shared
200
//	 *
201
//	 * @param string $path
202
//	 * @return bool
203
//	 */
204
//	public function isSharable($path) {
205
//		return $this->storage->isSharable($path);
206
//	}
207
//
208
//	/**
209
//	 * get the full permissions of a path.
210
//	 * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
211
//	 *
212
//	 * @param string $path
213
//	 * @return int
214
//	 */
215
//	public function getPermissions($path) {
216
//		return $this->storage->getPermissions($path);
217
//	}
218
//
219
//	/**
220
//	 * see http://php.net/manual/en/function.file_exists.php
221
//	 *
222
//	 * @param string $path
223
//	 * @return bool
224
//	 */
225
//	public function file_exists($path) {
226
//		return $this->storage->file_exists($path);
227
//	}
228
//
229
//	/**
230
//	 * see http://php.net/manual/en/function.filemtime.php
231
//	 *
232
//	 * @param string $path
233
//	 * @return int
234
//	 */
235
//	public function filemtime($path) {
236
//		return $this->storage->filemtime($path);
237
//	}
238
239
	/**
240
	 * see http://php.net/manual/en/function.file_get_contents.php
241
	 *
242
	 * @param string $path
243
	 * @return string
244
	 */
245 4
	public function file_get_contents($path) {
246 4
		$this->checkFileAccess($path);
247 2
		return $this->storage->file_get_contents($path);
248
	}
249
250
	/**
251
	 * see http://php.net/manual/en/function.file_put_contents.php
252
	 *
253
	 * @param string $path
254
	 * @param string $data
255
	 * @return bool
256
	 */
257
	public function file_put_contents($path, $data) {
258
		$this->checkFileAccess($path);
259
		return $this->storage->file_put_contents($path, $data);
260
	}
261
262
	/**
263
	 * see http://php.net/manual/en/function.unlink.php
264
	 *
265
	 * @param string $path
266
	 * @return bool
267
	 */
268 4
	public function unlink($path) {
269 4
		$this->checkFileAccess($path);
270 2
		return $this->storage->unlink($path);
271
	}
272
273
	/**
274
	 * see http://php.net/manual/en/function.rename.php
275
	 *
276
	 * @param string $path1
277
	 * @param string $path2
278
	 * @return bool
279
	 */
280
	public function rename($path1, $path2) {
281
		$this->checkFileAccess($path1);
282
		$this->checkFileAccess($path2);
283
		return $this->storage->rename($path1, $path2);
284
	}
285
286
	/**
287
	 * see http://php.net/manual/en/function.copy.php
288
	 *
289
	 * @param string $path1
290
	 * @param string $path2
291
	 * @return bool
292
	 */
293
	public function copy($path1, $path2) {
294
		$this->checkFileAccess($path1);
295
		$this->checkFileAccess($path2);
296
		return $this->storage->copy($path1, $path2);
297
	}
298
299
	/**
300
	 * see http://php.net/manual/en/function.fopen.php
301
	 *
302
	 * @param string $path
303
	 * @param string $mode
304
	 * @return resource
305
	 */
306
	public function fopen($path, $mode) {
307
		$this->checkFileAccess($path);
308
		return $this->storage->fopen($path, $mode);
309
	}
310
311
//	/**
312
//	 * get the mimetype for a file or folder
313
//	 * The mimetype for a folder is required to be "httpd/unix-directory"
314
//	 *
315
//	 * @param string $path
316
//	 * @return string
317
//	 */
318
//	public function getMimeType($path) {
319
//		return $this->storage->getMimeType($path);
320
//	}
321
//
322
//	/**
323
//	 * see http://php.net/manual/en/function.hash.php
324
//	 *
325
//	 * @param string $type
326
//	 * @param string $path
327
//	 * @param bool $raw
328
//	 * @return string
329
//	 */
330
//	public function hash($type, $path, $raw = false) {
331
//		return $this->storage->hash($type, $path, $raw);
332
//	}
333
//
334
//	/**
335
//	 * see http://php.net/manual/en/function.free_space.php
336
//	 *
337
//	 * @param string $path
338
//	 * @return int
339
//	 */
340
//	public function free_space($path) {
341
//		return $this->storage->free_space($path);
342
//	}
343
//
344
//	/**
345
//	 * search for occurrences of $query in file names
346
//	 *
347
//	 * @param string $query
348
//	 * @return array
349
//	 */
350
//	public function search($query) {
351
//		return $this->storage->search($query);
352
//	}
353
354
	/**
355
	 * see http://php.net/manual/en/function.touch.php
356
	 * If the backend does not support the operation, false should be returned
357
	 *
358
	 * @param string $path
359
	 * @param int $mtime
360
	 * @return bool
361
	 */
362
	public function touch($path, $mtime = null) {
363
		$this->checkFileAccess($path);
364
		return $this->storage->touch($path, $mtime);
365
	}
366
367
//	/**
368
//	 * get the path to a local version of the file.
369
//	 * The local version of the file can be temporary and doesn't have to be persistent across requests
370
//	 *
371
//	 * @param string $path
372
//	 * @return string
373
//	 */
374
//	public function getLocalFile($path) {
375
//		return $this->storage->getLocalFile($path);
376
//	}
377
//
378
//	/**
379
//	 * check if a file or folder has been updated since $time
380
//	 *
381
//	 * @param string $path
382
//	 * @param int $time
383
//	 * @return bool
384
//	 *
385
//	 * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
386
//	 * returning true for other changes in the folder is optional
387
//	 */
388
//	public function hasUpdated($path, $time) {
389
//		return $this->storage->hasUpdated($path, $time);
390
//	}
391
//
392
//	/**
393
//	 * get a cache instance for the storage
394
//	 *
395
//	 * @param string $path
396
//	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
397
//	 * @return \OC\Files\Cache\Cache
398
//	 */
399
//	public function getCache($path = '', $storage = null) {
400
//		if (!$storage) {
401
//			$storage = $this;
402
//		}
403
//		return $this->storage->getCache($path, $storage);
404
//	}
405
//
406
//	/**
407
//	 * get a scanner instance for the storage
408
//	 *
409
//	 * @param string $path
410
//	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
411
//	 * @return \OC\Files\Cache\Scanner
412
//	 */
413
//	public function getScanner($path = '', $storage = null) {
414
//		if (!$storage) {
415
//			$storage = $this;
416
//		}
417
//		return $this->storage->getScanner($path, $storage);
418
//	}
419
//
420
//
421
//	/**
422
//	 * get the user id of the owner of a file or folder
423
//	 *
424
//	 * @param string $path
425
//	 * @return string
426
//	 */
427
//	public function getOwner($path) {
428
//		return $this->storage->getOwner($path);
429
//	}
430
//
431
//	/**
432
//	 * get a watcher instance for the cache
433
//	 *
434
//	 * @param string $path
435
//	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
436
//	 * @return \OC\Files\Cache\Watcher
437
//	 */
438
//	public function getWatcher($path = '', $storage = null) {
439
//		if (!$storage) {
440
//			$storage = $this;
441
//		}
442
//		return $this->storage->getWatcher($path, $storage);
443
//	}
444
//
445
//	public function getPropagator($storage = null) {
446
//		if (!$storage) {
447
//			$storage = $this;
448
//		}
449
//		return $this->storage->getPropagator($storage);
450
//	}
451
//
452
//	public function getUpdater($storage = null) {
453
//		if (!$storage) {
454
//			$storage = $this;
455
//		}
456
//		return $this->storage->getUpdater($storage);
457
//	}
458
//
459
//	/**
460
//	 * @return \OC\Files\Cache\Storage
461
//	 */
462
//	public function getStorageCache() {
463
//		return $this->storage->getStorageCache();
464
//	}
465
//
466
//	/**
467
//	 * get the ETag for a file or folder
468
//	 *
469
//	 * @param string $path
470
//	 * @return string
471
//	 */
472
//	public function getETag($path) {
473
//		return $this->storage->getETag($path);
474
//	}
475
//
476
//	/**
477
//	 * Returns true
478
//	 *
479
//	 * @return true
480
//	 */
481
//	public function test() {
482
//		return $this->storage->test();
483
//	}
484
//
485
//	/**
486
//	 * Returns the wrapped storage's value for isLocal()
487
//	 *
488
//	 * @return bool wrapped storage's isLocal() value
489
//	 */
490
//	public function isLocal() {
491
//		return $this->storage->isLocal();
492
//	}
493
//
494
//	/**
495
//	 * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class
496
//	 *
497
//	 * @param string $class
498
//	 * @return bool
499
//	 */
500
//	public function instanceOfStorage($class) {
501
//		return is_a($this, $class) or $this->storage->instanceOfStorage($class);
502
//	}
503
//
504
//	/**
505
//	 * Pass any methods custom to specific storage implementations to the wrapped storage
506
//	 *
507
//	 * @param string $method
508
//	 * @param array $args
509
//	 * @return mixed
510
//	 */
511
//	public function __call($method, $args) {
512
//		return call_user_func_array(array($this->storage, $method), $args);
513
//	}
514
515
	/**
516
	 * A custom storage implementation can return an url for direct download of a give file.
517
	 *
518
	 * For now the returned array can hold the parameter url - in future more attributes might follow.
519
	 *
520
	 * @param string $path
521
	 * @return array
522
	 */
523 4
	public function getDirectDownload($path) {
524 4
		$this->checkFileAccess($path);
525 2
		return $this->storage->getDirectDownload($path);
526
	}
527
528
//	/**
529
//	 * Get availability of the storage
530
//	 *
531
//	 * @return array [ available, last_checked ]
532
//	 */
533
//	public function getAvailability() {
534
//		return $this->storage->getAvailability();
535
//	}
536
//
537
//	/**
538
//	 * Set availability of the storage
539
//	 *
540
//	 * @param bool $isAvailable
541
//	 */
542
//	public function setAvailability($isAvailable) {
543
//		$this->storage->setAvailability($isAvailable);
544
//	}
545
//
546
//	/**
547
//	 * @param string $path the path of the target folder
548
//	 * @param string $fileName the name of the file itself
549
//	 * @return void
550
//	 * @throws InvalidPathException
551
//	 */
552
//	public function verifyPath($path, $fileName) {
553
//		$this->storage->verifyPath($path, $fileName);
554
//	}
555
556
	/**
557
	 * @param \OCP\Files\Storage $sourceStorage
558
	 * @param string $sourceInternalPath
559
	 * @param string $targetInternalPath
560
	 * @return bool
561
	 */
562
	public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
563
		if ($sourceStorage === $this) {
564
			return $this->copy($sourceInternalPath, $targetInternalPath);
565
		}
566
567
		$this->checkFileAccess($targetInternalPath);
568
		return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
569
	}
570
571
	/**
572
	 * @param \OCP\Files\Storage $sourceStorage
573
	 * @param string $sourceInternalPath
574
	 * @param string $targetInternalPath
575
	 * @return bool
576
	 */
577
	public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
578
		if ($sourceStorage === $this) {
579
			return $this->rename($sourceInternalPath, $targetInternalPath);
580
		}
581
582
		$this->checkFileAccess($targetInternalPath);
583
		return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
584
	}
585
586
//	/**
587
//	 * @param string $path
588
//	 * @return array
589
//	 */
590
//	public function getMetaData($path) {
591
//		return $this->storage->getMetaData($path);
592
//	}
593
//
594
//	/**
595
//	 * @param string $path
596
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
597
//	 * @param \OCP\Lock\ILockingProvider $provider
598
//	 * @throws \OCP\Lock\LockedException
599
//	 */
600
//	public function acquireLock($path, $type, ILockingProvider $provider) {
601
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
602
//			$this->storage->acquireLock($path, $type, $provider);
603
//		}
604
//	}
605
//
606
//	/**
607
//	 * @param string $path
608
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
609
//	 * @param \OCP\Lock\ILockingProvider $provider
610
//	 */
611
//	public function releaseLock($path, $type, ILockingProvider $provider) {
612
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
613
//			$this->storage->releaseLock($path, $type, $provider);
614
//		}
615
//	}
616
//
617
//	/**
618
//	 * @param string $path
619
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
620
//	 * @param \OCP\Lock\ILockingProvider $provider
621
//	 */
622
//	public function changeLock($path, $type, ILockingProvider $provider) {
623
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
624
//			$this->storage->changeLock($path, $type, $provider);
625
//		}
626
//	}
627
}
628