From cce92cc56970d7ed68931d8c73171d7b1ffb736c Mon Sep 17 00:00:00 2001 From: SebastianKrupinski Date: Wed, 18 Sep 2024 10:49:23 -0400 Subject: [PATCH] fixup! feat: mail provider settings Signed-off-by: SebastianKrupinski --- apps/dav/lib/CalDAV/Schedule/IMipPlugin.php | 10 +- apps/dav/lib/Server.php | 2 +- .../unit/CalDAV/Schedule/IMipPluginTest.php | 26 ++--- .../composer/composer/autoload_classmap.php | 3 +- .../composer/composer/autoload_static.php | 3 +- apps/settings/lib/AppInfo/Application.php | 15 ++- .../Listener/MailSettingsEventListener.php | 108 ------------------ .../Listener/SystemMailSettingsListener.php | 65 +++++++++++ .../lib/Settings/Admin/SystemMail.php | 51 +++++++++ 9 files changed, 148 insertions(+), 135 deletions(-) delete mode 100644 apps/settings/lib/Listener/MailSettingsEventListener.php create mode 100644 apps/settings/lib/Listener/SystemMailSettingsListener.php create mode 100644 apps/settings/lib/Settings/Admin/SystemMail.php diff --git a/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php b/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php index 5fa2e78581f6f..182a27c7da37f 100644 --- a/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php +++ b/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php @@ -12,7 +12,7 @@ use OCA\DAV\CalDAV\EventComparisonService; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Defaults; -use OCP\IConfig; +use OCP\IAppConfig; use OCP\IUserSession; use OCP\Mail\IMailer; use OCP\Mail\Provider\IManager as IMailManager; @@ -44,7 +44,7 @@ */ class IMipPlugin extends SabreIMipPlugin { private IUserSession $userSession; - private IConfig $config; + private IAppConfig $config; private IMailer $mailer; private LoggerInterface $logger; private ITimeFactory $timeFactory; @@ -59,7 +59,7 @@ class IMipPlugin extends SabreIMipPlugin { private EventComparisonService $eventComparisonService; private IMailManager $mailManager; - public function __construct(IConfig $config, + public function __construct(IAppConfig $config, IMailer $mailer, LoggerInterface $logger, ITimeFactory $timeFactory, @@ -246,7 +246,7 @@ public function schedule(Message $iTipMessage) { */ $recipientDomain = substr(strrchr($recipient, '@'), 1); - $invitationLinkRecipients = explode(',', preg_replace('/\s+/', '', strtolower($this->config->getAppValue('dav', 'invitation_link_recipients', 'yes')))); + $invitationLinkRecipients = explode(',', preg_replace('/\s+/', '', strtolower($this->config->getValueString('dav', 'invitation_link_recipients', 'yes')))); if (strcmp('yes', $invitationLinkRecipients[0]) === 0 || in_array(strtolower($recipient), $invitationLinkRecipients) @@ -265,7 +265,7 @@ public function schedule(Message $iTipMessage) { $mailService = null; try { - if ($this->config->getSystemValueInt('mail_providers_disabled', 0) === 0) { + if ($this->config->getValueInt('core','mail_providers_disabled', 0) === 0) { // retrieve user object $user = $this->userSession->getUser(); // evaluate if user object exist diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index 2784e99b5f73b..726fd1f3b353b 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -293,7 +293,7 @@ public function __construct(IRequest $request, string $baseUri) { )); if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') { $this->server->addPlugin(new IMipPlugin( - \OC::$server->get(\OCP\IConfig::class), + \OC::$server->get(\OCP\IAppConfig::class), \OC::$server->get(\OCP\Mail\IMailer::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(\OCP\AppFramework\Utility\ITimeFactory::class), diff --git a/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php b/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php index f573595896706..aeeef8da5428e 100644 --- a/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php +++ b/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php @@ -11,7 +11,7 @@ use OCA\DAV\CalDAV\Schedule\IMipService; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Defaults; -use OCP\IConfig; +use OCP\IAppConfig; use OCP\IUser; use OCP\IUserSession; use OCP\Mail\IAttachment; @@ -52,7 +52,7 @@ class IMipPluginTest extends TestCase { /** @var ITimeFactory|MockObject */ private $timeFactory; - /** @var IConfig|MockObject */ + /** @var IAppConfig|MockObject */ private $config; /** @var IUserSession|MockObject */ @@ -105,7 +105,7 @@ protected function setUp(): void { $this->timeFactory = $this->createMock(ITimeFactory::class); $this->timeFactory->method('getTime')->willReturn(1496912528); // 2017-01-01 - $this->config = $this->createMock(IConfig::class); + $this->config = $this->createMock(IAppConfig::class); $this->user = $this->createMock(IUser::class); @@ -243,7 +243,7 @@ public function testParsingSingle(): void { ->method('getAttendeeRsvpOrReqForParticipant') ->willReturn(true); $this->config->expects(self::once()) - ->method('getAppValue') + ->method('getValueString') ->with('dav', 'invitation_link_recipients', 'yes') ->willReturn('yes'); $this->service->expects(self::once()) @@ -341,7 +341,7 @@ public function testAttendeeIsResource(): void { $this->service->expects(self::never()) ->method('getAttendeeRsvpOrReqForParticipant'); $this->config->expects(self::never()) - ->method('getAppValue'); + ->method('getValueString'); $this->service->expects(self::never()) ->method('createInvitationToken'); $this->service->expects(self::never()) @@ -447,7 +447,7 @@ public function testParsingRecurrence(): void { ->method('getAttendeeRsvpOrReqForParticipant') ->willReturn(true); $this->config->expects(self::once()) - ->method('getAppValue') + ->method('getValueString') ->with('dav', 'invitation_link_recipients', 'yes') ->willReturn('yes'); $this->service->expects(self::once()) @@ -578,7 +578,7 @@ public function testFailedDelivery(): void { ->method('getAttendeeRsvpOrReqForParticipant') ->willReturn(true); $this->config->expects(self::once()) - ->method('getAppValue') + ->method('getValueString') ->with('dav', 'invitation_link_recipients', 'yes') ->willReturn('yes'); $this->service->expects(self::once()) @@ -633,7 +633,7 @@ public function testMailProviderSend(): void { ]; // construct system config mock returns $this->config->expects(self::once()) - ->method('getAppValue') + ->method('getValueString') ->with('dav', 'invitation_link_recipients', 'yes') ->willReturn('yes'); // construct user mock returns @@ -791,12 +791,12 @@ public function testMailProviderDisabled(): void { ->method('getAttendeeRsvpOrReqForParticipant') ->willReturn(true); $this->config->expects(self::once()) - ->method('getAppValue') + ->method('getValueString') ->with('dav', 'invitation_link_recipients', 'yes') ->willReturn('yes'); $this->config->expects(self::once()) - ->method('getSystemValueInt') - ->with('mail_providers_disabled', 0) + ->method('getValueInt') + ->with('core','mail_providers_disabled', 0) ->willReturn(1); $this->service->expects(self::once()) ->method('createInvitationToken') @@ -886,7 +886,7 @@ public function testNoOldEvent(): void { ->method('getAttendeeRsvpOrReqForParticipant') ->willReturn(true); $this->config->expects(self::once()) - ->method('getAppValue') + ->method('getValueString') ->with('dav', 'invitation_link_recipients', 'yes') ->willReturn('yes'); $this->service->expects(self::once()) @@ -979,7 +979,7 @@ public function testNoButtons(): void { ->method('getAttendeeRsvpOrReqForParticipant') ->willReturn(true); $this->config->expects(self::once()) - ->method('getAppValue') + ->method('getValueString') ->with('dav', 'invitation_link_recipients', 'yes') ->willReturn('no'); $this->service->expects(self::never()) diff --git a/apps/settings/composer/composer/autoload_classmap.php b/apps/settings/composer/composer/autoload_classmap.php index 8090ba79fa361..82f15c8a1f2e6 100644 --- a/apps/settings/composer/composer/autoload_classmap.php +++ b/apps/settings/composer/composer/autoload_classmap.php @@ -40,7 +40,7 @@ 'OCA\\Settings\\Hooks' => $baseDir . '/../lib/Hooks.php', 'OCA\\Settings\\Listener\\AppPasswordCreatedActivityListener' => $baseDir . '/../lib/Listener/AppPasswordCreatedActivityListener.php', 'OCA\\Settings\\Listener\\GroupRemovedListener' => $baseDir . '/../lib/Listener/GroupRemovedListener.php', - 'OCA\\Settings\\Listener\\MailSettingsEventListener' => $baseDir . '/../lib/Listener/MailSettingsEventListener.php', + 'OCA\\Settings\\Listener\\SystemMailSettingsListener' => $baseDir . '/../lib/Listener/SystemMailSettingsListener.php', 'OCA\\Settings\\Listener\\UserAddedToGroupActivityListener' => $baseDir . '/../lib/Listener/UserAddedToGroupActivityListener.php', 'OCA\\Settings\\Listener\\UserRemovedFromGroupActivityListener' => $baseDir . '/../lib/Listener/UserRemovedFromGroupActivityListener.php', 'OCA\\Settings\\Mailer\\NewUserMailHelper' => $baseDir . '/../lib/Mailer/NewUserMailHelper.php', @@ -72,6 +72,7 @@ 'OCA\\Settings\\Settings\\Admin\\Security' => $baseDir . '/../lib/Settings/Admin/Security.php', 'OCA\\Settings\\Settings\\Admin\\Server' => $baseDir . '/../lib/Settings/Admin/Server.php', 'OCA\\Settings\\Settings\\Admin\\Sharing' => $baseDir . '/../lib/Settings/Admin/Sharing.php', + 'OCA\\Settings\\Settings\\Admin\\SystemMail' => $baseDir . '/../lib/Settings/Admin/SystemMail.php', 'OCA\\Settings\\Settings\\Admin\\Users' => $baseDir . '/../lib/Settings/Admin/Users.php', 'OCA\\Settings\\Settings\\Personal\\Additional' => $baseDir . '/../lib/Settings/Personal/Additional.php', 'OCA\\Settings\\Settings\\Personal\\PersonalInfo' => $baseDir . '/../lib/Settings/Personal/PersonalInfo.php', diff --git a/apps/settings/composer/composer/autoload_static.php b/apps/settings/composer/composer/autoload_static.php index e2e88f8b4dd45..66f41ef9c7933 100644 --- a/apps/settings/composer/composer/autoload_static.php +++ b/apps/settings/composer/composer/autoload_static.php @@ -55,7 +55,7 @@ class ComposerStaticInitSettings 'OCA\\Settings\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php', 'OCA\\Settings\\Listener\\AppPasswordCreatedActivityListener' => __DIR__ . '/..' . '/../lib/Listener/AppPasswordCreatedActivityListener.php', 'OCA\\Settings\\Listener\\GroupRemovedListener' => __DIR__ . '/..' . '/../lib/Listener/GroupRemovedListener.php', - 'OCA\\Settings\\Listener\\MailSettingsEventListener' => __DIR__ . '/..' . '/../lib/Listener/MailSettingsEventListener.php', + 'OCA\\Settings\\Listener\\SystemMailSettingsListener' => __DIR__ . '/..' . '/../lib/Listener/SystemMailSettingsListener.php', 'OCA\\Settings\\Listener\\UserAddedToGroupActivityListener' => __DIR__ . '/..' . '/../lib/Listener/UserAddedToGroupActivityListener.php', 'OCA\\Settings\\Listener\\UserRemovedFromGroupActivityListener' => __DIR__ . '/..' . '/../lib/Listener/UserRemovedFromGroupActivityListener.php', 'OCA\\Settings\\Mailer\\NewUserMailHelper' => __DIR__ . '/..' . '/../lib/Mailer/NewUserMailHelper.php', @@ -87,6 +87,7 @@ class ComposerStaticInitSettings 'OCA\\Settings\\Settings\\Admin\\Security' => __DIR__ . '/..' . '/../lib/Settings/Admin/Security.php', 'OCA\\Settings\\Settings\\Admin\\Server' => __DIR__ . '/..' . '/../lib/Settings/Admin/Server.php', 'OCA\\Settings\\Settings\\Admin\\Sharing' => __DIR__ . '/..' . '/../lib/Settings/Admin/Sharing.php', + 'OCA\\Settings\\Settings\\Admin\\SystemMail' => __DIR__ . '/..' . '/../lib/Settings/Admin/SystemMail.php', 'OCA\\Settings\\Settings\\Admin\\Users' => __DIR__ . '/..' . '/../lib/Settings/Admin/Users.php', 'OCA\\Settings\\Settings\\Personal\\Additional' => __DIR__ . '/..' . '/../lib/Settings/Personal/Additional.php', 'OCA\\Settings\\Settings\\Personal\\PersonalInfo' => __DIR__ . '/..' . '/../lib/Settings/Personal/PersonalInfo.php', diff --git a/apps/settings/lib/AppInfo/Application.php b/apps/settings/lib/AppInfo/Application.php index 4383a533f2e7e..490342df22c72 100644 --- a/apps/settings/lib/AppInfo/Application.php +++ b/apps/settings/lib/AppInfo/Application.php @@ -15,7 +15,7 @@ use OCA\Settings\Hooks; use OCA\Settings\Listener\AppPasswordCreatedActivityListener; use OCA\Settings\Listener\GroupRemovedListener; -use OCA\Settings\Listener\MailSettingsEventListener; +use OCA\Settings\Listener\SystemMailSettingsListener; use OCA\Settings\Listener\UserAddedToGroupActivityListener; use OCA\Settings\Listener\UserRemovedFromGroupActivityListener; use OCA\Settings\Mailer\NewUserMailHelper; @@ -23,6 +23,7 @@ use OCA\Settings\Search\AppSearch; use OCA\Settings\Search\SectionSearch; use OCA\Settings\Search\UserSearch; +use OCA\Settings\Settings\Admin\SystemMail; use OCA\Settings\SetupChecks\AllowedAdminRanges; use OCA\Settings\SetupChecks\AppDirsWithDifferentOwner; use OCA\Settings\SetupChecks\BruteForceThrottler; @@ -86,7 +87,6 @@ use OCP\Group\Events\UserRemovedEvent; use OCP\IServerContainer; use OCP\Settings\Events\DeclarativeSettingsGetValueEvent; -use OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent; use OCP\Settings\Events\DeclarativeSettingsSetValueEvent; use OCP\Settings\IManager; use OCP\Util; @@ -114,15 +114,18 @@ public function register(IRegistrationContext $context): void { $context->registerEventListener(UserAddedEvent::class, UserAddedToGroupActivityListener::class); $context->registerEventListener(UserRemovedEvent::class, UserRemovedFromGroupActivityListener::class); $context->registerEventListener(GroupDeletedEvent::class, GroupRemovedListener::class); - // Register Mail Settings listeners - $context->registerEventListener(DeclarativeSettingsRegisterFormEvent::class, MailSettingsEventListener::class); - $context->registerEventListener(DeclarativeSettingsGetValueEvent::class, MailSettingsEventListener::class); - $context->registerEventListener(DeclarativeSettingsSetValueEvent::class, MailSettingsEventListener::class); + + // Register SystemMail listeners + $context->registerEventListener(DeclarativeSettingsGetValueEvent::class, SystemMailSettingsListener::class); + $context->registerEventListener(DeclarativeSettingsSetValueEvent::class, SystemMailSettingsListener::class); // Register well-known handlers $context->registerWellKnownHandler(SecurityTxtHandler::class); $context->registerWellKnownHandler(ChangePasswordHandler::class); + // Register Settings Form(s) + $context->registerDeclarativeSettings(SystemMail::class); + /** * Core class wrappers */ diff --git a/apps/settings/lib/Listener/MailSettingsEventListener.php b/apps/settings/lib/Listener/MailSettingsEventListener.php deleted file mode 100644 index 30874f9b21123..0000000000000 --- a/apps/settings/lib/Listener/MailSettingsEventListener.php +++ /dev/null @@ -1,108 +0,0 @@ - */ -class MailSettingsEventListener implements IEventListener { - - public function __construct( - private IConfig $config, - private IL10N $l, - ) { - } - - public function handle(Event $event): void { - - if ($event instanceof DeclarativeSettingsRegisterFormEvent) { - $this->handleRegister($event); - return; - } - - /** @var DeclarativeSettingsGetValueEvent|DeclarativeSettingsSetValueEvent $event */ - if ($event->getApp() !== Application::APP_ID) { - return; - } - - if ($event instanceof DeclarativeSettingsGetValueEvent) { - $this->handleGetValue($event); - return; - } - - if ($event instanceof DeclarativeSettingsSetValueEvent) { - $this->handleSetValue($event); - return; - } - - } - - private function handleRegister(DeclarativeSettingsRegisterFormEvent $event): void { - - $event->registerSchema(Application::APP_ID, [ - 'id' => 'mail-provider-support', - '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('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_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 - ], - ], - ], - ], - ]); - - } - - - private function handleGetValue(DeclarativeSettingsGetValueEvent $event): void { - - $event->setValue( - match($event->getFieldId()) { - 'mail_providers_disabled' => $this->config->getSystemValueInt('mail_providers_disabled', 0), - } - ); - - } - - private function handleSetValue(DeclarativeSettingsSetValueEvent $event): void { - - switch ($event->getFieldId()) { - case 'mail_providers_disabled': - $this->config->setSystemValue('mail_providers_disabled', $event->getValue()); - $event->stopPropagation(); - break; - } - - } - -} diff --git a/apps/settings/lib/Listener/SystemMailSettingsListener.php b/apps/settings/lib/Listener/SystemMailSettingsListener.php new file mode 100644 index 0000000000000..28fd3936f939a --- /dev/null +++ b/apps/settings/lib/Listener/SystemMailSettingsListener.php @@ -0,0 +1,65 @@ + */ +class SystemMailSettingsListener implements IEventListener { + + public function __construct( + private IAppConfig $config + ) { + } + + public function handle(Event $event): void { + + /** @var DeclarativeSettingsGetValueEvent|DeclarativeSettingsSetValueEvent $event */ + if ($event->getApp() !== Application::APP_ID) { + return; + } + + if ($event instanceof DeclarativeSettingsGetValueEvent) { + $this->handleGetValue($event); + return; + } + + if ($event instanceof DeclarativeSettingsSetValueEvent) { + $this->handleSetValue($event); + return; + } + + } + + private function handleGetValue(DeclarativeSettingsGetValueEvent $event): void { + + $event->setValue( + match($event->getFieldId()) { + 'mail_providers_disabled' => $this->config->getValueInt('core', 'mail_providers_disabled', 0), + } + ); + + } + + private function handleSetValue(DeclarativeSettingsSetValueEvent $event): void { + + switch ($event->getFieldId()) { + case 'mail_providers_disabled': + $this->config->setValueInt('core', 'mail_providers_disabled', (int) $event->getValue()); + $event->stopPropagation(); + break; + } + + } + +} diff --git a/apps/settings/lib/Settings/Admin/SystemMail.php b/apps/settings/lib/Settings/Admin/SystemMail.php new file mode 100644 index 0000000000000..d11509455db40 --- /dev/null +++ b/apps/settings/lib/Settings/Admin/SystemMail.php @@ -0,0 +1,51 @@ + 'mail-provider-support', + '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('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_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 + ], + ], + ], + ], + ]; + } + +}