PrincipalsBackend::getPrincipalByPath()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
c 0
b 0
f 0
nop 1
dl 0
loc 27
rs 9.6
nc 5
1
<?php
2
3
/*
4
 * SPDX-License-Identifier: AGPL-3.0-only
5
 * SPDX-FileCopyrightText: Copyright 2016 - 2018 Kopano b.v.
6
 * SPDX-FileCopyrightText: Copyright 2020-2024 grommunio GmbH
7
 *
8
 * grommunio DAV Principals backend class.
9
 */
10
11
namespace grommunio\DAV;
12
13
use Sabre\DAV\PropPatch;
14
use Sabre\DAVACL\PrincipalBackend\BackendInterface;
15
16
class PrincipalsBackend implements BackendInterface {
17
	protected $gDavBackend;
18
19
	/**
20
	 * Constructor.
21
	 */
22
	public function __construct(GrommunioDavBackend $gDavBackend) {
23
		$this->gDavBackend = $gDavBackend;
24
	}
25
26
	/**
27
	 * Returns a list of principals based on a prefix.
28
	 *
29
	 * This prefix will often contain something like 'principals'. You are only
30
	 * expected to return principals that are in this base path.
31
	 *
32
	 * You are expected to return at least a 'uri' for every user, you can
33
	 * return any additional properties if you wish so. Common properties are:
34
	 *   {DAV:}displayname
35
	 *   {http://sabredav.org/ns}email-address - This is a custom SabreDAV
36
	 *     field that's actually injected in a number of other properties. If
37
	 *     you have an email address, use this property.
38
	 *
39
	 * @param string $prefixPath
40
	 *
41
	 * @return array
42
	 */
43
	public function getPrincipalsByPrefix($prefixPath) {
44
		$principals = [];
45
		if ($prefixPath === 'principals') {
46
			$principals[] = $this->getPrincipalByPath($prefixPath);
47
			$principals[] = $this->getPrincipalByPath('principals/public');
48
		}
49
50
		return $principals;
51
	}
52
53
	/**
54
	 * Returns a specific principal, specified by it's path.
55
	 * The returned structure should be the exact same as from
56
	 * getPrincipalsByPrefix.
57
	 *
58
	 * @param string $path
59
	 *
60
	 * @return array|bool
61
	 */
62
	public function getPrincipalByPath($path) {
63
		if ($path === 'principals/public') {
64
			return [
65
				'id' => 'public',
66
				'uri' => 'principals/public',
67
				'{DAV:}displayname' => 'Public',
68
				'{http://sabredav.org/ns}email-address' => 'postmaster@localhost',
69
			];
70
		}
71
		if ($path === 'principals') {
72
			$username = $this->gDavBackend->GetUser();
73
		}
74
		else {
75
			$username = str_replace('principals/', '', $path);
76
		}
77
		$userinfo = nsp_getuserinfo($username);
0 ignored issues
show
Bug introduced by
The function nsp_getuserinfo was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

77
		$userinfo = /** @scrutinizer ignore-call */ nsp_getuserinfo($username);
Loading history...
78
		if (!$userinfo) {
79
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Sabre\DAVACL\PrincipalBa...e::getPrincipalByPath() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
80
		}
81
		$emailaddress = $userinfo['emailaddress'] ?? false;
82
		$fullname = $userinfo['fullname'] ?? false;
83
84
		return [
85
			'id' => $username,
86
			'uri' => 'principals/' . $username,
87
			'{DAV:}displayname' => $fullname,
88
			'{http://sabredav.org/ns}email-address' => $emailaddress,
89
			// TODO 'vcardurl' should be set, see here: http://sabre.io/dav/principals/
90
		];
91
	}
92
93
	/**
94
	 * Updates one or more webdav properties on a principal.
95
	 *
96
	 * The list of mutations is stored in a Sabre\DAV\PropPatch object.
97
	 * To do the actual updates, you must tell this object which properties
98
	 * you're going to process with the handle() method.
99
	 *
100
	 * Calling the handle method is like telling the PropPatch object "I
101
	 * promise I can handle updating this property".
102
	 *
103
	 * Read the PropPatch documentation for more info and examples.
104
	 *
105
	 * @param string $path
106
	 */
107
	public function updatePrincipal($path, PropPatch $propPatch) {}
108
109
	/**
110
	 * This method is used to search for principals matching a set of
111
	 * properties.
112
	 *
113
	 * This search is specifically used by RFC3744's principal-property-search
114
	 * REPORT.
115
	 *
116
	 * The actual search should be a unicode-non-case-sensitive search. The
117
	 * keys in searchProperties are the WebDAV property names, while the values
118
	 * are the property values to search on.
119
	 *
120
	 * By default, if multiple properties are submitted to this method, the
121
	 * various properties should be combined with 'AND'. If $test is set to
122
	 * 'anyof', it should be combined using 'OR'.
123
	 *
124
	 * This method should simply return an array with full principal uri's.
125
	 *
126
	 * If somebody attempted to search on a property the backend does not
127
	 * support, you should simply return 0 results.
128
	 *
129
	 * You can also just return 0 results if you choose to not support
130
	 * searching at all, but keep in mind that this may stop certain features
131
	 * from working.
132
	 *
133
	 * @param string $prefixPath
134
	 * @param string $test
135
	 *
136
	 * @return array
137
	 */
138
	public function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
139
		return [];
140
	}
141
142
	/**
143
	 * Finds a principal by its URI.
144
	 *
145
	 * This method may receive any type of uri, but mailto: addresses will be
146
	 * the most common.
147
	 *
148
	 * Implementation of this API is optional. It is currently used by the
149
	 * CalDAV system to find principals based on their email addresses. If this
150
	 * API is not implemented, some features may not work correctly.
151
	 *
152
	 * This method must return a relative principal path, or null, if the
153
	 * principal was not found or you refuse to find it.
154
	 *
155
	 * @param string $uri
156
	 * @param string $principalPrefix
157
	 *
158
	 * @return string
159
	 */
160
	public function findByUri($uri, $principalPrefix) {
161
		return '';
162
	}
163
164
	/**
165
	 * Returns the list of members for a group-principal.
166
	 *
167
	 * @param string $principal
168
	 *
169
	 * @return array
170
	 */
171
	public function getGroupMemberSet($principal) {
172
		return [];
173
	}
174
175
	/**
176
	 * Returns the list of groups a principal is a member of.
177
	 *
178
	 * @param string $principal
179
	 *
180
	 * @return array
181
	 */
182
	public function getGroupMembership($principal) {
183
		return [];
184
	}
185
186
	/**
187
	 * Updates the list of group members for a group principal.
188
	 *
189
	 * The principals should be passed as a list of uri's.
190
	 *
191
	 * @param string $principal
192
	 */
193
	public function setGroupMemberSet($principal, array $members) {}
194
}
195