1 | <?php |
||
2 | /* |
||
3 | * SPDX-License-Identifier: AGPL-3.0-only |
||
4 | * SPDX-FileCopyrightText: Copyright 2016 - 2018 Kopano b.v. |
||
5 | * SPDX-FileCopyrightText: Copyright 2020-2024 grommunio GmbH |
||
6 | * |
||
7 | * grommunio DAV Principals backend class. |
||
8 | */ |
||
9 | |||
10 | namespace grommunio\DAV; |
||
11 | |||
12 | use Sabre\DAV\PropPatch; |
||
13 | use Sabre\DAVACL\PrincipalBackend\BackendInterface; |
||
14 | |||
15 | class PrincipalsBackend implements BackendInterface { |
||
16 | protected $gDavBackend; |
||
17 | |||
18 | /** |
||
19 | * Constructor. |
||
20 | */ |
||
21 | public function __construct(GrommunioDavBackend $gDavBackend) { |
||
22 | $this->gDavBackend = $gDavBackend; |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Returns a list of principals based on a prefix. |
||
27 | * |
||
28 | * This prefix will often contain something like 'principals'. You are only |
||
29 | * expected to return principals that are in this base path. |
||
30 | * |
||
31 | * You are expected to return at least a 'uri' for every user, you can |
||
32 | * return any additional properties if you wish so. Common properties are: |
||
33 | * {DAV:}displayname |
||
34 | * {http://sabredav.org/ns}email-address - This is a custom SabreDAV |
||
35 | * field that's actually injected in a number of other properties. If |
||
36 | * you have an email address, use this property. |
||
37 | * |
||
38 | * @param string $prefixPath |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | public function getPrincipalsByPrefix($prefixPath) { |
||
43 | $principals = []; |
||
44 | if ($prefixPath === 'principals') { |
||
45 | $principals[] = $this->getPrincipalByPath($prefixPath); |
||
46 | $principals[] = $this->getPrincipalByPath('principals/public'); |
||
47 | } |
||
48 | |||
49 | return $principals; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Returns a specific principal, specified by it's path. |
||
54 | * The returned structure should be the exact same as from |
||
55 | * getPrincipalsByPrefix. |
||
56 | * |
||
57 | * @param string $path |
||
58 | * |
||
59 | * @return array|bool |
||
60 | */ |
||
61 | public function getPrincipalByPath($path) { |
||
62 | if ($path === 'principals/public') { |
||
63 | return [ |
||
64 | 'id' => 'public', |
||
65 | 'uri' => 'principals/public', |
||
66 | '{DAV:}displayname' => 'Public', |
||
67 | '{http://sabredav.org/ns}email-address' => 'postmaster@localhost', |
||
68 | ]; |
||
69 | } |
||
70 | if ($path === 'principals') { |
||
71 | $username = $this->gDavBackend->GetUser(); |
||
72 | } |
||
73 | else { |
||
74 | $username = str_replace('principals/', '', $path); |
||
75 | } |
||
76 | $userinfo = nsp_getuserinfo($username); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
77 | if (!$userinfo) { |
||
78 | return false; |
||
79 | } |
||
80 | $emailaddress = $userinfo['emailaddress'] ?? false; |
||
81 | $fullname = $userinfo['fullname'] ?? false; |
||
82 | |||
83 | return [ |
||
84 | 'id' => $username, |
||
85 | 'uri' => 'principals/' . $username, |
||
86 | '{DAV:}displayname' => $fullname, |
||
87 | '{http://sabredav.org/ns}email-address' => $emailaddress, |
||
88 | // TODO 'vcardurl' should be set, see here: http://sabre.io/dav/principals/ |
||
89 | ]; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Updates one or more webdav properties on a principal. |
||
94 | * |
||
95 | * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
||
96 | * To do the actual updates, you must tell this object which properties |
||
97 | * you're going to process with the handle() method. |
||
98 | * |
||
99 | * Calling the handle method is like telling the PropPatch object "I |
||
100 | * promise I can handle updating this property". |
||
101 | * |
||
102 | * Read the PropPatch documentation for more info and examples. |
||
103 | * |
||
104 | * @param string $path |
||
105 | */ |
||
106 | public function updatePrincipal($path, PropPatch $propPatch) {} |
||
107 | |||
108 | /** |
||
109 | * This method is used to search for principals matching a set of |
||
110 | * properties. |
||
111 | * |
||
112 | * This search is specifically used by RFC3744's principal-property-search |
||
113 | * REPORT. |
||
114 | * |
||
115 | * The actual search should be a unicode-non-case-sensitive search. The |
||
116 | * keys in searchProperties are the WebDAV property names, while the values |
||
117 | * are the property values to search on. |
||
118 | * |
||
119 | * By default, if multiple properties are submitted to this method, the |
||
120 | * various properties should be combined with 'AND'. If $test is set to |
||
121 | * 'anyof', it should be combined using 'OR'. |
||
122 | * |
||
123 | * This method should simply return an array with full principal uri's. |
||
124 | * |
||
125 | * If somebody attempted to search on a property the backend does not |
||
126 | * support, you should simply return 0 results. |
||
127 | * |
||
128 | * You can also just return 0 results if you choose to not support |
||
129 | * searching at all, but keep in mind that this may stop certain features |
||
130 | * from working. |
||
131 | * |
||
132 | * @param string $prefixPath |
||
133 | * @param string $test |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | public function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
138 | return []; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Finds a principal by its URI. |
||
143 | * |
||
144 | * This method may receive any type of uri, but mailto: addresses will be |
||
145 | * the most common. |
||
146 | * |
||
147 | * Implementation of this API is optional. It is currently used by the |
||
148 | * CalDAV system to find principals based on their email addresses. If this |
||
149 | * API is not implemented, some features may not work correctly. |
||
150 | * |
||
151 | * This method must return a relative principal path, or null, if the |
||
152 | * principal was not found or you refuse to find it. |
||
153 | * |
||
154 | * @param string $uri |
||
155 | * @param string $principalPrefix |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | public function findByUri($uri, $principalPrefix) { |
||
160 | return ''; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Returns the list of members for a group-principal. |
||
165 | * |
||
166 | * @param string $principal |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | public function getGroupMemberSet($principal) { |
||
171 | return []; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Returns the list of groups a principal is a member of. |
||
176 | * |
||
177 | * @param string $principal |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | public function getGroupMembership($principal) { |
||
182 | return []; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Updates the list of group members for a group principal. |
||
187 | * |
||
188 | * The principals should be passed as a list of uri's. |
||
189 | * |
||
190 | * @param string $principal |
||
191 | */ |
||
192 | public function setGroupMemberSet($principal, array $members) {} |
||
193 | } |
||
194 |