From 96d3ae22f2eea732256dcf70f0e92716479ec7d8 Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Sun, 27 Oct 2019 12:40:15 -0400 Subject: [PATCH 1/6] Add required fields, incomplete configuration warning, and null transport --- .../core/js/src/admin/components/MailPage.js | 18 ++++++++--- .../Api/Serializer/MailDriverSerializer.php | 1 + framework/core/src/Mail/DriverInterface.php | 5 +++ framework/core/src/Mail/LogDriver.php | 5 +++ .../core/src/Mail/MailServiceProvider.php | 10 +++++- framework/core/src/Mail/MailgunDriver.php | 5 +++ framework/core/src/Mail/MandrillDriver.php | 5 +++ framework/core/src/Mail/NullDriver.php | 32 +++++++++++++++++++ framework/core/src/Mail/SendmailDriver.php | 5 +++ framework/core/src/Mail/SesDriver.php | 5 +++ framework/core/src/Mail/SmtpDriver.php | 13 ++++++-- 11 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 framework/core/src/Mail/NullDriver.php diff --git a/framework/core/js/src/admin/components/MailPage.js b/framework/core/js/src/admin/components/MailPage.js index 6a7b41e62..dcbbb8f13 100644 --- a/framework/core/js/src/admin/components/MailPage.js +++ b/framework/core/js/src/admin/components/MailPage.js @@ -15,6 +15,7 @@ export default class MailPage extends Page { this.driverFields = {}; this.fields = ['mail_driver', 'mail_from']; + this.fieldsRequired = []; this.values = {}; const settings = app.data.settings; @@ -36,6 +37,8 @@ export default class MailPage extends Page { } } + this.fieldsRequired = response['data'].map(driver => driver['attributes']['fieldsRequired']).flat(); + this.loading = false; m.redraw(); }); @@ -52,6 +55,8 @@ export default class MailPage extends Page { ); } + const fields = this.driverFields[this.values.mail_driver()]; + return (
@@ -83,13 +88,18 @@ export default class MailPage extends Page { ] })} - {Object.keys(this.driverFields[this.values.mail_driver()]).length > 0 && FieldSet.component({ + {Object.keys(fields).length > 0 && FieldSet.component({ label: app.translator.trans(`core.admin.email.${this.values.mail_driver()}_heading`), className: 'MailPage-MailSettings', children: [ + fields.filter(field => this.fieldsRequired.includes(field) && !this.values[field]()).length > 0 && Alert.component({ + children: app.translator.trans('core.admin.email.incomplete_configuration_text'), + dismissible: false, + }), +
- {Object.keys(this.driverFields[this.values.mail_driver()]).map(field => [ - , + {Object.keys(fields).map(field => [ + , this.renderField(field), ])}
@@ -115,7 +125,7 @@ export default class MailPage extends Page { const prop = this.values[name]; if (typeof field === 'string') { - return ; + return ; } else { return ; + return ; } else { return ; + return ; } else { return +
] })} @@ -84,8 +86,10 @@ export default class MailPage extends Page { className: 'MailPage-MailSettings', children: [
- - ({...memo, [val]: val}), {})} onchange={this.values.mail_driver} /> +
] })} @@ -101,8 +105,10 @@ export default class MailPage extends Page { children: [
{fieldKeys.map(field => [ - , - this.renderField(field), + , this.status.errors[field] &&

{this.status.errors[field]}

, ])}
diff --git a/framework/core/less/admin/MailPage.less b/framework/core/less/admin/MailPage.less index 66756ce09..63348b21f 100644 --- a/framework/core/less/admin/MailPage.less +++ b/framework/core/less/admin/MailPage.less @@ -22,18 +22,11 @@ .MailPage-MailSettings-input { label { - margin-bottom: 5px; - } - - .FormControl { + display: block; margin-bottom: 7px; } .Select { display: block; } - - :last-child { - margin-bottom: 0; - } } From b7bda373807b6b7ad83be3f3f04e0a69491b9ac3 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 24 Jan 2020 17:59:39 +0100 Subject: [PATCH 6/6] Add BC layer for mail driver configuration By commenting out the new methods on the `DriverInterface` and checking for these methods' existence before calling them, old implementations in extensions will not break right away. This will be removed after beta.12 is released, giving extension authors about two months time to update their extensions. --- .../src/Api/Controller/ShowMailSettingsController.php | 8 ++++++-- framework/core/src/Mail/DriverInterface.php | 8 ++++---- framework/core/src/Mail/MailServiceProvider.php | 10 +++++++--- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/framework/core/src/Api/Controller/ShowMailSettingsController.php b/framework/core/src/Api/Controller/ShowMailSettingsController.php index d699ee2b0..6fca5ffb1 100644 --- a/framework/core/src/Api/Controller/ShowMailSettingsController.php +++ b/framework/core/src/Api/Controller/ShowMailSettingsController.php @@ -41,11 +41,15 @@ class ShowMailSettingsController extends AbstractShowController $actual = self::$container->make('mail.driver'); $validator = self::$container->make(Factory::class); - $errors = $configured->validate($settings, $validator); + if (method_exists($configured, 'validate')) { + $errors = $configured->validate($settings, $validator); + } else { + $errors = new \Illuminate\Support\MessageBag; + } return [ 'drivers' => $drivers, - 'sending' => $actual->canSend(), + 'sending' => method_exists($actual, 'canSend') ? $actual->canSend() : true, 'errors' => $errors, ]; } diff --git a/framework/core/src/Mail/DriverInterface.php b/framework/core/src/Mail/DriverInterface.php index 17fa26aa9..44215a17f 100644 --- a/framework/core/src/Mail/DriverInterface.php +++ b/framework/core/src/Mail/DriverInterface.php @@ -10,8 +10,6 @@ namespace Flarum\Mail; use Flarum\Settings\SettingsRepositoryInterface; -use Illuminate\Contracts\Validation\Factory; -use Illuminate\Support\MessageBag; use Swift_Transport; /** @@ -46,12 +44,14 @@ interface DriverInterface * presence of validation problems with the configured mail driver, Flarum * will fall back to its no-op {@see \Flarum\Mail\NullDriver}. */ - public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag; + // TODO: Uncomment for beta.13 + //public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag; /** * Does this driver actually send out emails? */ - public function canSend(): bool; + // TODO: Uncomment for beta.13 + //public function canSend(): bool; /** * Build a mail transport based on Flarum's current settings. diff --git a/framework/core/src/Mail/MailServiceProvider.php b/framework/core/src/Mail/MailServiceProvider.php index 024757f2f..34fd2b504 100644 --- a/framework/core/src/Mail/MailServiceProvider.php +++ b/framework/core/src/Mail/MailServiceProvider.php @@ -36,9 +36,13 @@ class MailServiceProvider extends AbstractServiceProvider $settings = $this->app->make(SettingsRepositoryInterface::class); $validator = $this->app->make(Factory::class); - return $configured->validate($settings, $validator)->any() - ? $this->app->make(NullDriver::class) - : $configured; + if (method_exists($configured, 'validate')) { + return $configured->validate($settings, $validator)->any() + ? $this->app->make(NullDriver::class) + : $configured; + } else { + return $configured; + } }); $this->app->alias('mail.driver', DriverInterface::class);