Passed
Push — master ( 23e8ae...e8872f )
by Robin
16:16 queued 12s
created

Root::getUserFolder()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 5
nop 1
dl 0
loc 34
rs 9.3222
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Jörn Friedrich Dreyer <[email protected]>
9
 * @author Julius Härtl <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Robin Appelman <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 * @author Stefan Weil <[email protected]>
15
 * @author Vincent Petry <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
33
namespace OC\Files\Node;
34
35
use OC\Cache\CappedMemoryCache;
36
use OC\Files\Mount\Manager;
37
use OC\Files\Mount\MountPoint;
38
use OC\Files\View;
39
use OC\Hooks\PublicEmitter;
40
use OC\User\NoUserException;
41
use OCP\EventDispatcher\IEventDispatcher;
42
use OCP\Files\Config\IUserMountCache;
43
use OCP\Files\Events\Node\FilesystemTornDownEvent;
44
use OCP\Files\IRootFolder;
45
use OCP\Files\NotFoundException;
46
use OCP\Files\NotPermittedException;
47
use OCP\ILogger;
48
use OCP\IUser;
49
use OCP\IUserManager;
50
51
/**
52
 * Class Root
53
 *
54
 * Hooks available in scope \OC\Files
55
 * - preWrite(\OCP\Files\Node $node)
56
 * - postWrite(\OCP\Files\Node $node)
57
 * - preCreate(\OCP\Files\Node $node)
58
 * - postCreate(\OCP\Files\Node $node)
59
 * - preDelete(\OCP\Files\Node $node)
60
 * - postDelete(\OCP\Files\Node $node)
61
 * - preTouch(\OC\FilesP\Node $node, int $mtime)
62
 * - postTouch(\OCP\Files\Node $node)
63
 * - preCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
64
 * - postCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
65
 * - preRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
66
 * - postRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
67
 *
68
 * @package OC\Files\Node
69
 */
