mirror of
https://github.com/flarum/framework.git
synced 2025-02-06 09:51:18 +08:00
Add required fields, incomplete configuration warning, and null transport
This commit is contained in:
parent
6f6958dd6b
commit
96d3ae22f2
|
@ -15,6 +15,7 @@ export default class MailPage extends Page {
|
||||||
|
|
||||||
this.driverFields = {};
|
this.driverFields = {};
|
||||||
this.fields = ['mail_driver', 'mail_from'];
|
this.fields = ['mail_driver', 'mail_from'];
|
||||||
|
this.fieldsRequired = [];
|
||||||
this.values = {};
|
this.values = {};
|
||||||
|
|
||||||
const settings = app.data.settings;
|
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;
|
this.loading = false;
|
||||||
m.redraw();
|
m.redraw();
|
||||||
});
|
});
|
||||||
|
@ -52,6 +55,8 @@ export default class MailPage extends Page {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fields = this.driverFields[this.values.mail_driver()];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="MailPage">
|
<div className="MailPage">
|
||||||
<div className="container">
|
<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`),
|
label: app.translator.trans(`core.admin.email.${this.values.mail_driver()}_heading`),
|
||||||
className: 'MailPage-MailSettings',
|
className: 'MailPage-MailSettings',
|
||||||
children: [
|
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">
|
<div className="MailPage-MailSettings-input">
|
||||||
{Object.keys(this.driverFields[this.values.mail_driver()]).map(field => [
|
{Object.keys(fields).map(field => [
|
||||||
<label>{app.translator.trans(`core.admin.email.${field}_label`)}</label>,
|
<label>{app.translator.trans(`core.admin.email.${field}_label`)} {this.fieldsRequired.includes(field) ? '*' : ''}</label>,
|
||||||
this.renderField(field),
|
this.renderField(field),
|
||||||
])}
|
])}
|
||||||
</div>
|
</div>
|
||||||
|
@ -115,7 +125,7 @@ export default class MailPage extends Page {
|
||||||
const prop = this.values[name];
|
const prop = this.values[name];
|
||||||
|
|
||||||
if (typeof field === 'string') {
|
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 {
|
} else {
|
||||||
return <Select value={prop()} options={field} onchange={prop} />;
|
return <Select value={prop()} options={field} onchange={prop} />;
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,7 @@ class MailDriverSerializer extends AbstractSerializer
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'fields' => $settings,
|
'fields' => $settings,
|
||||||
|
'fieldsRequired' => $driver['driver']->requiredFields(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,11 @@ interface DriverInterface
|
||||||
*/
|
*/
|
||||||
public function availableSettings(): array;
|
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.
|
* Build a mail transport based on Flarum's current settings.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -31,6 +31,11 @@ class LogDriver implements DriverInterface
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function requiredFields(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||||
{
|
{
|
||||||
return new LogTransport($this->logger);
|
return new LogTransport($this->logger);
|
||||||
|
|
|
@ -33,7 +33,15 @@ class MailServiceProvider extends AbstractServiceProvider
|
||||||
$settings = $this->app->make(SettingsRepositoryInterface::class);
|
$settings = $this->app->make(SettingsRepositoryInterface::class);
|
||||||
$drivers = $this->app->make('mail.supported_drivers');
|
$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);
|
$this->app->alias('mail.driver', DriverInterface::class);
|
||||||
|
|
|
@ -28,6 +28,11 @@ class MailgunDriver implements DriverInterface
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function requiredFields(): array
|
||||||
|
{
|
||||||
|
return $this->availableSettings();
|
||||||
|
}
|
||||||
|
|
||||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||||
{
|
{
|
||||||
return new MailgunTransport(
|
return new MailgunTransport(
|
||||||
|
|
|
@ -23,6 +23,11 @@ class MandrillDriver implements DriverInterface
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function requiredFields(): array
|
||||||
|
{
|
||||||
|
return $this->availableSettings();
|
||||||
|
}
|
||||||
|
|
||||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||||
{
|
{
|
||||||
return new MandrillTransport(
|
return new MandrillTransport(
|
||||||
|
|
32
framework/core/src/Mail/NullDriver.php
Normal file
32
framework/core/src/Mail/NullDriver.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,6 +20,11 @@ class SendmailDriver implements DriverInterface
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function requiredFields(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||||
{
|
{
|
||||||
return new Swift_SendmailTransport;
|
return new Swift_SendmailTransport;
|
||||||
|
|
|
@ -25,6 +25,11 @@ class SesDriver implements DriverInterface
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function requiredFields(): array
|
||||||
|
{
|
||||||
|
return $this->availableSettings();
|
||||||
|
}
|
||||||
|
|
||||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||||
{
|
{
|
||||||
$config = [
|
$config = [
|
||||||
|
|
|
@ -21,8 +21,17 @@ class SmtpDriver implements DriverInterface
|
||||||
'mail_host' => '', // a hostname, IPv4 address or IPv6 wrapped in []
|
'mail_host' => '', // a hostname, IPv4 address or IPv6 wrapped in []
|
||||||
'mail_port' => '', // a number, defaults to 25
|
'mail_port' => '', // a number, defaults to 25
|
||||||
'mail_encryption' => '', // "tls" or "ssl"
|
'mail_encryption' => '', // "tls" or "ssl"
|
||||||
'mail_username' => '', // required
|
'mail_username' => '',
|
||||||
'mail_password' => '', // required
|
'mail_password' => '',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function requiredFields(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'mail_host',
|
||||||
|
'mail_username',
|
||||||
|
'mail_password',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user