Skip to content

Commit 2ac9a1b

Browse files
SebastianKrupinskisolracsf
authored andcommitted
fix(caldav): Do not load IMipPlugin before user auth and session is created
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
1 parent fa949da commit 2ac9a1b

File tree

3 files changed

+91
-39
lines changed

3 files changed

+91
-39
lines changed

apps/dav/lib/CalDAV/Schedule/IMipPlugin.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
use OCP\AppFramework\Utility\ITimeFactory;
4242
use OCP\Defaults;
4343
use OCP\IConfig;
44-
use OCP\IUserManager;
44+
use OCP\IUserSession;
4545
use OCP\Mail\IMailer;
4646
use OCP\Util;
4747
use Psr\Log\LoggerInterface;
@@ -69,13 +69,12 @@
6969
* @license http://sabre.io/license/ Modified BSD License
7070
*/
7171
class IMipPlugin extends SabreIMipPlugin {
72-
private ?string $userId;
72+
private IUserSession $userSession;
7373
private IConfig $config;
7474
private IMailer $mailer;
7575
private LoggerInterface $logger;
7676
private ITimeFactory $timeFactory;
7777
private Defaults $defaults;
78-
private IUserManager $userManager;
7978
private ?VCalendar $vCalendar = null;
8079
private IMipService $imipService;
8180
public const MAX_DATE = '2038-01-01';
@@ -90,18 +89,16 @@ public function __construct(IConfig $config,
9089
LoggerInterface $logger,
9190
ITimeFactory $timeFactory,
9291
Defaults $defaults,
93-
IUserManager $userManager,
94-
$userId,
92+
IUserSession $userSession,
9593
IMipService $imipService,
9694
EventComparisonService $eventComparisonService) {
9795
parent::__construct('');
98-
$this->userId = $userId;
96+
$this->userSession = $userSession;
9997
$this->config = $config;
10098
$this->mailer = $mailer;
10199
$this->logger = $logger;
102100
$this->timeFactory = $timeFactory;
103101
$this->defaults = $defaults;
104-
$this->userManager = $userManager;
105102
$this->imipService = $imipService;
106103
$this->eventComparisonService = $eventComparisonService;
107104
}
@@ -206,17 +203,16 @@ public function schedule(Message $iTipMessage) {
206203
$this->imipService->setL10n($attendee);
207204

208205
// Build the sender name.
209-
// Due to a bug in sabre, the senderName property for an iTIP message
210-
// can actually also be a VObject Property
211-
/** @var Parameter|string|null $senderName */
212-
$senderName = $iTipMessage->senderName ?: null;
213-
if($senderName instanceof Parameter) {
214-
$senderName = $senderName->getValue() ?? null;
215-
}
216-
217-
// Try to get the sender name from the current user id if available.
218-
if ($this->userId !== null && ($senderName === null || empty(trim($senderName)))) {
219-
$senderName = $this->userManager->getDisplayName($this->userId);
206+
// Due to a bug in sabre, the senderName property for an iTIP message can actually also be a VObject Property
207+
// If the iTIP message senderName is null or empty use the user session name as the senderName
208+
if (($iTipMessage->senderName instanceof Parameter) && !empty(trim($iTipMessage->senderName->getValue()))) {
209+
$senderName = trim($iTipMessage->senderName->getValue());
210+
} elseif (is_string($iTipMessage->senderName) && !empty(trim($iTipMessage->senderName))) {
211+
$senderName = trim($iTipMessage->senderName);
212+
} elseif ($this->userSession->getUser() !== null) {
213+
$senderName = trim($this->userSession->getUser()->getDisplayName());
214+
} else {
215+
$senderName = '';
220216
}
221217

222218
$sender = substr($iTipMessage->sender, 7);

apps/dav/lib/Server.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use OCA\DAV\AppInfo\PluginManager;
4040
use OCA\DAV\BulkUpload\BulkUploadPlugin;
4141
use OCA\DAV\CalDAV\BirthdayService;
42+
use OCA\DAV\CalDAV\Schedule\IMipPlugin;
4243
use OCA\DAV\CalDAV\Security\RateLimitingPlugin;
4344
use OCA\DAV\CalDAV\Validation\CalDavValidatePlugin;
4445
use OCA\DAV\CardDAV\HasPhotoPlugin;
@@ -179,12 +180,10 @@ public function __construct(IRequest $request, string $baseUri) {
179180

180181
// calendar plugins
181182
if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) {
183+
$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
182184
$this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
183185
$this->server->addPlugin(new \OCA\DAV\CalDAV\ICSExportPlugin\ICSExportPlugin(\OC::$server->getConfig(), $logger));
184186
$this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class)));
185-
if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
186-
$this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
187-
}
188187

