Completed
Pull Request — master (#11)
by Joas
02:50
created

Operation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 50
ccs 0
cts 24
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A checkFileAccess() 0 15 3
A isCreatingSkeletonFiles() 0 13 4
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
25
use OCP\Files\ForbiddenException;
26
use OCP\WorkflowEngine\IManager;
27
28
class Operation {
29
30
	/**
31
	 * AccessControl constructor.
32
	 *
33
	 * @param IManager $manager
34
	 */
35
	public function __construct(IManager $manager) {
36
		$this->manager = $manager;
0 ignored issues
show
Bug introduced by
The property manager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
	}
38
39
	/**
40
	 * @param StorageWrapper $storage
41
	 * @param string $path
42
	 * @throws ForbiddenException
43
	 */
44
	public function checkFileAccess(StorageWrapper $storage, $path) {
45
		if ($this->isCreatingSkeletonFiles()) {
46
			// Allow creating skeletons, otherwise the first login fails, see
47
			// https://github.com/nextcloud/files_accesscontrol/issues/5
48
			return;
49
		}
50
51
		$this->manager->setFileInfo($storage, $path);
52
		$match = $this->manager->getMatchingOperations('OCA\FilesAccessControl\Operation');
53
54
		if (!empty($match)) {
55
			// All Checks of one operation matched: prevent access
56
			throw new ForbiddenException('Access denied', true);
57
		}
58
	}
59
60
	/**
61
	 * Check if we are in the LoginController and if so, ignore the firewall
62
	 * @return bool
63
	 */
64
	protected function isCreatingSkeletonFiles() {
65
		$exception = new \Exception();
66
		$trace = $exception->getTrace();
67
68
		foreach ($trace as $step) {
69
			if ($step['class'] === 'OC\Core\Controller\LoginController' &&
70
				$step['function'] === 'tryLogin') {
71
				return true;
72
			}
73
		}
74
75
		return false;
76
	}
77
}
78