StorageWrapper   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 296
Duplicated Lines 0 %

Test Coverage

Coverage 70.3%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
eloc 79
c 9
b 0
f 0
dl 0
loc 296
ccs 45
cts 64
cp 0.703
rs 10
wmc 30

21 Methods

Rating   Name   Duplication   Size   Complexity  
A rmdir() 0 3 1
A getDirectDownload() 0 3 1
A touch() 0 3 1
A isDeletable() 0 7 2
A isUpdatable() 0 7 2
A getPermissions() 0 7 2
A isCreatable() 0 7 2
A writeStream() 0 11 2
A checkFileAccess() 0 2 1
A __construct() 0 10 1
A mkdir() 0 3 1
A copy() 0 4 1
A file_get_contents() 0 3 1
A copyFromStorage() 0 7 2
A file_put_contents() 0 3 1
A isReadable() 0 7 2
A fopen() 0 3 1
A rename() 0 4 1
A moveFromStorage() 0 7 2
A unlink() 0 3 1
A getCache() 0 6 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\Cache\Cache;
0 ignored issues
show
Bug introduced by
The type OC\Files\Cache\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use OC\Files\Storage\Storage;
0 ignored issues
show
Bug introduced by
The type OC\Files\Storage\Storage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use OC\Files\Storage\Wrapper\Wrapper;
0 ignored issues
show
Bug introduced by
The type OC\Files\Storage\Wrapper\Wrapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use OCP\Constants;
28
use OCP\Files\ForbiddenException;
29
use OCP\Files\Storage\IStorage;
30
use OCP\Files\Storage\IWriteStreamStorage;
31
32
class StorageWrapper extends Wrapper implements IWriteStreamStorage {
33
34
	/** @var Operation */
35
	protected $operation;
36
37
	/** @var string */
38
	public $mountPoint;
39
	/** @var int */
40
	protected $mask;
41
42
	/**
43 38
	 * @param array $parameters
44 38
	 */
45 38
	public function __construct($parameters) {
46 38
		parent::__construct($parameters);
47
		$this->operation = $parameters['operation'];
48 38
		$this->mountPoint = $parameters['mountPoint'];
49 38
50 38
		$this->mask = Constants::PERMISSION_ALL;
51 38
		$this->mask &= ~Constants::PERMISSION_READ;
52 38
		$this->mask &= ~Constants::PERMISSION_CREATE;
53 38
		$this->mask &= ~Constants::PERMISSION_UPDATE;
54
		$this->mask &= ~Constants::PERMISSION_DELETE;
55
	}
56
57
	/**
58
	 * @throws ForbiddenException
59 2
	 */
60 2
	protected function checkFileAccess(string $path, bool $isDir = false): void {
61 2
		$this->operation->checkFileAccess($this, $path, $isDir);
62
	}
63
64
	/*
65
	 * Storage wrapper methods
66
	 */
67
68
	/**
69
	 * see http://php.net/manual/en/function.mkdir.php
70
	 *
71
	 * @param string $path
72
	 * @return bool
73 4
	 * @throws ForbiddenException
74 4
	 */
75 2
	public function mkdir($path) {
76
		$this->checkFileAccess($path, true);
77
		return $this->storage->mkdir($path);
78
	}
79
80
	/**
81
	 * see http://php.net/manual/en/function.rmdir.php
82
	 *
83
	 * @param string $path
84 4
	 * @return bool
85 4
	 * @throws ForbiddenException
86 2
	 */
87
	public function rmdir($path) {
88
		$this->checkFileAccess($path, true);
89
		return $this->storage->rmdir($path);
90
	}
91
92
	/**
93
	 * check if a file can be created in $path
94
	 *
95
	 * @param string $path
96
	 * @return bool
97
	 */
98
	public function isCreatable($path) {
99
		try {
100
			$this->checkFileAccess($path);
101
		} catch (ForbiddenException $e) {
102
			return false;
103
		}
104
		return $this->storage->isCreatable($path);
105
	}
106
107
	/**
108
	 * check if a file can be read
109
	 *
110
	 * @param string $path
111
	 * @return bool
112
	 */
113
	public function isReadable($path) {
114
		try {
115
			$this->checkFileAccess($path);
116
		} catch (ForbiddenException $e) {
117
			return false;
118
		}
119
		return $this->storage->isReadable($path);
120
	}
121
122
	/**
123
	 * check if a file can be written to
124
	 *
125
	 * @param string $path
126
	 * @return bool
127
	 */
128
	public function isUpdatable($path) {
129
		try {
130
			$this->checkFileAccess($path);
131
		} catch (ForbiddenException $e) {
132
			return false;
133
		}
134
		return $this->storage->isUpdatable($path);
135
	}
136
137
	/**
138
	 * check if a file can be deleted
139
	 *
140
	 * @param string $path
141
	 * @return bool
142
	 */
143
	public function isDeletable($path) {
144
		try {
145
			$this->checkFileAccess($path);
146
		} catch (ForbiddenException $e) {
147
			return false;
148
		}
149
		return $this->storage->isDeletable($path);
150
	}
151
152
	public function getPermissions($path) {
153
		try {
154
			$this->checkFileAccess($path);
155
		} catch (ForbiddenException $e) {
156
			return $this->mask;
157 4
		}
158
		return $this->storage->getPermissions($path);
159 4
	}
160 2
161 2
	/**
162
	 * see http://php.net/manual/en/function.file_get_contents.php
163 2
	 *
164
	 * @param string $path
165
	 * @return string
166
	 * @throws ForbiddenException
167
	 */
168
	public function file_get_contents($path) {
169
		$this->checkFileAccess($path);
170
		return $this->storage->file_get_contents($path);
171
	}
172 4
173
	/**
174 4
	 * see http://php.net/manual/en/function.file_put_contents.php
175 2
	 *
176 2
	 * @param string $path
177
	 * @param string $data
178 2
	 * @return bool
179
	 * @throws ForbiddenException
180
	 */
181
	public function file_put_contents($path, $data) {
182
		$this->checkFileAccess($path);
183
		return $this->storage->file_put_contents($path, $data);
184
	}
185
186
	/**
187 4
	 * see http://php.net/manual/en/function.unlink.php
188
	 *
189 4
	 * @param string $path
190 2
	 * @return bool
191 2
	 * @throws ForbiddenException
192
	 */
193 2
	public function unlink($path) {
194
		$this->checkFileAccess($path);
195
		return $this->storage->unlink($path);
196
	}
197
198
	/**
199
	 * see http://php.net/manual/en/function.rename.php
200
	 *
201
	 * @param string $path1
202 4
	 * @param string $path2
203
	 * @return bool
204 4
	 * @throws ForbiddenException
205 2
	 */
206 2
	public function rename($path1, $path2) {
207
		$this->checkFileAccess($path1);
208 2
		$this->checkFileAccess($path2);
209
		return $this->storage->rename($path1, $path2);
210
	}
211
212
	/**
213
	 * see http://php.net/manual/en/function.copy.php
214
	 *
215
	 * @param string $path1
216
	 * @param string $path2
217
	 * @return bool
218
	 * @throws ForbiddenException
219
	 */
220
	public function copy($path1, $path2) {
221
		$this->checkFileAccess($path1);
222
		$this->checkFileAccess($path2);
223
		return $this->storage->copy($path1, $path2);
224
	}
225
226
	/**
227
	 * see http://php.net/manual/en/function.fopen.php
228
	 *
229
	 * @param string $path
230
	 * @param string $mode
231
	 * @return resource
232
	 * @throws ForbiddenException
233
	 */
234
	public function fopen($path, $mode) {
235
		$this->checkFileAccess($path);
236
		return $this->storage->fopen($path, $mode);
237
	}
238
239
	/**
240
	 * see http://php.net/manual/en/function.touch.php
241
	 * If the backend does not support the operation, false should be returned
242
	 *
243
	 * @param string $path
244
	 * @param int $mtime
245
	 * @return bool
246
	 * @throws ForbiddenException
247
	 */
248
	public function touch($path, $mtime = null) {
249
		$this->checkFileAccess($path);
250
		return $this->storage->touch($path, $mtime);
251
	}
252
253
	/**
254
	 * get a cache instance for the storage
255
	 *
256
	 * @param string $path
257
	 * @param Storage (optional) the storage to pass to the cache
0 ignored issues
show
Bug introduced by
The type OCA\FilesAccessControl\optional was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
258
	 * @return Cache
259
	 */
260
	public function getCache($path = '', $storage = null) {
261
		if (!$storage) {
262
			$storage = $this;
263 4
		}
264 4
		$cache = $this->storage->getCache($path, $storage);
265 2
		return new CacheWrapper($cache, $storage, $this->operation);
266
	}
267
268
	/**
269
	 * A custom storage implementation can return an url for direct download of a give file.
270
	 *
271
	 * For now the returned array can hold the parameter url - in future more attributes might follow.
272
	 *
273
	 * @param string $path
274
	 * @return array
275
	 * @throws ForbiddenException
276
	 */
277
	public function getDirectDownload($path) {
278
		$this->checkFileAccess($path);
279
		return $this->storage->getDirectDownload($path);
280
	}
281
282
	/**
283
	 * @param IStorage $sourceStorage
284
	 * @param string $sourceInternalPath
285
	 * @param string $targetInternalPath
286 4
	 * @return bool
287 4
	 * @throws ForbiddenException
288 2
	 */
289
	public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
290
		if ($sourceStorage === $this) {
0 ignored issues
show
introduced by
The condition $sourceStorage === $this is always false.
Loading history...
291
			return $this->copy($sourceInternalPath, $targetInternalPath);
292
		}
293
294
		$this->checkFileAccess($targetInternalPath);
295
		return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
296
	}
297
298
	/**
299
	 * @param IStorage $sourceStorage
300
	 * @param string $sourceInternalPath
301
	 * @param string $targetInternalPath
302
	 * @return bool
303
	 * @throws ForbiddenException
304
	 */
305
	public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
306
		if ($sourceStorage === $this) {
0 ignored issues
show
introduced by
The condition $sourceStorage === $this is always false.
Loading history...
307
			return $this->rename($sourceInternalPath, $targetInternalPath);
308
		}
309
310
		$this->checkFileAccess($targetInternalPath);
311
		return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
312
	}
313
314
	/**
315
	 * @throws ForbiddenException
316
	 */
317
	public function writeStream(string $path, $stream, int $size = null): int {
318
		// Required for object storage since  part file is not in the storage so we cannot check it before moving it to the storage
319
		// As an alternative we might be able to check on the cache update/insert/delete though the Cache wrapper
320
		$result = $this->storage->writeStream($path, $stream, $size);
321
		try {
322
			$this->checkFileAccess($path);
323
		} catch (\Exception $e) {
324
			$this->storage->unlink($path);
325
			throw $e;
326
		}
327
		return $result;
328
	}
329
}
330