Skip to content

Commit c17fcc4

Browse files
Merge pull request #45098 from nextcloud/fix/issue-45081
Do not load IMipPlugin before user session is initialized also use userSession instead of userId in constructor.
2 parents 461f6d4 + 51d338b commit c17fcc4

3 files changed

Lines changed: 95 additions & 39 deletions

File tree

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\CardDAV\HasPhotoPlugin;
4445
use OCA\DAV\CardDAV\ImageExportPlugin;
@@ -176,12 +177,10 @@ public function __construct(IRequest $request, string $baseUri) {
176177

177178
// calendar plugins
178179
if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) {
180+
$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
179181
$this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
180182
$this->server->addPlugin(new \OCA\DAV\CalDAV\ICSExportPlugin\ICSExportPlugin(\OC::$server->getConfig(), $logger));
181183
$this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class)));
182-
if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
183-
$this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
184-
}
185184

186185
$this->server->addPlugin(\OC::$server->get(\OCA\DAV\CalDAV\Trashbin\Plugin::class));
187186
$this->server->addPlugin(new \OCA\DAV\CalDAV\WebcalCaching\Plugin($request));
@@ -190,7 +189,6 @@ public function __construct(IRequest $request, string $baseUri) {
190189
}
191190

192191
$this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
193-
$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
194192
$this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
195193
\OC::$server->getConfig(),
196194
\OC::$server->getURLGenerator()
@@ -304,6 +302,18 @@ public function __construct(IRequest $request, string $baseUri) {
304302
\OC::$server->getCommentsManager(),
305303
$userSession
306304
));
305+
if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
306+
$this->server->addPlugin(new IMipPlugin(
307+
\OC::$server->get(\OCP\IConfig::class),
308+
\OC::$server->get(\OCP\Mail\IMailer::class),
309+
\OC::$server->get(LoggerInterface::class),
310+
\OC::$server->get(\OCP\AppFramework\Utility\ITimeFactory::class),
311+
\OC::$server->get(\OCP\Defaults::class),
312+
$userSession,
313+
\OC::$server->get(\OCA\DAV\CalDAV\Schedule\IMipService::class),
314+
\OC::$server->get(\OCA\DAV\CalDAV\EventComparisonService::class)
315+
));
316+
}
307317
$this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
308318
if ($view !== null) {
309319
$this->server->addPlugin(new FilesReportPlugin(

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

Lines changed: 67 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,16 @@ 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);
116+
/*
117+
$this->user->method('getUID');
118+
$this->user->method('getDisplayName');
119+
*/
110120

111-
$this->userManager = $this->createMock(IUserManager::class);
121+
$this->userSession = $this->createMock(IUserSession::class);
122+
$this->userSession->method('getUser')
123+
->willReturn($this->user);
112124

113125
$this->defaults = $this->createMock(Defaults::class);
114126
$this->defaults->method('getName')
@@ -124,8 +136,7 @@ protected function setUp(): void {
124136
$this->logger,
125137
$this->timeFactory,
126138
$this->defaults,
127-
$this->userManager,
128-
'user123',
139+
$this->userSession,
129140
$this->service,
130141
$this->eventComparisonService
131142
);
@@ -213,8 +224,15 @@ public function testParsingSingle(): void {
213224
->method('buildBodyData')
214225
->with($newVevent, $oldVEvent)
215226
->willReturn($data);
216-
$this->userManager->expects(self::never())
217-
->method('getDisplayName');
227+
$this->user->expects(self::any())
228+
->method('getUID')
229+
->willReturn('user1');
230+
$this->user->expects(self::any())
231+
->method('getDisplayName')
232+
->willReturn('Mr. Wizard');
233+
$this->userSession->expects(self::any())
234+
->method('getUser')
235+
->willReturn($this->user);
218236
$this->service->expects(self::once())
219237
->method('getFrom');
220238
$this->service->expects(self::once())
@@ -307,8 +325,15 @@ public function testAttendeeIsResource(): void {
307325
->willReturn(true);
308326
$this->service->expects(self::never())
309327
->method('buildBodyData');
310-
$this->userManager->expects(self::never())
311-
->method('getDisplayName');
328+
$this->user->expects(self::any())
329+
->method('getUID')
330+
->willReturn('user1');
331+
$this->user->expects(self::any())
332+
->method('getDisplayName')
333+
->willReturn('Mr. Wizard');
334+
$this->userSession->expects(self::any())
335+
->method('getUser')
336+
->willReturn($this->user);
312337
$this->service->expects(self::never())
313338
->method('getFrom');
314339
$this->service->expects(self::never())
@@ -331,7 +356,6 @@ public function testAttendeeIsResource(): void {
331356
$this->assertEquals('1.0', $message->getScheduleStatus());
332357
}
333358

334-
335359
public function testParsingRecurrence(): void {
336360
$message = new Message();
337361
$message->method = 'REQUEST';
@@ -404,9 +428,15 @@ public function testParsingRecurrence(): void {
404428
->method('buildBodyData')
405429
->with($newVevent, null)
406430
->willReturn($data);
407-
$this->userManager->expects(self::once())
431+
$this->user->expects(self::any())
432+
->method('getUID')
433+
->willReturn('user1');
434+
$this->user->expects(self::any())
408435
->method('getDisplayName')
409436
->willReturn('Mr. Wizard');
437+
$this->userSession->expects(self::any())
438+
->method('getUser')
439+
->willReturn($this->user);
410440
$this->service->expects(self::once())
411441
->method('getFrom');
412442
$this->service->expects(self::once())
@@ -529,8 +559,15 @@ public function testFailedDelivery(): void {
529559
->method('buildBodyData')
530560
->with($newVevent, null)
531561
->willReturn($data);
532-
$this->userManager->expects(self::never())
533-
->method('getDisplayName');
562+
$this->user->expects(self::any())
563+
->method('getUID')
564+
->willReturn('user1');
565+
$this->user->expects(self::any())
566+
->method('getDisplayName')
567+
->willReturn('Mr. Wizard');
568+
$this->userSession->expects(self::any())
569+
->method('getUser')
570+
->willReturn($this->user);
534571
$this->service->expects(self::once())
535572
->method('getFrom');
536573
$this->service->expects(self::once())
@@ -618,8 +655,15 @@ public function testNoOldEvent(): void {
618655
->method('buildBodyData')
619656
->with($newVevent, null)
620657
->willReturn($data);
621-
$this->userManager->expects(self::never())
622-
->method('getDisplayName');
658+
$this->user->expects(self::any())
659+
->method('getUID')
660+
->willReturn('user1');
661+
$this->user->expects(self::any())
662+
->method('getDisplayName')
663+
->willReturn('Mr. Wizard');
664+
$this->userSession->expects(self::any())
665+
->method('getUser')
666+
->willReturn($this->user);
623667
$this->service->expects(self::once())
624668
->method('getFrom');
625669
$this->service->expects(self::once())
@@ -704,9 +748,15 @@ public function testNoButtons(): void {
704748
->method('buildBodyData')
705749
->with($newVevent, null)
706750
->willReturn($data);
707-
$this->userManager->expects(self::once())
751+
$this->user->expects(self::any())
752+
->method('getUID')
753+
->willReturn('user1');
754+
$this->user->expects(self::any())
708755
->method('getDisplayName')
709756
->willReturn('Mr. Wizard');
757+
$this->userSession->expects(self::any())
758+
->method('getUser')
759+
->willReturn($this->user);
710760
$this->service->expects(self::once())
711761
->method('getFrom');
712762
$this->service->expects(self::once())

0 commit comments

Comments
 (0)