|
11 | 11 |
|
12 | 12 | use OCA\DAV\CalDAV\CalendarObject; |
13 | 13 | use OCA\DAV\CalDAV\EventComparisonService; |
| 14 | +use OCP\Accounts\IAccountManager; |
14 | 15 | use OCP\AppFramework\Utility\ITimeFactory; |
15 | 16 | use OCP\Defaults; |
16 | 17 | use OCP\IAppConfig; |
| 18 | +use OCP\IUser; |
| 19 | +use OCP\IUserManager; |
17 | 20 | use OCP\IUserSession; |
18 | 21 | use OCP\Mail\IEmailValidator; |
19 | 22 | use OCP\Mail\IMailer; |
@@ -66,6 +69,8 @@ public function __construct( |
66 | 69 | private EventComparisonService $eventComparisonService, |
67 | 70 | private IMailManager $mailManager, |
68 | 71 | private IEmailValidator $emailValidator, |
| 72 | + private IUserManager $userManager, |
| 73 | + private IAccountManager $accountManager, |
69 | 74 | ) { |
70 | 75 | parent::__construct(''); |
71 | 76 | } |
@@ -184,21 +189,18 @@ public function schedule(Message $iTipMessage) { |
184 | 189 | } |
185 | 190 | $this->imipService->setL10nFromAttendee($attendee); |
186 | 191 |
|
| 192 | + $sender = substr($iTipMessage->sender, 7); |
| 193 | + |
187 | 194 | // Build the sender name. |
188 | 195 | // Due to a bug in sabre, the senderName property for an iTIP message can actually also be a VObject Property |
189 | | - // If the iTIP message senderName is null or empty use the user session name as the senderName |
190 | 196 | if (($iTipMessage->senderName instanceof Parameter) && !empty(trim($iTipMessage->senderName->getValue()))) { |
191 | 197 | $senderName = trim($iTipMessage->senderName->getValue()); |
192 | 198 | } elseif (is_string($iTipMessage->senderName) && !empty(trim($iTipMessage->senderName))) { |
193 | 199 | $senderName = trim($iTipMessage->senderName); |
194 | | - } elseif ($this->userSession->getUser() !== null) { |
195 | | - $senderName = trim($this->userSession->getUser()->getDisplayName()); |
196 | 200 | } else { |
197 | | - $senderName = ''; |
| 201 | + $senderName = $this->getSenderNameFor($sender); |
198 | 202 | } |
199 | 203 |
|
200 | | - $sender = substr($iTipMessage->sender, 7); |
201 | | - |
202 | 204 | $replyingAttendee = null; |
203 | 205 | switch (strtolower($iTipMessage->method)) { |
204 | 206 | case self::METHOD_REPLY: |
@@ -338,6 +340,49 @@ public function schedule(Message $iTipMessage) { |
338 | 340 | } |
339 | 341 | } |
340 | 342 |
|
| 343 | + /** |
| 344 | + * Resolves a display name for a sender address when the iTip message |
| 345 | + * carries no CN parameter. |
| 346 | + * |
| 347 | + * Messages are regularly brokered on behalf of somebody else, so the |
| 348 | + * session user's name is only used when the address is one of theirs. |
| 349 | + * Otherwise the address must map unambiguously to a single local user, |
| 350 | + * matching how login by email treats ambiguous addresses. |
| 351 | + */ |
| 352 | + private function getSenderNameFor(string $sender): ?string { |
| 353 | + $user = $this->userSession->getUser(); |
| 354 | + if ($user !== null && $this->isAddressOfUser($sender, $user)) { |
| 355 | + return trim($user->getDisplayName()); |
| 356 | + } |
| 357 | + |
| 358 | + $candidates = $this->userManager->getByEmail($sender); |
| 359 | + if (count($candidates) === 1) { |
| 360 | + return trim($candidates[0]->getDisplayName()); |
| 361 | + } |
| 362 | + |
| 363 | + return null; |
| 364 | + } |
| 365 | + |
| 366 | + /** |
| 367 | + * Whether the address is the user's system email address or one of the |
| 368 | + * profile email addresses advertised in their calendar-user-address-set. |
| 369 | + */ |
| 370 | + private function isAddressOfUser(string $address, IUser $user): bool { |
| 371 | + if (strcasecmp((string)$user->getEMailAddress(), $address) === 0) { |
| 372 | + return true; |
| 373 | + } |
| 374 | + |
| 375 | + $emailCollection = $this->accountManager->getAccount($user) |
| 376 | + ->getPropertyCollection(IAccountManager::COLLECTION_EMAIL); |
| 377 | + foreach ($emailCollection->getProperties() as $property) { |
| 378 | + if (strcasecmp($property->getValue(), $address) === 0) { |
| 379 | + return true; |
| 380 | + } |
| 381 | + } |
| 382 | + |
| 383 | + return false; |
| 384 | + } |
| 385 | + |
341 | 386 | /** |
342 | 387 | * @return ?VCalendar |
343 | 388 | */ |
|
0 commit comments