mirror of
https://github.com/flarum/core.git
synced 2025-08-06 08:27:42 +02:00
Merge pull request #1921 from flarum/ds/1763-handle-incomplete-email-configuration
Improve handling of incomplete mail configuration
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
<?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\Api\Controller;
|
||||
|
||||
use Flarum\Api\Serializer\MailDriverSerializer;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
class ListMailDriversController extends AbstractListController
|
||||
{
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $serializer = MailDriverSerializer::class;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function data(ServerRequestInterface $request, Document $document)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
56
src/Api/Controller/ShowMailSettingsController.php
Normal file
56
src/Api/Controller/ShowMailSettingsController.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?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\Api\Controller;
|
||||
|
||||
use Flarum\Api\Serializer\MailSettingsSerializer;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
class ShowMailSettingsController extends AbstractShowController
|
||||
{
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $serializer = MailSettingsSerializer::class;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function data(ServerRequestInterface $request, Document $document)
|
||||
{
|
||||
$this->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,
|
||||
];
|
||||
}
|
||||
}
|
@@ -1,57 +0,0 @@
|
||||
<?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\Api\Serializer;
|
||||
|
||||
use Flarum\Mail\DriverInterface;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class MailDriverSerializer extends AbstractSerializer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $type = 'mail-drivers';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param \Flarum\Mail\DriverInterface $driver
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function getDefaultAttributes($driver)
|
||||
{
|
||||
if (! ($driver['driver'] instanceof DriverInterface)) {
|
||||
throw new InvalidArgumentException(
|
||||
get_class($this).' can only serialize instances of '.DriverInterface::class
|
||||
);
|
||||
}
|
||||
|
||||
$settings = $driver['driver']->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'];
|
||||
}
|
||||
}
|
46
src/Api/Serializer/MailSettingsSerializer.php
Normal file
46
src/Api/Serializer/MailSettingsSerializer.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?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\Api\Serializer;
|
||||
|
||||
use Flarum\Mail\DriverInterface;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class MailSettingsSerializer extends AbstractSerializer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $type = 'mail-settings';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param array $settings
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function getDefaultAttributes($settings)
|
||||
{
|
||||
return [
|
||||
'fields' => 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';
|
||||
}
|
||||
}
|
@@ -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)
|
||||
);
|
||||
};
|
||||
|
Reference in New Issue
Block a user