@@ -36,109 +36,109 @@ |
||
36 | 36 | |
37 | 37 | class CalendarHome extends \Sabre\CalDAV\CalendarHome { |
38 | 38 | |
39 | - /** @var \OCP\IL10N */ |
|
40 | - private $l10n; |
|
41 | - |
|
42 | - /** @var \OCP\IConfig */ |
|
43 | - private $config; |
|
44 | - |
|
45 | - public function __construct(BackendInterface $caldavBackend, $principalInfo) { |
|
46 | - parent::__construct($caldavBackend, $principalInfo); |
|
47 | - $this->l10n = \OC::$server->getL10N('dav'); |
|
48 | - $this->config = \OC::$server->getConfig(); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @return BackendInterface |
|
53 | - */ |
|
54 | - public function getCalDAVBackend() { |
|
55 | - return $this->caldavBackend; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @inheritdoc |
|
60 | - */ |
|
61 | - function createExtendedCollection($name, MkCol $mkCol) { |
|
62 | - $reservedNames = [BirthdayService::BIRTHDAY_CALENDAR_URI]; |
|
63 | - |
|
64 | - if (in_array($name, $reservedNames)) { |
|
65 | - throw new MethodNotAllowed('The resource you tried to create has a reserved name'); |
|
66 | - } |
|
67 | - |
|
68 | - parent::createExtendedCollection($name, $mkCol); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @inheritdoc |
|
73 | - */ |
|
74 | - function getChildren() { |
|
75 | - $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']); |
|
76 | - $objects = []; |
|
77 | - foreach ($calendars as $calendar) { |
|
78 | - $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config); |
|
79 | - } |
|
80 | - |
|
81 | - if ($this->caldavBackend instanceof SchedulingSupport) { |
|
82 | - $objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']); |
|
83 | - $objects[] = new Outbox($this->config, $this->principalInfo['uri']); |
|
84 | - } |
|
85 | - |
|
86 | - // We're adding a notifications node, if it's supported by the backend. |
|
87 | - if ($this->caldavBackend instanceof NotificationSupport) { |
|
88 | - $objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']); |
|
89 | - } |
|
90 | - |
|
91 | - // If the backend supports subscriptions, we'll add those as well, |
|
92 | - if ($this->caldavBackend instanceof SubscriptionSupport) { |
|
93 | - foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) { |
|
94 | - $objects[] = new Subscription($this->caldavBackend, $subscription); |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - return $objects; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @inheritdoc |
|
103 | - */ |
|
104 | - function getChild($name) { |
|
105 | - // Special nodes |
|
106 | - if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) { |
|
107 | - return new Inbox($this->caldavBackend, $this->principalInfo['uri']); |
|
108 | - } |
|
109 | - if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) { |
|
110 | - return new Outbox($this->config, $this->principalInfo['uri']); |
|
111 | - } |
|
112 | - if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) { |
|
113 | - return new \Sabre\CalDAv\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']); |
|
114 | - } |
|
115 | - |
|
116 | - // Calendars |
|
117 | - foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) { |
|
118 | - if ($calendar['uri'] === $name) { |
|
119 | - return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config); |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - if ($this->caldavBackend instanceof SubscriptionSupport) { |
|
124 | - foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) { |
|
125 | - if ($subscription['uri'] === $name) { |
|
126 | - return new Subscription($this->caldavBackend, $subscription); |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - } |
|
131 | - |
|
132 | - throw new NotFound('Node with name \'' . $name . '\' could not be found'); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * @param array $filters |
|
137 | - * @param integer|null $limit |
|
138 | - * @param integer|null $offset |
|
139 | - */ |
|
140 | - function calendarSearch(array $filters, $limit=null, $offset=null) { |
|
141 | - $principalUri = $this->principalInfo['uri']; |
|
142 | - return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset); |
|
143 | - } |
|
39 | + /** @var \OCP\IL10N */ |
|
40 | + private $l10n; |
|
41 | + |
|
42 | + /** @var \OCP\IConfig */ |
|
43 | + private $config; |
|
44 | + |
|
45 | + public function __construct(BackendInterface $caldavBackend, $principalInfo) { |
|
46 | + parent::__construct($caldavBackend, $principalInfo); |
|
47 | + $this->l10n = \OC::$server->getL10N('dav'); |
|
48 | + $this->config = \OC::$server->getConfig(); |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @return BackendInterface |
|
53 | + */ |
|
54 | + public function getCalDAVBackend() { |
|
55 | + return $this->caldavBackend; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @inheritdoc |
|
60 | + */ |
|
61 | + function createExtendedCollection($name, MkCol $mkCol) { |
|
62 | + $reservedNames = [BirthdayService::BIRTHDAY_CALENDAR_URI]; |
|
63 | + |
|
64 | + if (in_array($name, $reservedNames)) { |
|
65 | + throw new MethodNotAllowed('The resource you tried to create has a reserved name'); |
|
66 | + } |
|
67 | + |
|
68 | + parent::createExtendedCollection($name, $mkCol); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @inheritdoc |
|
73 | + */ |
|
74 | + function getChildren() { |
|
75 | + $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']); |
|
76 | + $objects = []; |
|
77 | + foreach ($calendars as $calendar) { |
|
78 | + $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config); |
|
79 | + } |
|
80 | + |
|
81 | + if ($this->caldavBackend instanceof SchedulingSupport) { |
|
82 | + $objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']); |
|
83 | + $objects[] = new Outbox($this->config, $this->principalInfo['uri']); |
|
84 | + } |
|
85 | + |
|
86 | + // We're adding a notifications node, if it's supported by the backend. |
|
87 | + if ($this->caldavBackend instanceof NotificationSupport) { |
|
88 | + $objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']); |
|
89 | + } |
|
90 | + |
|
91 | + // If the backend supports subscriptions, we'll add those as well, |
|
92 | + if ($this->caldavBackend instanceof SubscriptionSupport) { |
|
93 | + foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) { |
|
94 | + $objects[] = new Subscription($this->caldavBackend, $subscription); |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + return $objects; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @inheritdoc |
|
103 | + */ |
|
104 | + function getChild($name) { |
|
105 | + // Special nodes |
|
106 | + if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) { |
|
107 | + return new Inbox($this->caldavBackend, $this->principalInfo['uri']); |
|
108 | + } |
|
109 | + if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) { |
|
110 | + return new Outbox($this->config, $this->principalInfo['uri']); |
|
111 | + } |
|
112 | + if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) { |
|
113 | + return new \Sabre\CalDAv\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']); |
|
114 | + } |
|
115 | + |
|
116 | + // Calendars |
|
117 | + foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) { |
|
118 | + if ($calendar['uri'] === $name) { |
|
119 | + return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config); |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + if ($this->caldavBackend instanceof SubscriptionSupport) { |
|
124 | + foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) { |
|
125 | + if ($subscription['uri'] === $name) { |
|
126 | + return new Subscription($this->caldavBackend, $subscription); |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + } |
|
131 | + |
|
132 | + throw new NotFound('Node with name \'' . $name . '\' could not be found'); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * @param array $filters |
|
137 | + * @param integer|null $limit |
|
138 | + * @param integer|null $offset |
|
139 | + */ |
|
140 | + function calendarSearch(array $filters, $limit=null, $offset=null) { |
|
141 | + $principalUri = $this->principalInfo['uri']; |
|
142 | + return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset); |
|
143 | + } |
|
144 | 144 | } |
@@ -43,329 +43,329 @@ |
||
43 | 43 | |
44 | 44 | class Principal implements BackendInterface { |
45 | 45 | |
46 | - /** @var IUserManager */ |
|
47 | - private $userManager; |
|
48 | - |
|
49 | - /** @var IGroupManager */ |
|
50 | - private $groupManager; |
|
51 | - |
|
52 | - /** @var IShareManager */ |
|
53 | - private $shareManager; |
|
54 | - |
|
55 | - /** @var IUserSession */ |
|
56 | - private $userSession; |
|
57 | - |
|
58 | - /** @var IConfig */ |
|
59 | - private $config; |
|
60 | - |
|
61 | - /** @var string */ |
|
62 | - private $principalPrefix; |
|
63 | - |
|
64 | - /** @var bool */ |
|
65 | - private $hasGroups; |
|
66 | - |
|
67 | - /** |
|
68 | - * @param IUserManager $userManager |
|
69 | - * @param IGroupManager $groupManager |
|
70 | - * @param IShareManager $shareManager |
|
71 | - * @param IUserSession $userSession |
|
72 | - * @param IConfig $config |
|
73 | - * @param string $principalPrefix |
|
74 | - */ |
|
75 | - public function __construct(IUserManager $userManager, |
|
76 | - IGroupManager $groupManager, |
|
77 | - IShareManager $shareManager, |
|
78 | - IUserSession $userSession, |
|
79 | - IConfig $config, |
|
80 | - $principalPrefix = 'principals/users/') { |
|
81 | - $this->userManager = $userManager; |
|
82 | - $this->groupManager = $groupManager; |
|
83 | - $this->shareManager = $shareManager; |
|
84 | - $this->userSession = $userSession; |
|
85 | - $this->config = $config; |
|
86 | - $this->principalPrefix = trim($principalPrefix, '/'); |
|
87 | - $this->hasGroups = ($principalPrefix === 'principals/users/'); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Returns a list of principals based on a prefix. |
|
92 | - * |
|
93 | - * This prefix will often contain something like 'principals'. You are only |
|
94 | - * expected to return principals that are in this base path. |
|
95 | - * |
|
96 | - * You are expected to return at least a 'uri' for every user, you can |
|
97 | - * return any additional properties if you wish so. Common properties are: |
|
98 | - * {DAV:}displayname |
|
99 | - * |
|
100 | - * @param string $prefixPath |
|
101 | - * @return string[] |
|
102 | - */ |
|
103 | - public function getPrincipalsByPrefix($prefixPath) { |
|
104 | - $principals = []; |
|
105 | - |
|
106 | - if ($prefixPath === $this->principalPrefix) { |
|
107 | - foreach($this->userManager->search('') as $user) { |
|
108 | - $principals[] = $this->userToPrincipal($user); |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - return $principals; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns a specific principal, specified by it's path. |
|
117 | - * The returned structure should be the exact same as from |
|
118 | - * getPrincipalsByPrefix. |
|
119 | - * |
|
120 | - * @param string $path |
|
121 | - * @return array |
|
122 | - */ |
|
123 | - public function getPrincipalByPath($path) { |
|
124 | - list($prefix, $name) = \Sabre\Uri\split($path); |
|
125 | - |
|
126 | - if ($prefix === $this->principalPrefix) { |
|
127 | - $user = $this->userManager->get($name); |
|
128 | - |
|
129 | - if ($user !== null) { |
|
130 | - return $this->userToPrincipal($user); |
|
131 | - } |
|
132 | - } |
|
133 | - return null; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Returns the list of members for a group-principal |
|
138 | - * |
|
139 | - * @param string $principal |
|
140 | - * @return string[] |
|
141 | - * @throws Exception |
|
142 | - */ |
|
143 | - public function getGroupMemberSet($principal) { |
|
144 | - // TODO: for now the group principal has only one member, the user itself |
|
145 | - $principal = $this->getPrincipalByPath($principal); |
|
146 | - if (!$principal) { |
|
147 | - throw new Exception('Principal not found'); |
|
148 | - } |
|
149 | - |
|
150 | - return [$principal['uri']]; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Returns the list of groups a principal is a member of |
|
155 | - * |
|
156 | - * @param string $principal |
|
157 | - * @param bool $needGroups |
|
158 | - * @return array |
|
159 | - * @throws Exception |
|
160 | - */ |
|
161 | - public function getGroupMembership($principal, $needGroups = false) { |
|
162 | - list($prefix, $name) = \Sabre\Uri\split($principal); |
|
163 | - |
|
164 | - if ($prefix === $this->principalPrefix) { |
|
165 | - $user = $this->userManager->get($name); |
|
166 | - if (!$user) { |
|
167 | - throw new Exception('Principal not found'); |
|
168 | - } |
|
169 | - |
|
170 | - if ($this->hasGroups || $needGroups) { |
|
171 | - $groups = $this->groupManager->getUserGroups($user); |
|
172 | - $groups = array_map(function($group) { |
|
173 | - /** @var IGroup $group */ |
|
174 | - return 'principals/groups/' . urlencode($group->getGID()); |
|
175 | - }, $groups); |
|
176 | - |
|
177 | - return $groups; |
|
178 | - } |
|
179 | - } |
|
180 | - return []; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Updates the list of group members for a group principal. |
|
185 | - * |
|
186 | - * The principals should be passed as a list of uri's. |
|
187 | - * |
|
188 | - * @param string $principal |
|
189 | - * @param string[] $members |
|
190 | - * @throws Exception |
|
191 | - */ |
|
192 | - public function setGroupMemberSet($principal, array $members) { |
|
193 | - throw new Exception('Setting members of the group is not supported yet'); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * @param string $path |
|
198 | - * @param PropPatch $propPatch |
|
199 | - * @return int |
|
200 | - */ |
|
201 | - function updatePrincipal($path, PropPatch $propPatch) { |
|
202 | - return 0; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Search user principals |
|
207 | - * |
|
208 | - * @param array $searchProperties |
|
209 | - * @param string $test |
|
210 | - * @return array |
|
211 | - */ |
|
212 | - protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
|
213 | - $results = []; |
|
214 | - |
|
215 | - // If sharing is disabled, return the empty array |
|
216 | - $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
|
217 | - if (!$shareAPIEnabled) { |
|
218 | - return []; |
|
219 | - } |
|
220 | - |
|
221 | - // If sharing is restricted to group members only, |
|
222 | - // return only members that have groups in common |
|
223 | - $restrictGroups = false; |
|
224 | - if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
225 | - $user = $this->userSession->getUser(); |
|
226 | - if (!$user) { |
|
227 | - return []; |
|
228 | - } |
|
229 | - |
|
230 | - $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
231 | - } |
|
232 | - |
|
233 | - foreach ($searchProperties as $prop => $value) { |
|
234 | - switch ($prop) { |
|
235 | - case '{http://sabredav.org/ns}email-address': |
|
236 | - $users = $this->userManager->getByEmail($value); |
|
237 | - |
|
238 | - $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
|
239 | - // is sharing restricted to groups only? |
|
240 | - if ($restrictGroups !== false) { |
|
241 | - $userGroups = $this->groupManager->getUserGroupIds($user); |
|
242 | - if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
|
243 | - return $carry; |
|
244 | - } |
|
245 | - } |
|
246 | - |
|
247 | - $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
|
248 | - return $carry; |
|
249 | - }, []); |
|
250 | - break; |
|
251 | - |
|
252 | - default: |
|
253 | - $results[] = []; |
|
254 | - break; |
|
255 | - } |
|
256 | - } |
|
257 | - |
|
258 | - // results is an array of arrays, so this is not the first search result |
|
259 | - // but the results of the first searchProperty |
|
260 | - if (count($results) === 1) { |
|
261 | - return $results[0]; |
|
262 | - } |
|
263 | - |
|
264 | - switch ($test) { |
|
265 | - case 'anyof': |
|
266 | - return array_unique(array_merge(...$results)); |
|
267 | - |
|
268 | - case 'allof': |
|
269 | - default: |
|
270 | - return array_intersect(...$results); |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * @param string $prefixPath |
|
276 | - * @param array $searchProperties |
|
277 | - * @param string $test |
|
278 | - * @return array |
|
279 | - */ |
|
280 | - function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
|
281 | - if (count($searchProperties) === 0) { |
|
282 | - return []; |
|
283 | - } |
|
284 | - |
|
285 | - switch ($prefixPath) { |
|
286 | - case 'principals/users': |
|
287 | - return $this->searchUserPrincipals($searchProperties, $test); |
|
288 | - |
|
289 | - default: |
|
290 | - return []; |
|
291 | - } |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * @param string $uri |
|
296 | - * @param string $principalPrefix |
|
297 | - * @return string |
|
298 | - */ |
|
299 | - function findByUri($uri, $principalPrefix) { |
|
300 | - // If sharing is disabled, return the empty array |
|
301 | - $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
|
302 | - if (!$shareAPIEnabled) { |
|
303 | - return null; |
|
304 | - } |
|
305 | - |
|
306 | - // If sharing is restricted to group members only, |
|
307 | - // return only members that have groups in common |
|
308 | - $restrictGroups = false; |
|
309 | - if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
310 | - $user = $this->userSession->getUser(); |
|
311 | - if (!$user) { |
|
312 | - return null; |
|
313 | - } |
|
314 | - |
|
315 | - $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
316 | - } |
|
317 | - |
|
318 | - if (strpos($uri, 'mailto:') === 0) { |
|
319 | - if ($principalPrefix === 'principals/users') { |
|
320 | - $users = $this->userManager->getByEmail(substr($uri, 7)); |
|
321 | - if (count($users) !== 1) { |
|
322 | - return null; |
|
323 | - } |
|
324 | - $user = $users[0]; |
|
325 | - |
|
326 | - if ($restrictGroups !== false) { |
|
327 | - $userGroups = $this->groupManager->getUserGroupIds($user); |
|
328 | - if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
|
329 | - return null; |
|
330 | - } |
|
331 | - } |
|
332 | - |
|
333 | - return $this->principalPrefix . '/' . $user->getUID(); |
|
334 | - } |
|
335 | - } |
|
336 | - if (substr($uri, 0, 10) === 'principal:') { |
|
337 | - $principal = substr($uri, 10); |
|
338 | - $principal = $this->getPrincipalByPath($principal); |
|
339 | - if ($principal !== null) { |
|
340 | - return $principal['uri']; |
|
341 | - } |
|
342 | - } |
|
343 | - |
|
344 | - return null; |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * @param IUser $user |
|
349 | - * @return array |
|
350 | - */ |
|
351 | - protected function userToPrincipal($user) { |
|
352 | - $userId = $user->getUID(); |
|
353 | - $displayName = $user->getDisplayName(); |
|
354 | - $principal = [ |
|
355 | - 'uri' => $this->principalPrefix . '/' . $userId, |
|
356 | - '{DAV:}displayname' => is_null($displayName) ? $userId : $displayName, |
|
357 | - ]; |
|
358 | - |
|
359 | - $email = $user->getEMailAddress(); |
|
360 | - if (!empty($email)) { |
|
361 | - $principal['{http://sabredav.org/ns}email-address'] = $email; |
|
362 | - } |
|
363 | - |
|
364 | - return $principal; |
|
365 | - } |
|
366 | - |
|
367 | - public function getPrincipalPrefix() { |
|
368 | - return $this->principalPrefix; |
|
369 | - } |
|
46 | + /** @var IUserManager */ |
|
47 | + private $userManager; |
|
48 | + |
|
49 | + /** @var IGroupManager */ |
|
50 | + private $groupManager; |
|
51 | + |
|
52 | + /** @var IShareManager */ |
|
53 | + private $shareManager; |
|
54 | + |
|
55 | + /** @var IUserSession */ |
|
56 | + private $userSession; |
|
57 | + |
|
58 | + /** @var IConfig */ |
|
59 | + private $config; |
|
60 | + |
|
61 | + /** @var string */ |
|
62 | + private $principalPrefix; |
|
63 | + |
|
64 | + /** @var bool */ |
|
65 | + private $hasGroups; |
|
66 | + |
|
67 | + /** |
|
68 | + * @param IUserManager $userManager |
|
69 | + * @param IGroupManager $groupManager |
|
70 | + * @param IShareManager $shareManager |
|
71 | + * @param IUserSession $userSession |
|
72 | + * @param IConfig $config |
|
73 | + * @param string $principalPrefix |
|
74 | + */ |
|
75 | + public function __construct(IUserManager $userManager, |
|
76 | + IGroupManager $groupManager, |
|
77 | + IShareManager $shareManager, |
|
78 | + IUserSession $userSession, |
|
79 | + IConfig $config, |
|
80 | + $principalPrefix = 'principals/users/') { |
|
81 | + $this->userManager = $userManager; |
|
82 | + $this->groupManager = $groupManager; |
|
83 | + $this->shareManager = $shareManager; |
|
84 | + $this->userSession = $userSession; |
|
85 | + $this->config = $config; |
|
86 | + $this->principalPrefix = trim($principalPrefix, '/'); |
|
87 | + $this->hasGroups = ($principalPrefix === 'principals/users/'); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Returns a list of principals based on a prefix. |
|
92 | + * |
|
93 | + * This prefix will often contain something like 'principals'. You are only |
|
94 | + * expected to return principals that are in this base path. |
|
95 | + * |
|
96 | + * You are expected to return at least a 'uri' for every user, you can |
|
97 | + * return any additional properties if you wish so. Common properties are: |
|
98 | + * {DAV:}displayname |
|
99 | + * |
|
100 | + * @param string $prefixPath |
|
101 | + * @return string[] |
|
102 | + */ |
|
103 | + public function getPrincipalsByPrefix($prefixPath) { |
|
104 | + $principals = []; |
|
105 | + |
|
106 | + if ($prefixPath === $this->principalPrefix) { |
|
107 | + foreach($this->userManager->search('') as $user) { |
|
108 | + $principals[] = $this->userToPrincipal($user); |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + return $principals; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns a specific principal, specified by it's path. |
|
117 | + * The returned structure should be the exact same as from |
|
118 | + * getPrincipalsByPrefix. |
|
119 | + * |
|
120 | + * @param string $path |
|
121 | + * @return array |
|
122 | + */ |
|
123 | + public function getPrincipalByPath($path) { |
|
124 | + list($prefix, $name) = \Sabre\Uri\split($path); |
|
125 | + |
|
126 | + if ($prefix === $this->principalPrefix) { |
|
127 | + $user = $this->userManager->get($name); |
|
128 | + |
|
129 | + if ($user !== null) { |
|
130 | + return $this->userToPrincipal($user); |
|
131 | + } |
|
132 | + } |
|
133 | + return null; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Returns the list of members for a group-principal |
|
138 | + * |
|
139 | + * @param string $principal |
|
140 | + * @return string[] |
|
141 | + * @throws Exception |
|
142 | + */ |
|
143 | + public function getGroupMemberSet($principal) { |
|
144 | + // TODO: for now the group principal has only one member, the user itself |
|
145 | + $principal = $this->getPrincipalByPath($principal); |
|
146 | + if (!$principal) { |
|
147 | + throw new Exception('Principal not found'); |
|
148 | + } |
|
149 | + |
|
150 | + return [$principal['uri']]; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Returns the list of groups a principal is a member of |
|
155 | + * |
|
156 | + * @param string $principal |
|
157 | + * @param bool $needGroups |
|
158 | + * @return array |
|
159 | + * @throws Exception |
|
160 | + */ |
|
161 | + public function getGroupMembership($principal, $needGroups = false) { |
|
162 | + list($prefix, $name) = \Sabre\Uri\split($principal); |
|
163 | + |
|
164 | + if ($prefix === $this->principalPrefix) { |
|
165 | + $user = $this->userManager->get($name); |
|
166 | + if (!$user) { |
|
167 | + throw new Exception('Principal not found'); |
|
168 | + } |
|
169 | + |
|
170 | + if ($this->hasGroups || $needGroups) { |
|
171 | + $groups = $this->groupManager->getUserGroups($user); |
|
172 | + $groups = array_map(function($group) { |
|
173 | + /** @var IGroup $group */ |
|
174 | + return 'principals/groups/' . urlencode($group->getGID()); |
|
175 | + }, $groups); |
|
176 | + |
|
177 | + return $groups; |
|
178 | + } |
|
179 | + } |
|
180 | + return []; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Updates the list of group members for a group principal. |
|
185 | + * |
|
186 | + * The principals should be passed as a list of uri's. |
|
187 | + * |
|
188 | + * @param string $principal |
|
189 | + * @param string[] $members |
|
190 | + * @throws Exception |
|
191 | + */ |
|
192 | + public function setGroupMemberSet($principal, array $members) { |
|
193 | + throw new Exception('Setting members of the group is not supported yet'); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * @param string $path |
|
198 | + * @param PropPatch $propPatch |
|
199 | + * @return int |
|
200 | + */ |
|
201 | + function updatePrincipal($path, PropPatch $propPatch) { |
|
202 | + return 0; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Search user principals |
|
207 | + * |
|
208 | + * @param array $searchProperties |
|
209 | + * @param string $test |
|
210 | + * @return array |
|
211 | + */ |
|
212 | + protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
|
213 | + $results = []; |
|
214 | + |
|
215 | + // If sharing is disabled, return the empty array |
|
216 | + $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
|
217 | + if (!$shareAPIEnabled) { |
|
218 | + return []; |
|
219 | + } |
|
220 | + |
|
221 | + // If sharing is restricted to group members only, |
|
222 | + // return only members that have groups in common |
|
223 | + $restrictGroups = false; |
|
224 | + if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
225 | + $user = $this->userSession->getUser(); |
|
226 | + if (!$user) { |
|
227 | + return []; |
|
228 | + } |
|
229 | + |
|
230 | + $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
231 | + } |
|
232 | + |
|
233 | + foreach ($searchProperties as $prop => $value) { |
|
234 | + switch ($prop) { |
|
235 | + case '{http://sabredav.org/ns}email-address': |
|
236 | + $users = $this->userManager->getByEmail($value); |
|
237 | + |
|
238 | + $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
|
239 | + // is sharing restricted to groups only? |
|
240 | + if ($restrictGroups !== false) { |
|
241 | + $userGroups = $this->groupManager->getUserGroupIds($user); |
|
242 | + if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
|
243 | + return $carry; |
|
244 | + } |
|
245 | + } |
|
246 | + |
|
247 | + $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
|
248 | + return $carry; |
|
249 | + }, []); |
|
250 | + break; |
|
251 | + |
|
252 | + default: |
|
253 | + $results[] = []; |
|
254 | + break; |
|
255 | + } |
|
256 | + } |
|
257 | + |
|
258 | + // results is an array of arrays, so this is not the first search result |
|
259 | + // but the results of the first searchProperty |
|
260 | + if (count($results) === 1) { |
|
261 | + return $results[0]; |
|
262 | + } |
|
263 | + |
|
264 | + switch ($test) { |
|
265 | + case 'anyof': |
|
266 | + return array_unique(array_merge(...$results)); |
|
267 | + |
|
268 | + case 'allof': |
|
269 | + default: |
|
270 | + return array_intersect(...$results); |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * @param string $prefixPath |
|
276 | + * @param array $searchProperties |
|
277 | + * @param string $test |
|
278 | + * @return array |
|
279 | + */ |
|
280 | + function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
|
281 | + if (count($searchProperties) === 0) { |
|
282 | + return []; |
|
283 | + } |
|
284 | + |
|
285 | + switch ($prefixPath) { |
|
286 | + case 'principals/users': |
|
287 | + return $this->searchUserPrincipals($searchProperties, $test); |
|
288 | + |
|
289 | + default: |
|
290 | + return []; |
|
291 | + } |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * @param string $uri |
|
296 | + * @param string $principalPrefix |
|
297 | + * @return string |
|
298 | + */ |
|
299 | + function findByUri($uri, $principalPrefix) { |
|
300 | + // If sharing is disabled, return the empty array |
|
301 | + $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
|
302 | + if (!$shareAPIEnabled) { |
|
303 | + return null; |
|
304 | + } |
|
305 | + |
|
306 | + // If sharing is restricted to group members only, |
|
307 | + // return only members that have groups in common |
|
308 | + $restrictGroups = false; |
|
309 | + if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
310 | + $user = $this->userSession->getUser(); |
|
311 | + if (!$user) { |
|
312 | + return null; |
|
313 | + } |
|
314 | + |
|
315 | + $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
316 | + } |
|
317 | + |
|
318 | + if (strpos($uri, 'mailto:') === 0) { |
|
319 | + if ($principalPrefix === 'principals/users') { |
|
320 | + $users = $this->userManager->getByEmail(substr($uri, 7)); |
|
321 | + if (count($users) !== 1) { |
|
322 | + return null; |
|
323 | + } |
|
324 | + $user = $users[0]; |
|
325 | + |
|
326 | + if ($restrictGroups !== false) { |
|
327 | + $userGroups = $this->groupManager->getUserGroupIds($user); |
|
328 | + if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
|
329 | + return null; |
|
330 | + } |
|
331 | + } |
|
332 | + |
|
333 | + return $this->principalPrefix . '/' . $user->getUID(); |
|
334 | + } |
|
335 | + } |
|
336 | + if (substr($uri, 0, 10) === 'principal:') { |
|
337 | + $principal = substr($uri, 10); |
|
338 | + $principal = $this->getPrincipalByPath($principal); |
|
339 | + if ($principal !== null) { |
|
340 | + return $principal['uri']; |
|
341 | + } |
|
342 | + } |
|
343 | + |
|
344 | + return null; |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * @param IUser $user |
|
349 | + * @return array |
|
350 | + */ |
|
351 | + protected function userToPrincipal($user) { |
|
352 | + $userId = $user->getUID(); |
|
353 | + $displayName = $user->getDisplayName(); |
|
354 | + $principal = [ |
|
355 | + 'uri' => $this->principalPrefix . '/' . $userId, |
|
356 | + '{DAV:}displayname' => is_null($displayName) ? $userId : $displayName, |
|
357 | + ]; |
|
358 | + |
|
359 | + $email = $user->getEMailAddress(); |
|
360 | + if (!empty($email)) { |
|
361 | + $principal['{http://sabredav.org/ns}email-address'] = $email; |
|
362 | + } |
|
363 | + |
|
364 | + return $principal; |
|
365 | + } |
|
366 | + |
|
367 | + public function getPrincipalPrefix() { |
|
368 | + return $this->principalPrefix; |
|
369 | + } |
|
370 | 370 | |
371 | 371 | } |
@@ -31,102 +31,102 @@ |
||
31 | 31 | */ |
32 | 32 | class Outbox extends \Sabre\CalDAV\Schedule\Outbox { |
33 | 33 | |
34 | - /** @var IConfig */ |
|
35 | - private $config; |
|
34 | + /** @var IConfig */ |
|
35 | + private $config; |
|
36 | 36 | |
37 | - /** @var null|bool */ |
|
38 | - private $disableFreeBusy = null; |
|
37 | + /** @var null|bool */ |
|
38 | + private $disableFreeBusy = null; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Outbox constructor. |
|
42 | - * |
|
43 | - * @param IConfig $config |
|
44 | - * @param string $principalUri |
|
45 | - */ |
|
46 | - public function __construct(IConfig $config, $principalUri) { |
|
47 | - parent::__construct($principalUri); |
|
48 | - $this->config = $config; |
|
49 | - } |
|
40 | + /** |
|
41 | + * Outbox constructor. |
|
42 | + * |
|
43 | + * @param IConfig $config |
|
44 | + * @param string $principalUri |
|
45 | + */ |
|
46 | + public function __construct(IConfig $config, $principalUri) { |
|
47 | + parent::__construct($principalUri); |
|
48 | + $this->config = $config; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Returns a list of ACE's for this node. |
|
53 | - * |
|
54 | - * Each ACE has the following properties: |
|
55 | - * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are |
|
56 | - * currently the only supported privileges |
|
57 | - * * 'principal', a url to the principal who owns the node |
|
58 | - * * 'protected' (optional), indicating that this ACE is not allowed to |
|
59 | - * be updated. |
|
60 | - * |
|
61 | - * @return array |
|
62 | - */ |
|
63 | - function getACL() { |
|
64 | - // getACL is called so frequently that we cache the config result |
|
65 | - if ($this->disableFreeBusy === null) { |
|
66 | - $this->disableFreeBusy = ($this->config->getAppValue('dav', 'disableFreeBusy', 'no') === 'yes'); |
|
67 | - } |
|
51 | + /** |
|
52 | + * Returns a list of ACE's for this node. |
|
53 | + * |
|
54 | + * Each ACE has the following properties: |
|
55 | + * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are |
|
56 | + * currently the only supported privileges |
|
57 | + * * 'principal', a url to the principal who owns the node |
|
58 | + * * 'protected' (optional), indicating that this ACE is not allowed to |
|
59 | + * be updated. |
|
60 | + * |
|
61 | + * @return array |
|
62 | + */ |
|
63 | + function getACL() { |
|
64 | + // getACL is called so frequently that we cache the config result |
|
65 | + if ($this->disableFreeBusy === null) { |
|
66 | + $this->disableFreeBusy = ($this->config->getAppValue('dav', 'disableFreeBusy', 'no') === 'yes'); |
|
67 | + } |
|
68 | 68 | |
69 | - $commonAcl = [ |
|
70 | - [ |
|
71 | - 'privilege' => '{DAV:}read', |
|
72 | - 'principal' => $this->getOwner(), |
|
73 | - 'protected' => true, |
|
74 | - ], |
|
75 | - [ |
|
76 | - 'privilege' => '{DAV:}read', |
|
77 | - 'principal' => $this->getOwner() . '/calendar-proxy-read', |
|
78 | - 'protected' => true, |
|
79 | - ], |
|
80 | - [ |
|
81 | - 'privilege' => '{DAV:}read', |
|
82 | - 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
83 | - 'protected' => true, |
|
84 | - ], |
|
85 | - ]; |
|
69 | + $commonAcl = [ |
|
70 | + [ |
|
71 | + 'privilege' => '{DAV:}read', |
|
72 | + 'principal' => $this->getOwner(), |
|
73 | + 'protected' => true, |
|
74 | + ], |
|
75 | + [ |
|
76 | + 'privilege' => '{DAV:}read', |
|
77 | + 'principal' => $this->getOwner() . '/calendar-proxy-read', |
|
78 | + 'protected' => true, |
|
79 | + ], |
|
80 | + [ |
|
81 | + 'privilege' => '{DAV:}read', |
|
82 | + 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
83 | + 'protected' => true, |
|
84 | + ], |
|
85 | + ]; |
|
86 | 86 | |
87 | - // schedule-send is an aggregate privilege for: |
|
88 | - // - schedule-send-invite |
|
89 | - // - schedule-send-reply |
|
90 | - // - schedule-send-freebusy |
|
91 | - // |
|
92 | - // If FreeBusy is disabled, we have to remove the latter privilege |
|
87 | + // schedule-send is an aggregate privilege for: |
|
88 | + // - schedule-send-invite |
|
89 | + // - schedule-send-reply |
|
90 | + // - schedule-send-freebusy |
|
91 | + // |
|
92 | + // If FreeBusy is disabled, we have to remove the latter privilege |
|
93 | 93 | |
94 | - if ($this->disableFreeBusy) { |
|
95 | - return array_merge($commonAcl, [ |
|
96 | - [ |
|
97 | - 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send-invite', |
|
98 | - 'principal' => $this->getOwner(), |
|
99 | - 'protected' => true, |
|
100 | - ], |
|
101 | - [ |
|
102 | - 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send-invite', |
|
103 | - 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
104 | - 'protected' => true, |
|
105 | - ], |
|
106 | - [ |
|
107 | - 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send-reply', |
|
108 | - 'principal' => $this->getOwner(), |
|
109 | - 'protected' => true, |
|
110 | - ], |
|
111 | - [ |
|
112 | - 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send-reply', |
|
113 | - 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
114 | - 'protected' => true, |
|
115 | - ], |
|
116 | - ]); |
|
117 | - } |
|
94 | + if ($this->disableFreeBusy) { |
|
95 | + return array_merge($commonAcl, [ |
|
96 | + [ |
|
97 | + 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send-invite', |
|
98 | + 'principal' => $this->getOwner(), |
|
99 | + 'protected' => true, |
|
100 | + ], |
|
101 | + [ |
|
102 | + 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send-invite', |
|
103 | + 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
104 | + 'protected' => true, |
|
105 | + ], |
|
106 | + [ |
|
107 | + 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send-reply', |
|
108 | + 'principal' => $this->getOwner(), |
|
109 | + 'protected' => true, |
|
110 | + ], |
|
111 | + [ |
|
112 | + 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send-reply', |
|
113 | + 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
114 | + 'protected' => true, |
|
115 | + ], |
|
116 | + ]); |
|
117 | + } |
|
118 | 118 | |
119 | - return array_merge($commonAcl, [ |
|
120 | - [ |
|
121 | - 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send', |
|
122 | - 'principal' => $this->getOwner(), |
|
123 | - 'protected' => true, |
|
124 | - ], |
|
125 | - [ |
|
126 | - 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send', |
|
127 | - 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
128 | - 'protected' => true, |
|
129 | - ], |
|
130 | - ]); |
|
131 | - } |
|
119 | + return array_merge($commonAcl, [ |
|
120 | + [ |
|
121 | + 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send', |
|
122 | + 'principal' => $this->getOwner(), |
|
123 | + 'protected' => true, |
|
124 | + ], |
|
125 | + [ |
|
126 | + 'privilege' => '{' . CalDAVPlugin::NS_CALDAV . '}schedule-send', |
|
127 | + 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
128 | + 'protected' => true, |
|
129 | + ], |
|
130 | + ]); |
|
131 | + } |
|
132 | 132 | } |