Completed
Push — stable10 ( ee2bd3...02cccc )
by Lukas
08:15
created

DavAclPlugin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B checkPrivileges() 0 25 4
A __construct() 0 3 1
A propFind() 0 15 4
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\DAV\Connector\Sabre;
25
26
use Sabre\CalDAV\Principal\User;
27
use Sabre\DAV\Exception\NotFound;
28
use Sabre\DAV\IFile;
29
use Sabre\DAV\INode;
30
use \Sabre\DAV\PropFind;
31
use \Sabre\DAV\PropPatch;
32
use Sabre\DAVACL\Exception\NeedPrivileges;
33
use \Sabre\HTTP\RequestInterface;
34
use \Sabre\HTTP\ResponseInterface;
35
use Sabre\HTTP\URLUtil;
36
37
/**
38
 * Class DavAclPlugin is a wrapper around \Sabre\DAVACL\Plugin that returns 404
39
 * responses in case the resource to a response has been forbidden instead of
40
 * a 403. This is used to prevent enumeration of valid resources.
41
 *
42
 * @see https://github.com/owncloud/core/issues/22578
43
 * @package OCA\DAV\Connector\Sabre
44
 */
45
class DavAclPlugin extends \Sabre\DAVACL\Plugin {
46
	public function __construct() {
47
		$this->hideNodesFromListings = true;
48
	}
49
50
	function checkPrivileges($uri, $privileges, $recursion = self::R_PARENT, $throwExceptions = true) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
		$access = parent::checkPrivileges($uri, $privileges, $recursion, false);
52
		if($access === false && $throwExceptions) {
53
			/** @var INode $node */
54
			$node = $this->server->tree->getNodeForPath($uri);
55
56
			switch(get_class($node)) {
57
				case 'OCA\DAV\CardDAV\AddressBook':
58
					$type = 'Addressbook';
59
					break;
60
				default:
61
					$type = 'Node';
62
					break;
63
			}
64
			throw new NotFound(
65
				sprintf(
66
					"%s with name '%s' could not be found",
67
					$type,
68
					$node->getName()
69
				)
70
			);
71
		}
72
73
		return $access;
74
	}
75
76
	public function propFind(PropFind $propFind, INode $node) {
77
		// If the node is neither readable nor writable then fail unless its of
78
		// the standard user-principal
79
		if(!($node instanceof User)) {
0 ignored issues
show
Bug introduced by
The class Sabre\CalDAV\Principal\User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
80
			$path = $propFind->getPath();
81
			$readPermissions = $this->checkPrivileges($path, '{DAV:}read', self::R_PARENT, false);
82
			$writePermissions = $this->checkPrivileges($path, '{DAV:}write', self::R_PARENT, false);
83
			if ($readPermissions === false && $writePermissions === false) {
84
				$this->checkPrivileges($path, '{DAV:}read', self::R_PARENT, true);
85
				$this->checkPrivileges($path, '{DAV:}write', self::R_PARENT, true);
86
			}
87
		}
88
89
		return parent::propFind($propFind, $node);
90
	}
91
}
92