Completed
Push — master ( 1fdbbf...8c8013 )
by Thomas
09:12
created

LegacyDAVACL::convertPrincipal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Lukas Reschke <[email protected]>
5
 * @author Thomas Müller <[email protected]>
6
 *
7
 * @copyright Copyright (c) 2017, ownCloud GmbH
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;
25
26
use OCA\DAV\Connector\Sabre\DavAclPlugin;
27
use Sabre\DAV\INode;
28
use Sabre\DAV\PropFind;
29
use Sabre\HTTP\URLUtil;
30
use Sabre\DAVACL\Xml\Property\Principal;
31
32
class LegacyDAVACL extends DavAclPlugin {
33
34
	/**
35
	 * @inheritdoc
36
	 */
37
	public function getCurrentUserPrincipals() {
38
		$principalV2 = $this->getCurrentUserPrincipal();
39
40
		if (is_null($principalV2)) return [];
41
42
		$principalV1 = $this->convertPrincipal($principalV2, false);
43
		return array_merge(
44
			[
45
				$principalV2,
46
				$principalV1
47
			],
48
			$this->getPrincipalMembership($principalV1)
49
		);
50
	}
51
52
	private function convertPrincipal($principal, $toV2) {
53
		list(, $name) = URLUtil::splitPath($principal);
54
		if ($toV2) {
55
			return "principals/users/$name";
56
		}
57
		return "principals/$name";
58
	}
59
60
	public function propFind(PropFind $propFind, INode $node) {
61
		/* Overload current-user-principal */
62
		$propFind->handle('{DAV:}current-user-principal', function () {
63
			if ($url = parent::getCurrentUserPrincipal()) {
64
				return new Principal(Principal::HREF, $url . '/');
65
			} else {
66
				return new Principal(Principal::UNAUTHENTICATED);
67
			}
68
		});
69
		return parent::propFind($propFind, $node);
70
	}
71
}
72