@@ -66,8 +73,10 @@ export default class MailPage extends Page {
className: 'MailPage-MailSettings',
children: [
-
-
+
]
})}
@@ -77,20 +86,30 @@ export default class MailPage extends Page {
className: 'MailPage-MailSettings',
children: [
-
-
]
})}
- {Object.keys(this.driverFields[this.values.mail_driver()]).length > 0 && FieldSet.component({
+ {this.status.sending || Alert.component({
+ children: app.translator.trans('core.admin.email.not_sending_message'),
+ dismissible: false,
+ })}
+
+ {fieldKeys.length > 0 && FieldSet.component({
label: app.translator.trans(`core.admin.email.${this.values.mail_driver()}_heading`),
className: 'MailPage-MailSettings',
children: [
- {Object.keys(this.driverFields[this.values.mail_driver()]).map(field => [
-
,
- this.renderField(field),
+ {fieldKeys.map(field => [
+
,
+ this.status.errors[field] &&
{this.status.errors[field]}
,
])}
]
@@ -100,7 +119,6 @@ export default class MailPage extends Page {
type: 'submit',
className: 'Button Button--primary',
children: app.translator.trans('core.admin.email.submit_button'),
- loading: this.saving,
disabled: !this.changed()
})}
@@ -115,7 +133,7 @@ export default class MailPage extends Page {
const prop = this.values[name];
if (typeof field === 'string') {
- return
;
+ return
;
} else {
return
;
}
@@ -144,7 +162,7 @@ export default class MailPage extends Page {
.catch(() => {})
.then(() => {
this.saving = false;
- m.redraw();
+ this.refresh();
});
}
}
diff --git a/less/admin/MailPage.less b/less/admin/MailPage.less
index 964453d74..63348b21f 100644
--- a/less/admin/MailPage.less
+++ b/less/admin/MailPage.less
@@ -8,32 +8,25 @@
}
}
- fieldset {
- margin-bottom: 30px;
+ fieldset, .Alert {
+ margin-bottom: 20px;
+ }
- > ul {
- list-style: none;
- margin: 0;
- padding: 0;
- }
+ fieldset > ul {
+ list-style: none;
+ margin: 0;
+ padding: 0;
}
}
.MailPage-MailSettings-input {
label {
- margin-bottom: 5px;
- }
-
- .FormControl {
+ display: block;
margin-bottom: 7px;
}
.Select {
display: block;
}
-
- :last-child {
- margin-bottom: 0;
- }
}
diff --git a/less/common/ValidationError.less b/less/common/ValidationError.less
new file mode 100644
index 000000000..a6a5018b5
--- /dev/null
+++ b/less/common/ValidationError.less
@@ -0,0 +1,5 @@
+.ValidationError {
+ font-size: 0.9em;
+ font-weight: bold;
+ color: @validation-error-color;
+}
diff --git a/less/common/common.less b/less/common/common.less
index 739a236b5..f62794196 100644
--- a/less/common/common.less
+++ b/less/common/common.less
@@ -26,3 +26,4 @@
@import "Search";
@import "Select";
@import "Tooltip";
+@import "ValidationError";
diff --git a/less/common/variables.less b/less/common/variables.less
index bc2ba5f32..e63ea0dd5 100644
--- a/less/common/variables.less
+++ b/less/common/variables.less
@@ -68,15 +68,19 @@
@hero-color: @control-color;
@hero-muted-color: @control-color;
+@error-color: #d83e3e;
+
@alert-bg: #fff2ae;
@alert-color: #ad6c00;
-@alert-error-bg: #d83e3e;
+@alert-error-bg: @error-color;
@alert-error-color: #fff;
@alert-success-bg: #B4F1AF;
@alert-success-color: #33722D;
+@validation-error-color: @error-color;
+
.define-header(@config-colored-header);
.define-header(false) {
@header-bg: @body-bg;
diff --git a/src/Api/Controller/ListMailDriversController.php b/src/Api/Controller/ListMailDriversController.php
deleted file mode 100644
index b10bd9f6c..000000000
--- a/src/Api/Controller/ListMailDriversController.php
+++ /dev/null
@@ -1,43 +0,0 @@
-assertAdmin($request->getAttribute('actor'));
-
- $drivers = self::$container->make('mail.supported_drivers');
- array_walk($drivers, function (&$driver, $key) {
- $driver = [
- 'id' => $key,
- 'driver' => self::$container->make($driver),
- ];
- });
-
- return $drivers;
- }
-}
diff --git a/src/Api/Controller/ShowMailSettingsController.php b/src/Api/Controller/ShowMailSettingsController.php
new file mode 100644
index 000000000..6fca5ffb1
--- /dev/null
+++ b/src/Api/Controller/ShowMailSettingsController.php
@@ -0,0 +1,56 @@
+assertAdmin($request->getAttribute('actor'));
+
+ $drivers = array_map(function ($driver) {
+ return self::$container->make($driver);
+ }, self::$container->make('mail.supported_drivers'));
+
+ $settings = self::$container->make(SettingsRepositoryInterface::class);
+ $configured = self::$container->make('flarum.mail.configured_driver');
+ $actual = self::$container->make('mail.driver');
+ $validator = self::$container->make(Factory::class);
+
+ if (method_exists($configured, 'validate')) {
+ $errors = $configured->validate($settings, $validator);
+ } else {
+ $errors = new \Illuminate\Support\MessageBag;
+ }
+
+ return [
+ 'drivers' => $drivers,
+ 'sending' => method_exists($actual, 'canSend') ? $actual->canSend() : true,
+ 'errors' => $errors,
+ ];
+ }
+}
diff --git a/src/Api/Serializer/MailDriverSerializer.php b/src/Api/Serializer/MailDriverSerializer.php
deleted file mode 100644
index 03a23dcfe..000000000
--- a/src/Api/Serializer/MailDriverSerializer.php
+++ /dev/null
@@ -1,57 +0,0 @@
-availableSettings();
-
- if (key($settings) === 0) {
- // BACKWARDS COMPATIBILITY: Support a simple list of fields (without
- // type or additional metadata).
- // Turns ["f1", "f2"] into {"f1": "", "f2": ""}
- // @deprecated since 0.1.0-beta.12
- $settings = array_reduce($settings, function ($memo, $key) {
- return [$key => ''] + $memo;
- }, []);
- }
-
- return [
- 'fields' => $settings,
- ];
- }
-
- public function getId($model)
- {
- return $model['id'];
- }
-}
diff --git a/src/Api/Serializer/MailSettingsSerializer.php b/src/Api/Serializer/MailSettingsSerializer.php
new file mode 100644
index 000000000..20cf512fd
--- /dev/null
+++ b/src/Api/Serializer/MailSettingsSerializer.php
@@ -0,0 +1,46 @@
+ array_map([$this, 'serializeDriver'], $settings['drivers']),
+ 'sending' => $settings['sending'],
+ 'errors' => $settings['errors'],
+ ];
+ }
+
+ private function serializeDriver(DriverInterface $driver)
+ {
+ return $driver->availableSettings();
+ }
+
+ public function getId($model)
+ {
+ return 'global';
+ }
+}
diff --git a/src/Api/routes.php b/src/Api/routes.php
index 9ef94abe7..ea591427f 100644
--- a/src/Api/routes.php
+++ b/src/Api/routes.php
@@ -307,10 +307,10 @@ return function (RouteCollection $map, RouteHandlerFactory $route) {
$route->toController(Controller\ClearCacheController::class)
);
- // List available mail drivers and their configuration fields
+ // List available mail drivers, available fields and validation status
$map->get(
- '/mail-drivers',
- 'mailDrivers.index',
- $route->toController(Controller\ListMailDriversController::class)
+ '/mail-settings',
+ 'mailSettings.index',
+ $route->toController(Controller\ShowMailSettingsController::class)
);
};
diff --git a/src/Mail/DriverInterface.php b/src/Mail/DriverInterface.php
index d4c2f18e6..44215a17f 100644
--- a/src/Mail/DriverInterface.php
+++ b/src/Mail/DriverInterface.php
@@ -31,6 +31,28 @@ interface DriverInterface
*/
public function availableSettings(): array;
+ /**
+ * Ensure the given settings are enough to send emails.
+ *
+ * This method is responsible for determining whether the user-provided
+ * values stored in Flarum's settings are "valid" as far as a simple
+ * inspection of these values can determine it. Of course, this does not
+ * mean that the mail server or API will actually accept e.g. credentials.
+ *
+ * Any errors must be wrapped in a {@see \Illuminate\Support\MessageBag}.
+ * If there are no errors, an empty instance can be returned. In the
+ * presence of validation problems with the configured mail driver, Flarum
+ * will fall back to its no-op {@see \Flarum\Mail\NullDriver}.
+ */
+ // TODO: Uncomment for beta.13
+ //public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag;
+
+ /**
+ * Does this driver actually send out emails?
+ */
+ // TODO: Uncomment for beta.13
+ //public function canSend(): bool;
+
/**
* Build a mail transport based on Flarum's current settings.
*/
diff --git a/src/Mail/LogDriver.php b/src/Mail/LogDriver.php
index afa2025b2..d2ef014b5 100644
--- a/src/Mail/LogDriver.php
+++ b/src/Mail/LogDriver.php
@@ -10,7 +10,9 @@
namespace Flarum\Mail;
use Flarum\Settings\SettingsRepositoryInterface;
+use Illuminate\Contracts\Validation\Factory;
use Illuminate\Mail\Transport\LogTransport;
+use Illuminate\Support\MessageBag;
use Psr\Log\LoggerInterface;
use Swift_Transport;
@@ -31,6 +33,16 @@ class LogDriver implements DriverInterface
return [];
}
+ public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
+ {
+ return new MessageBag;
+ }
+
+ public function canSend(): bool
+ {
+ return false;
+ }
+
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
return new LogTransport($this->logger);
diff --git a/src/Mail/MailServiceProvider.php b/src/Mail/MailServiceProvider.php
index c8ca8f0e0..34fd2b504 100644
--- a/src/Mail/MailServiceProvider.php
+++ b/src/Mail/MailServiceProvider.php
@@ -11,7 +11,9 @@ namespace Flarum\Mail;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Settings\SettingsRepositoryInterface;
+use Illuminate\Contracts\Validation\Factory;
use Illuminate\Mail\Mailer;
+use Illuminate\Support\Arr;
use Swift_Mailer;
class MailServiceProvider extends AbstractServiceProvider
@@ -30,14 +32,33 @@ class MailServiceProvider extends AbstractServiceProvider
});
$this->app->singleton('mail.driver', function () {
+ $configured = $this->app->make('flarum.mail.configured_driver');
$settings = $this->app->make(SettingsRepositoryInterface::class);
- $drivers = $this->app->make('mail.supported_drivers');
+ $validator = $this->app->make(Factory::class);
- return $this->app->make($drivers[$settings->get('mail_driver')]);
+ 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);
+ $this->app->singleton('flarum.mail.configured_driver', function () {
+ $drivers = $this->app->make('mail.supported_drivers');
+ $settings = $this->app->make(SettingsRepositoryInterface::class);
+ $driverName = $settings->get('mail_driver');
+
+ $driverClass = Arr::get($drivers, $driverName);
+
+ return $driverClass
+ ? $this->app->make($driverClass)
+ : $this->app->make(NullDriver::class);
+ });
+
$this->app->singleton('swift.mailer', function ($app) {
return new Swift_Mailer(
$app->make('mail.driver')->buildTransport(
diff --git a/src/Mail/MailgunDriver.php b/src/Mail/MailgunDriver.php
index d600db6b4..1d28a05be 100644
--- a/src/Mail/MailgunDriver.php
+++ b/src/Mail/MailgunDriver.php
@@ -11,7 +11,9 @@ namespace Flarum\Mail;
use Flarum\Settings\SettingsRepositoryInterface;
use GuzzleHttp\Client;
+use Illuminate\Contracts\Validation\Factory;
use Illuminate\Mail\Transport\MailgunTransport;
+use Illuminate\Support\MessageBag;
use Swift_Transport;
class MailgunDriver implements DriverInterface
@@ -28,6 +30,20 @@ class MailgunDriver implements DriverInterface
];
}
+ public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
+ {
+ return $validator->make($settings->all(), [
+ 'mail_mailgun_secret' => 'required',
+ 'mail_mailgun_domain' => 'required|regex:/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/',
+ 'mail_mailgun_region' => 'required|in:api.mailgun.net,api.eu.mailgun.net',
+ ])->errors();
+ }
+
+ public function canSend(): bool
+ {
+ return true;
+ }
+
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
return new MailgunTransport(
diff --git a/src/Mail/MandrillDriver.php b/src/Mail/MandrillDriver.php
index 45a0c4051..7ffce3235 100644
--- a/src/Mail/MandrillDriver.php
+++ b/src/Mail/MandrillDriver.php
@@ -11,7 +11,9 @@ namespace Flarum\Mail;
use Flarum\Settings\SettingsRepositoryInterface;
use GuzzleHttp\Client;
+use Illuminate\Contracts\Validation\Factory;
use Illuminate\Mail\Transport\MandrillTransport;
+use Illuminate\Support\MessageBag;
use Swift_Transport;
class MandrillDriver implements DriverInterface
@@ -23,6 +25,18 @@ class MandrillDriver implements DriverInterface
];
}
+ public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
+ {
+ return $validator->make($settings->all(), [
+ 'mail_mandrill_secret' => 'required',
+ ])->errors();
+ }
+
+ public function canSend(): bool
+ {
+ return true;
+ }
+
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
return new MandrillTransport(
diff --git a/src/Mail/NullDriver.php b/src/Mail/NullDriver.php
new file mode 100644
index 000000000..55e651a4a
--- /dev/null
+++ b/src/Mail/NullDriver.php
@@ -0,0 +1,39 @@
+make($settings->all(), [
+ 'mail_ses_key' => 'required',
+ 'mail_ses_secret' => 'required',
+ 'mail_ses_region' => 'required',
+ ])->errors();
+ }
+
+ public function canSend(): bool
+ {
+ return true;
+ }
+
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
$config = [
diff --git a/src/Mail/SmtpDriver.php b/src/Mail/SmtpDriver.php
index b6233cfe8..b9791ab3b 100644
--- a/src/Mail/SmtpDriver.php
+++ b/src/Mail/SmtpDriver.php
@@ -10,6 +10,8 @@
namespace Flarum\Mail;
use Flarum\Settings\SettingsRepositoryInterface;
+use Illuminate\Contracts\Validation\Factory;
+use Illuminate\Support\MessageBag;
use Swift_SmtpTransport;
use Swift_Transport;
@@ -21,11 +23,27 @@ class SmtpDriver implements DriverInterface
'mail_host' => '', // a hostname, IPv4 address or IPv6 wrapped in []
'mail_port' => '', // a number, defaults to 25
'mail_encryption' => '', // "tls" or "ssl"
- 'mail_username' => '', // required
- 'mail_password' => '', // required
+ 'mail_username' => '',
+ 'mail_password' => '',
];
}
+ public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
+ {
+ return $validator->make($settings->all(), [
+ 'mail_host' => 'required',
+ 'mail_port' => 'nullable|integer',
+ 'mail_encryption' => 'nullable|in:tls,ssl',
+ 'mail_username' => 'required',
+ 'mail_password' => 'required',
+ ])->errors();
+ }
+
+ public function canSend(): bool
+ {
+ return true;
+ }
+
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
$transport = new Swift_SmtpTransport(