189188
$this->server->addPlugin(\OC::$server->get(\OCA\DAV\CalDAV\Trashbin\Plugin::class));
190189
$this->server->addPlugin(new \OCA\DAV\CalDAV\WebcalCaching\Plugin($request));
@@ -193,7 +192,6 @@ public function __construct(IRequest $request, string $baseUri) {
193192
}
194193

195194
$this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
196-
$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
197195
$this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
198196
\OC::$server->getConfig(),
199197
\OC::$server->getURLGenerator()
@@ -310,6 +308,18 @@ public function __construct(IRequest $request, string $baseUri) {
310308
\OC::$server->getCommentsManager(),
311309
$userSession
312310
));
311+
if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
312+
$this->server->addPlugin(new IMipPlugin(
313+
\OC::$server->get(\OCP\IConfig::class),
314+
\OC::$server->get(\OCP\Mail\IMailer::class),
315+
\OC::$server->get(LoggerInterface::class),
316+
\OC::$server->get(\OCP\AppFramework\Utility\ITimeFactory::class),
317+
\OC::$server->get(\OCP\Defaults::class),
318+
$userSession,
319+
\OC::$server->get(\OCA\DAV\CalDAV\Schedule\IMipService::class),
320+
\OC::$server->get(\OCA\DAV\CalDAV\EventComparisonService::class)
321+
));
322+
}
313323
$this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
314324
if ($view !== null) {
315325
$this->server->addPlugin(new FilesReportPlugin(

apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
use OCP\AppFramework\Utility\ITimeFactory;
3636
use OCP\Defaults;
3737
use OCP\IConfig;
38-
use OCP\IUserManager;
38+
use OCP\IUser;
39+
use OCP\IUserSession;
3940
use OCP\Mail\IAttachment;
4041
use OCP\Mail\IEMailTemplate;
4142
use OCP\Mail\IMailer;
@@ -68,8 +69,11 @@ class IMipPluginTest extends TestCase {
6869
/** @var IConfig|MockObject */
6970
private $config;
7071

71-
/** @var IUserManager|MockObject */
72-
private $userManager;
72+
/** @var IUserSession|MockObject */
73+
private $userSession;
74+
75+
/** @var IUser|MockObject */
76+
private $user;
7377

7478
/** @var IMipPlugin */
7579
private $plugin;
@@ -107,8 +111,12 @@ protected function setUp(): void {
107111
$this->timeFactory->method('getTime')->willReturn(1496912528); // 2017-01-01
108112

109113
$this->config = $this->createMock(IConfig::class);
114+
115+
$this->user = $this->createMock(IUser::class);
110116

111-
$this->userManager = $this->createMock(IUserManager::class);
117+
$this->userSession = $this->createMock(IUserSession::class);
118+
$this->userSession->method('getUser')
119+
->willReturn($this->user);
112120

113121
$this->defaults = $this->createMock(Defaults::class);
114122
$this->defaults->method('getName')
@@ -124,8 +132,7 @@ protected function setUp(): void {
124132
$this->logger,
125133
$this->timeFactory,
126134
$this->defaults,
127-
$this->userManager,
128-
'user123',
135+
$this->userSession,
129136
$this->service,
130137
$this->eventComparisonService
131138
);
@@ -213,8 +220,15 @@ public function testParsingSingle(): void {
213220
->method('buildBodyData')
214221
->with($newVevent, $oldVEvent)
215222
->willReturn($data);
216-
$this->userManager->expects(self::never())
217-
->method('getDisplayName');
223+
$this->user->expects(self::any())
224+
->method('getUID')
225+
->willReturn('user1');
226+
$this->user->expects(self::any())
227+
->method('getDisplayName')
228+
->willReturn('Mr. Wizard');
229+
$this->userSession->expects(self::any())
230+
->method('getUser')
231+
->willReturn($this->user);
218232
$this->service->expects(self::once())
219233
->method('getFrom');
220234
$this->service->expects(self::once())
@@ -307,8 +321,15 @@ public function testAttendeeIsResource(): void {
307321
->willReturn(true);
308322
$this->service->expects(self::never())
309323
->method('buildBodyData');
310-
$this->userManager->expects(self::never())
311-
->method('getDisplayName');
324+
$this->user->expects(self::any())
325+
->method('getUID')
326+
->willReturn('user1');
327+
$this->user->expects(self::any())
328+
->method('getDisplayName')
329+
->willReturn('Mr. Wizard');
330+
$this->userSession->expects(self::any())
331+
->method('getUser')
332+
->willReturn($this->user);
312333
$this->service->expects(self::never())
313334
->method('getFrom');
314335
$this->service->expects(self::never())
@@ -331,7 +352,6 @@ public function testAttendeeIsResource(): void {
331352
$this->assertEquals('1.0', $message->getScheduleStatus());
332353
}
333354

334-
335355
public function testParsingRecurrence(): void {
336356
$message = new Message();
337357
$message->method = 'REQUEST';
@@ -404,9 +424,15 @@ public function testParsingRecurrence(): void {
404424
->method('buildBodyData')
405425
->with($newVevent, null)
406426
->willReturn($data);
407-
$this->userManager->expects(self::once())
427+
$this->user->expects(self::any())
428+
->method('getUID')
429+
->willReturn('user1');
430+
$this->user->expects(self::any())
408431
->method('getDisplayName')
409432
->willReturn('Mr. Wizard');
433+
$this->userSession->expects(self::any())
434+
->method('getUser')
435+
->willReturn($this->user);
410436
$this->service->expects(self::once())
411437
->method('getFrom');
412438
$this->service->expects(self::once())
@@ -529,8 +555,15 @@ public function testFailedDelivery(): void {
529555
->method('buildBodyData')
530556
->with($newVevent, null)
531557
->willReturn($data);
532-
$this->userManager->expects(self::never())
533-
->method('getDisplayName');
558+
$this->user->expects(self::any())
559+
->method('getUID')
560+
->willReturn('user1');
561+
$this->user->expects(self::any())
562+
->method('getDisplayName')
563+
->willReturn('Mr. Wizard');
564+
$this->userSession->expects(self::any())
565+
->method('getUser')
566+
->willReturn($this->user);
534567
$this->service->expects(self::once())
535568
->method('getFrom');
536569
$this->service->expects(self::once())
@@ -618,8 +651,15 @@ public function testNoOldEvent(): void {
618651
->method('buildBodyData')
619652
->with($newVevent, null)
620653
->willReturn($data);
621-
$this->userManager->expects(self::never())
622-
->method('getDisplayName');
654+
$this->user->expects(self::any())
655+
->method('getUID')
656+
->willReturn('user1');
657+
$this->user->expects(self::any())
658+
->method('getDisplayName')
659+
->willReturn('Mr. Wizard');
660+
$this->userSession->expects(self::any())
661+
->method('getUser')
662+
->willReturn($this->user);
623663
$this->service->expects(self::once())
624664
->method('getFrom');
625665
$this->service->expects(self::once())
@@ -704,9 +744,15 @@ public function testNoButtons(): void {
704744
->method('buildBodyData')
705745
->with($newVevent, null)
706746
->willReturn($data);
707-
$this->userManager->expects(self::once())
747+
$this->user->expects(self::any())
748+
->method('getUID')
749+
->willReturn('user1');
750+
$this->user->expects(self::any())
708751
->method('getDisplayName')
709752
->willReturn('Mr. Wizard');
753+
$this->userSession->expects(self::any())
754+
->method('getUser')
755+
->willReturn($this->user);
710756
$this->service->expects(self::once())
711757
->method('getFrom');
712758
$this->service->expects(self::once())

0 commit comments

Comments
 (0)