Skip to content

Commit

Permalink
fixup! feat: mail provider settings
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianKrupinski committed Sep 17, 2024
1 parent cd3d111 commit d6a1b1b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 19 deletions.
14 changes: 8 additions & 6 deletions apps/dav/lib/CalDAV/Schedule/IMipPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,14 @@ public function schedule(Message $iTipMessage) {
$mailService = null;

try {
// retrieve user object
$user = $this->userSession->getUser();
// evaluate if user object exist
if ($user !== null) {
// retrieve appropriate service with the same address as sender
$mailService = $this->mailManager->findServiceByAddress($user->getUID(), $sender);
if ($this->config->getSystemValueInt('mail_providers_disabled', 0) === 0) {
// retrieve user object
$user = $this->userSession->getUser();
// evaluate if user object exist
if ($user !== null) {
// retrieve appropriate service with the same address as sender
$mailService = $this->mailManager->findServiceByAddress($user->getUID(), $sender);
}
}
// evaluate if a mail service was found and has sending capabilities
if ($mailService !== null && $mailService instanceof IMessageSend) {
Expand Down
107 changes: 107 additions & 0 deletions apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,113 @@ public function testMailProviderSend(): void {
$this->assertEquals('1.1', $message->getScheduleStatus());
}

public function testMailProviderDisabled(): void {
$message = new Message();
$message->method = 'REQUEST';
$newVCalendar = new VCalendar();
$newVevent = new VEvent($newVCalendar, 'one', array_merge([
'UID' => 'uid-1234',
'SEQUENCE' => 1,
'SUMMARY' => 'Fellowship meeting without (!) Boromir',
'DTSTART' => new \DateTime('2016-01-01 00:00:00')
], []));
$newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
$newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
$message->message = $newVCalendar;
$message->sender = 'mailto:gandalf@wiz.ard';
$message->senderName = 'Mr. Wizard';
$message->recipient = 'mailto:' . 'frodo@hobb.it';
// save the old copy in the plugin
$oldVCalendar = new VCalendar();
$oldVEvent = new VEvent($oldVCalendar, 'one', [
'UID' => 'uid-1234',
'SEQUENCE' => 0,
'SUMMARY' => 'Fellowship meeting',
'DTSTART' => new \DateTime('2016-01-01 00:00:00')
]);
$oldVEvent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
$oldVEvent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
$oldVEvent->add('ATTENDEE', 'mailto:' . 'boromir@tra.it.or', ['RSVP' => 'TRUE']);
$oldVCalendar->add($oldVEvent);
$data = ['invitee_name' => 'Mr. Wizard',
'meeting_title' => 'Fellowship meeting without (!) Boromir',
'attendee_name' => 'frodo@hobb.it'
];
$attendees = $newVevent->select('ATTENDEE');
$atnd = '';
foreach ($attendees as $attendee) {
if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
$atnd = $attendee;
}
}
$this->plugin->setVCalendar($oldVCalendar);
$this->service->expects(self::once())
->method('getLastOccurrence')
->willReturn(1496912700);
$this->mailer->expects(self::once())
->method('validateMailAddress')
->with('frodo@hobb.it')
->willReturn(true);
$this->eventComparisonService->expects(self::once())
->method('findModified')
->willReturn(['new' => [$newVevent], 'old' => [$oldVEvent]]);
$this->service->expects(self::once())
->method('getCurrentAttendee')
->with($message)
->willReturn($atnd);
$this->service->expects(self::once())
->method('isRoomOrResource')
->with($atnd)
->willReturn(false);
$this->service->expects(self::once())
->method('buildBodyData')
->with($newVevent, $oldVEvent)
->willReturn($data);
$this->user->expects(self::any())
->method('getUID')
->willReturn('user1');
$this->user->expects(self::any())
->method('getDisplayName')
->willReturn('Mr. Wizard');
$this->userSession->expects(self::any())
->method('getUser')
->willReturn($this->user);
$this->service->expects(self::once())
->method('getFrom');
$this->service->expects(self::once())
->method('addSubjectAndHeading')
->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting without (!) Boromir', true);
$this->service->expects(self::once())
->method('addBulletList')
->with($this->emailTemplate, $newVevent, $data);
$this->service->expects(self::once())
->method('getAttendeeRsvpOrReqForParticipant')
->willReturn(true);
$this->config->expects(self::once())
->method('getAppValue')
->with('dav', 'invitation_link_recipients', 'yes')
->willReturn('yes');
$this->config->expects(self::once())
->method('getSystemValueInt')
->with('mail_providers_disabled', 0)
->willReturn(1);
$this->service->expects(self::once())
->method('createInvitationToken')
->with($message, $newVevent, 1496912700)
->willReturn('token');
$this->service->expects(self::once())
->method('addResponseButtons')
->with($this->emailTemplate, 'token');
$this->service->expects(self::once())
->method('addMoreOptionsButton')
->with($this->emailTemplate, 'token');
$this->mailer->expects(self::once())
->method('send')
->willReturn([]);
$this->plugin->schedule($message);
$this->assertEquals('1.1', $message->getScheduleStatus());
}

public function testNoOldEvent(): void {
$message = new Message();
$message->method = 'REQUEST';
Expand Down
3 changes: 3 additions & 0 deletions apps/settings/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
use OCP\Group\Events\GroupDeletedEvent;
use OCP\Group\Events\UserAddedEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent;
use OCP\Settings\Events\DeclarativeSettingsSetValueEvent;
use OCP\Settings\Events\DeclarativeSettingsGetValueEvent;
use OCP\IServerContainer;
use OCP\Settings\IManager;
use OCP\Util;
Expand Down
38 changes: 25 additions & 13 deletions apps/settings/lib/Listener/MailSettingsEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,23 @@ public function __construct(

public function handle(Event $event): void {

if ($event->getApp() !== Application::APP_ID) {
if ($event instanceof DeclarativeSettingsRegisterFormEvent) {

Check notice

Code scanning / Psalm

RedundantConditionGivenDocblockType Note

Docblock-defined type OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent for $event is always OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent
$this->handleRegister($event);
return;
}

if ($event instanceof DeclarativeSettingsRegisterFormEvent) {
$this->handleRegister($event);
if ($event->getApp() !== Application::APP_ID) {

Check failure

Code scanning / Psalm

UndefinedMethod Error

Method OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent::getApp does not exist

Check failure on line 36 in apps/settings/lib/Listener/MailSettingsEventListener.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedMethod

apps/settings/lib/Listener/MailSettingsEventListener.php:36:15: UndefinedMethod: Method OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent::getApp does not exist (see https://psalm.dev/022)
return;
}

if ($event instanceof DeclarativeSettingsGetValueEvent) {

Check notice

Code scanning / Psalm

DocblockTypeContradiction Note

Cannot resolve types for $event - docblock-defined type OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent does not contain OCP\Settings\Events\DeclarativeSettingsGetValueEvent
$this->handleGetValue($event);
return;
}

if ($event instanceof DeclarativeSettingsSetValueEvent) {

Check notice

Code scanning / Psalm

DocblockTypeContradiction Note

Cannot resolve types for $event - docblock-defined type OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent does not contain OCP\Settings\Events\DeclarativeSettingsSetValueEvent
$this->handleSetValue($event);
return;
}

}
Expand All @@ -50,20 +53,29 @@ private function handleRegister(Event $event) {

$event->registerSchema(Application::APP_ID, [

Check failure

Code scanning / Psalm

UndefinedMethod Error

Method OCP\EventDispatcher\Event::registerSchema does not exist

Check failure on line 54 in apps/settings/lib/Listener/MailSettingsEventListener.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedMethod

apps/settings/lib/Listener/MailSettingsEventListener.php:54:11: UndefinedMethod: Method OCP\EventDispatcher\Event::registerSchema does not exist (see https://psalm.dev/022)
'id' => 'mail-provider-support',
'priority' => 10,
'priority' => 11,
'section_type' => DeclarativeSettingsTypes::SECTION_TYPE_ADMIN,
'section_id' => 'server',
'storage_type' => DeclarativeSettingsTypes::STORAGE_TYPE_EXTERNAL,
'title' => $this->l->t('System Mails'),
'description' => $this->l->t('Allow to restrict filenames to ensure files can be synced with all clients. By default all filenames valid on POSIX (e.g. Linux or macOS) are allowed.'),
'description' => $this->l->t('System e-mails are messages generated automatically by Nextcloud. They are sent for example when a share is created or when inviting attendees to a calendar event..'),

'fields' => [
[
'id' => 'mail_providers_enabled',
'title' => $this->l->t('Allow system to use configured user mail accounts'),
'description' => $this->l->t('This will block filenames not valid on Windows systems, like using reserved names or special characters. But this will not enforce compatibility of case sensitivity.'),
'type' => DeclarativeSettingsTypes::CHECKBOX,
'default' => false,
'id' => 'mail_providers_disabled',
'title' => $this->l->t('Send system e-mails using'),
'type' => DeclarativeSettingsTypes::RADIO,
'default' => 0,
'options' => [
[
'name' => $this->l->t('People\'s configured mail account'),
'value' => 0
],
[
'name' => $this->l->t('System account'),
'value' => 1
],
],
],
],
]);
Expand All @@ -74,7 +86,7 @@ private function handleGetValue(Event $event) {

$event->setValue(

Check failure

Code scanning / Psalm

UndefinedMethod Error

Method OCP\EventDispatcher\Event::setValue does not exist

Check failure on line 87 in apps/settings/lib/Listener/MailSettingsEventListener.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedMethod

apps/settings/lib/Listener/MailSettingsEventListener.php:87:11: UndefinedMethod: Method OCP\EventDispatcher\Event::setValue does not exist (see https://psalm.dev/022)
match($event->getFieldId()) {
'mail_providers_enabled' => $this->config->getValueBool('core', 'mail_providers_enabled', true),
'mail_providers_disabled' => $this->config->getSystemValueInt('mail_providers_disabled', 0),
}
);

Expand All @@ -83,8 +95,8 @@ private function handleGetValue(Event $event) {
private function handleSetValue(Event $event) {

Check notice

Code scanning / Psalm

MissingReturnType Note

Method OCA\Settings\Listener\MailSettingsEventListener::handleSetValue does not have a return type, expecting void

switch ($event->getFieldId()) {

Check failure

Code scanning / Psalm

UndefinedMethod Error

Method OCP\EventDispatcher\Event::getFieldId does not exist

Check failure on line 97 in apps/settings/lib/Listener/MailSettingsEventListener.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedMethod

apps/settings/lib/Listener/MailSettingsEventListener.php:97:19: UndefinedMethod: Method OCP\EventDispatcher\Event::getFieldId does not exist (see https://psalm.dev/022)
case 'mail_providers_enabled':
$this->config->setValueBool('core', 'mail_providers_enabled', (bool)$event->getValue());
case 'mail_providers_disabled':
$this->config->setSystemValue('mail_providers_disabled', $event->getValue());

Check failure

Code scanning / Psalm

UndefinedMethod Error

Method OCP\EventDispatcher\Event::getValue does not exist

Check failure on line 99 in apps/settings/lib/Listener/MailSettingsEventListener.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedMethod

apps/settings/lib/Listener/MailSettingsEventListener.php:99:70: UndefinedMethod: Method OCP\EventDispatcher\Event::getValue does not exist (see https://psalm.dev/022)
$event->stopPropagation();
break;
}
Expand Down

0 comments on commit d6a1b1b

Please sign in to comment.