Add required fields, incomplete configuration warning, and null transport

This commit is contained in:
David Sevilla Martin 2019-10-27 12:40:15 -04:00 committed by Franz Liedke
parent a330a8fa28
commit 8b70cec6a1
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
11 changed files with 97 additions and 7 deletions

View File

@ -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 (
<div className="MailPage">
<div className="container">
@ -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,
}),
<div className="MailPage-MailSettings-input">
{Object.keys(this.driverFields[this.values.mail_driver()]).map(field => [
<label>{app.translator.trans(`core.admin.email.${field}_label`)}</label>,
{Object.keys(fields).map(field => [
<label>{app.translator.trans(`core.admin.email.${field}_label`)} {this.fieldsRequired.includes(field) ? '*' : ''}</label>,
this.renderField(field),
])}
</div>
@ -115,7 +125,7 @@ export default class MailPage extends Page {
const prop = this.values[name];
if (typeof field === 'string') {
return <input className="FormControl" value={prop() || ''} oninput={m.withAttr('value', prop)}/>;
return <input className="FormControl" value={prop() || ''} oninput={m.withAttr('value', prop)} required={this.fieldsRequired.includes(field)} />;
} else {
return <Select value={prop()} options={field} onchange={prop} />;
}

View File

@ -47,6 +47,7 @@ class MailDriverSerializer extends AbstractSerializer
return [
'fields' => $settings,
'fieldsRequired' => $driver['driver']->requiredFields(),
];
}

View File

@ -31,6 +31,11 @@ interface DriverInterface
*/
public function availableSettings(): array;
/**
* Provide a list of required settings for this driver.
*/
public function requiredFields(): array;
/**
* Build a mail transport based on Flarum's current settings.
*/

View File

@ -31,6 +31,11 @@ class LogDriver implements DriverInterface
return [];
}
public function requiredFields(): array
{
return [];
}
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
return new LogTransport($this->logger);

View File

@ -33,7 +33,15 @@ class MailServiceProvider extends AbstractServiceProvider
$settings = $this->app->make(SettingsRepositoryInterface::class);
$drivers = $this->app->make('mail.supported_drivers');
return $this->app->make($drivers[$settings->get('mail_driver')]);
$driver = $this->app->make($drivers[$settings->get('mail_driver')]);
// check that all required fields have been filled
$settings = $this->app->make(SettingsRepositoryInterface::class);
$valid = $driver && array_reduce($driver->requiredFields(), function ($carry, $field) use ($settings) {
return $carry && ! empty($settings->get($field));
}, true);
return $valid ? $driver : $this->app->make(NullDriver::class);
});
$this->app->alias('mail.driver', DriverInterface::class);

View File

@ -28,6 +28,11 @@ class MailgunDriver implements DriverInterface
];
}
public function requiredFields(): array
{
return $this->availableSettings();
}
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
return new MailgunTransport(

View File

@ -23,6 +23,11 @@ class MandrillDriver implements DriverInterface
];
}
public function requiredFields(): array
{
return $this->availableSettings();
}
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
return new MandrillTransport(

32
src/Mail/NullDriver.php Normal file
View File

@ -0,0 +1,32 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Mail;
use Flarum\Settings\SettingsRepositoryInterface;
use Swift_NullTransport;
use Swift_Transport;
class NullDriver implements DriverInterface
{
public function availableSettings(): array
{
return [];
}
public function requiredFields(): array
{
return [];
}
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
return new Swift_NullTransport();
}
}

View File

@ -20,6 +20,11 @@ class SendmailDriver implements DriverInterface
return [];
}
public function requiredFields(): array
{
return [];
}
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
return new Swift_SendmailTransport;

View File

@ -25,6 +25,11 @@ class SesDriver implements DriverInterface
];
}
public function requiredFields(): array
{
return $this->availableSettings();
}
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
{
$config = [

View File

@ -21,8 +21,17 @@ 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 requiredFields(): array
{
return [
'mail_host',
'mail_username',
'mail_password',
];
}