70
class Root extends Folder implements IRootFolder {
71
	private Manager $mountManager;
72
	private PublicEmitter $emitter;
73
	private ?IUser $user;
74
	private CappedMemoryCache $userFolderCache;
75
	private IUserMountCache $userMountCache;
76
	private ILogger $logger;
77
	private IUserManager $userManager;
78
	private IEventDispatcher $eventDispatcher;
0 ignored issues
show
introduced by
The private property $eventDispatcher is not used, and could be removed.
Loading history...
79
80
	/**
81
	 * @param Manager $manager
82
	 * @param View $view
83
	 * @param IUser|null $user
84
	 * @param IUserMountCache $userMountCache
85
	 * @param ILogger $logger
86
	 * @param IUserManager $userManager
87
	 */
88
	public function __construct(
89
		$manager,
90
		$view,
91
		$user,
92
		IUserMountCache $userMountCache,
93
		ILogger $logger,
94
		IUserManager $userManager,
95
		IEventDispatcher $eventDispatcher
96
	) {
97
		parent::__construct($this, $view, '');
98
		$this->mountManager = $manager;
99
		$this->user = $user;
100
		$this->emitter = new PublicEmitter();
0 ignored issues
show
Deprecated Code introduced by
The class OC\Hooks\PublicEmitter has been deprecated: 18.0.0 use events and the \OCP\EventDispatcher\IEventDispatcher service ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

100
		$this->emitter = /** @scrutinizer ignore-deprecated */ new PublicEmitter();
Loading history...
101
		$this->userFolderCache = new CappedMemoryCache();
102
		$this->userMountCache = $userMountCache;
103
		$this->logger = $logger;
104
		$this->userManager = $userManager;
105
		$eventDispatcher->addListener(FilesystemTornDownEvent::class, function () {
106
			$this->userFolderCache = new CappedMemoryCache();
107
		});
108
	}
109
110
	/**
111
	 * Get the user for which the filesystem is setup
112
	 *
113
	 * @return \OC\User\User
114
	 */
115
	public function getUser() {
116
		return $this->user;
117
	}
118
119
	/**
120
	 * @param string $scope
121
	 * @param string $method
122
	 * @param callable $callback
123
	 */
124
	public function listen($scope, $method, callable $callback) {
125
		$this->emitter->listen($scope, $method, $callback);
126
	}
127
128
	/**
129
	 * @param string $scope optional
130
	 * @param string $method optional
131
	 * @param callable $callback optional
132
	 */
133
	public function removeListener($scope = null, $method = null, callable $callback = null) {
134
		$this->emitter->removeListener($scope, $method, $callback);
135
	}
136
137
	/**
138
	 * @param string $scope
139
	 * @param string $method
140
	 * @param Node[] $arguments
141
	 */
142
	public function emit($scope, $method, $arguments = []) {
143
		$this->emitter->emit($scope, $method, $arguments);
144
	}
145
146
	/**
147
	 * @param \OC\Files\Storage\Storage $storage
148
	 * @param string $mountPoint
149
	 * @param array $arguments
150
	 */
151
	public function mount($storage, $mountPoint, $arguments = []) {
152
		$mount = new MountPoint($storage, $mountPoint, $arguments);
153
		$this->mountManager->addMount($mount);
154
	}
155
156
	/**
157
	 * @param string $mountPoint
158
	 * @return \OC\Files\Mount\MountPoint
159
	 */
160
	public function getMount($mountPoint) {
161
		return $this->mountManager->find($mountPoint);
162
	}
163
164
	/**
165
	 * @param string $mountPoint
166
	 * @return \OC\Files\Mount\MountPoint[]
167
	 */
168
	public function getMountsIn($mountPoint) {
169
		return $this->mountManager->findIn($mountPoint);
170
	}
171
172
	/**
173
	 * @param string $storageId
174
	 * @return \OC\Files\Mount\MountPoint[]
175
	 */
176
	public function getMountByStorageId($storageId) {
177
		return $this->mountManager->findByStorageId($storageId);
178
	}
179
180
	/**
181
	 * @param int $numericId
182
	 * @return MountPoint[]
183
	 */
184
	public function getMountByNumericStorageId($numericId) {
185
		return $this->mountManager->findByNumericId($numericId);
186
	}
187
188
	/**
189
	 * @param \OC\Files\Mount\MountPoint $mount
190
	 */
191
	public function unMount($mount) {
192
		$this->mountManager->remove($mount);
0 ignored issues
show
Bug introduced by
The method remove() does not exist on OC\Files\Mount\Manager. Did you maybe mean removeMount()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

192
		$this->mountManager->/** @scrutinizer ignore-call */ 
193
                       remove($mount);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
193
	}
194
195
	/**
196
	 * @param string $path
197
	 * @return Node
198
	 * @throws \OCP\Files\NotPermittedException
199
	 * @throws \OCP\Files\NotFoundException
200
	 */
201
	public function get($path) {
202
		$path = $this->normalizePath($path);
203
		if ($this->isValidPath($path)) {
204
			$fullPath = $this->getFullPath($path);
205
			$fileInfo = $this->view->getFileInfo($fullPath);
206
			if ($fileInfo) {
207
				return $this->createNode($fullPath, $fileInfo);
208
			} else {
209
				throw new NotFoundException($path);
210
			}
211
		} else {
212
			throw new NotPermittedException();
213
		}
214
	}
215
216
	//most operations can't be done on the root
217
218
	/**
219
	 * @param string $targetPath
220
	 * @return \OC\Files\Node\Node
221
	 * @throws \OCP\Files\NotPermittedException
222
	 */
223
	public function rename($targetPath) {
0 ignored issues
show
Unused Code introduced by
The parameter $targetPath is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

223
	public function rename(/** @scrutinizer ignore-unused */ $targetPath) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
224
		throw new NotPermittedException();
225
	}
226
227
	public function delete() {
228
		throw new NotPermittedException();
229
	}
230
231
	/**
232
	 * @param string $targetPath
233
	 * @return \OC\Files\Node\Node
234
	 * @throws \OCP\Files\NotPermittedException
235
	 */
236
	public function copy($targetPath) {
237
		throw new NotPermittedException();
238
	}
239
240
	/**
241
	 * @param int $mtime
242
	 * @throws \OCP\Files\NotPermittedException
243
	 */
244
	public function touch($mtime = null) {
245
		throw new NotPermittedException();
246
	}
247
248
	/**
249
	 * @return \OC\Files\Storage\Storage
250
	 * @throws \OCP\Files\NotFoundException
251
	 */
252
	public function getStorage() {
253
		throw new NotFoundException();
254
	}
255
256
	/**
257
	 * @return string
258
	 */
259
	public function getPath() {
260
		return '/';
261
	}
262
263
	/**
264
	 * @return string
265
	 */
266
	public function getInternalPath() {
267
		return '';
268
	}
269
270
	/**
271
	 * @return int
272
	 */
273
	public function getId() {
274
		return 0;
275
	}
276
277
	/**
278
	 * @return array
279
	 */
280
	public function stat() {
281
		return [];
282
	}
283
284
	/**
285
	 * @return int
286
	 */
287
	public function getMTime() {
288
		return 0;
289
	}
290
291
	/**
292
	 * @param bool $includeMounts
293
	 * @return int
294
	 */
295
	public function getSize($includeMounts = true) {
296
		return 0;
297
	}
298
299
	/**
300
	 * @return string
301
	 */
302
	public function getEtag() {
303
		return '';
304
	}
305
306
	/**
307
	 * @return int
308
	 */
309
	public function getPermissions() {
310
		return \OCP\Constants::PERMISSION_CREATE;
311
	}
312
313
	/**
314
	 * @return bool
315
	 */
316
	public function isReadable() {
317
		return false;
318
	}
319
320
	/**
321
	 * @return bool
322
	 */
323
	public function isUpdateable() {
324
		return false;
325
	}
326
327
	/**
328
	 * @return bool
329
	 */
330
	public function isDeletable() {
331
		return false;
332
	}
333
334
	/**
335
	 * @return bool
336
	 */
337
	public function isShareable() {
338
		return false;
339
	}
340
341
	/**
342
	 * @return Node
343
	 * @throws \OCP\Files\NotFoundException
344
	 */
345
	public function getParent() {
346
		throw new NotFoundException();
347
	}
348
349
	/**
350
	 * @return string
351
	 */
352
	public function getName() {
353
		return '';
354
	}
355
356
	/**
357
	 * Returns a view to user's files folder
358
	 *
359
	 * @param string $userId user ID
360
	 * @return \OCP\Files\Folder
361
	 * @throws NoUserException
362
	 * @throws NotPermittedException
363
	 */
364
	public function getUserFolder($userId) {
365
		$userObject = $this->userManager->get($userId);
366
367
		if (is_null($userObject)) {
368
			$this->logger->error(
369
				sprintf(
370
					'Backends provided no user object for %s',
371
					$userId
372
				),
373
				[
374
					'app' => 'files',
375
				]
376
			);
377
			throw new NoUserException('Backends provided no user object');
378
		}
379
380
		$userId = $userObject->getUID();
381
382
		if (!$this->userFolderCache->hasKey($userId)) {
383
			\OC\Files\Filesystem::initMountPoints($userId);
384
385
			try {
386
				$folder = $this->get('/' . $userId . '/files');
387
			} catch (NotFoundException $e) {
388
				if (!$this->nodeExists('/' . $userId)) {
389
					$this->newFolder('/' . $userId);
390
				}
391
				$folder = $this->newFolder('/' . $userId . '/files');
392
			}
393
394
			$this->userFolderCache->set($userId, $folder);
395
		}
396
397
		return $this->userFolderCache->get($userId);
398
	}
399
400
	public function getUserMountCache() {
401
		return $this->userMountCache;
402
	}
403
}
404