mirror of
https://github.com/flarum/framework.git
synced 2025-02-09 10:48:29 +08:00
![Franz Liedke](/assets/img/avatar_default.png)
* Integration tests: Memoize request handler as well This is useful to send HTTP requests (or their PSR-7 equivalents) through the entire application's middleware stack (instead of talking to specific controllers, which should be considered implementation detail). * Add tests for CSRF token check * Integration tests: Configure vendor path Now that this is possible, make the easy change... * Implement middleware for CSRF token verification This fixes a rather large oversight in Flarum's codebase, which was that we had no explicit CSRF protection using the traditional token approach. The JS frontend was actually sending these tokens, but the backend did not require them. * Accept CSRF token in request body as well * Refactor tests to shorten HTTP requests Multiple tests now provide JSON request bodies, and others copy cookies from previous responses, so let's provide convenient helpers for these. * Fixed issue with tmp/storage/views not existing, this caused tmpname to notice. Fixed csrf test that assumed an access token allows application access, which is actually api token. Improved return type hinting in the StartSession middleware * Using a different setting key now, so that it won't break tests whenever you re-run them once smtp is set. Fixed, badly, the test to create users etc caused by the prepareDatabase flushing all settings by default. * added custom view, now needs translation
148 lines
4.9 KiB
PHP
148 lines
4.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Flarum.
|
|
*
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Flarum\Admin;
|
|
|
|
use Flarum\Event\ConfigureMiddleware;
|
|
use Flarum\Extension\Event\Disabled;
|
|
use Flarum\Extension\Event\Enabled;
|
|
use Flarum\Foundation\AbstractServiceProvider;
|
|
use Flarum\Foundation\Application;
|
|
use Flarum\Foundation\Event\ClearingCache;
|
|
use Flarum\Frontend\AddLocaleAssets;
|
|
use Flarum\Frontend\AddTranslations;
|
|
use Flarum\Frontend\Compiler\Source\SourceCollector;
|
|
use Flarum\Frontend\RecompileFrontendAssets;
|
|
use Flarum\Http\Middleware as HttpMiddleware;
|
|
use Flarum\Http\RouteCollection;
|
|
use Flarum\Http\RouteHandlerFactory;
|
|
use Flarum\Http\UrlGenerator;
|
|
use Flarum\Locale\LocaleManager;
|
|
use Flarum\Settings\Event\Saved;
|
|
use Zend\Stratigility\MiddlewarePipe;
|
|
|
|
class AdminServiceProvider extends AbstractServiceProvider
|
|
{
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function register()
|
|
{
|
|
$this->app->extend(UrlGenerator::class, function (UrlGenerator $url) {
|
|
return $url->addCollection('admin', $this->app->make('flarum.admin.routes'), 'admin');
|
|
});
|
|
|
|
$this->app->singleton('flarum.admin.routes', function () {
|
|
$routes = new RouteCollection;
|
|
$this->populateRoutes($routes);
|
|
|
|
return $routes;
|
|
});
|
|
|
|
$this->app->singleton('flarum.admin.middleware', function (Application $app) {
|
|
$pipe = new MiddlewarePipe;
|
|
|
|
// All requests should first be piped through our global error handler
|
|
if ($app->inDebugMode()) {
|
|
$pipe->pipe($app->make(HttpMiddleware\HandleErrorsWithWhoops::class));
|
|
} else {
|
|
$pipe->pipe($app->make(HttpMiddleware\HandleErrorsWithView::class));
|
|
}
|
|
|
|
$pipe->pipe($app->make(HttpMiddleware\ParseJsonBody::class));
|
|
$pipe->pipe($app->make(HttpMiddleware\StartSession::class));
|
|
$pipe->pipe($app->make(HttpMiddleware\RememberFromCookie::class));
|
|
$pipe->pipe($app->make(HttpMiddleware\AuthenticateWithSession::class));
|
|
$pipe->pipe($app->make(HttpMiddleware\CheckCsrfToken::class));
|
|
$pipe->pipe($app->make(HttpMiddleware\SetLocale::class));
|
|
$pipe->pipe($app->make(Middleware\RequireAdministrateAbility::class));
|
|
|
|
event(new ConfigureMiddleware($pipe, 'admin'));
|
|
|
|
return $pipe;
|
|
});
|
|
|
|
$this->app->afterResolving('flarum.admin.middleware', function (MiddlewarePipe $pipe) {
|
|
$pipe->pipe(new HttpMiddleware\DispatchRoute($this->app->make('flarum.admin.routes')));
|
|
});
|
|
|
|
$this->app->bind('flarum.assets.admin', function () {
|
|
/** @var \Flarum\Frontend\Assets $assets */
|
|
$assets = $this->app->make('flarum.assets.factory')('admin');
|
|
|
|
$assets->js(function (SourceCollector $sources) {
|
|
$sources->addFile(__DIR__.'/../../js/dist/admin.js');
|
|
});
|
|
|
|
$assets->css(function (SourceCollector $sources) {
|
|
$sources->addFile(__DIR__.'/../../less/admin.less');
|
|
});
|
|
|
|
$this->app->make(AddTranslations::class)->forFrontend('admin')->to($assets);
|
|
$this->app->make(AddLocaleAssets::class)->to($assets);
|
|
|
|
return $assets;
|
|
});
|
|
|
|
$this->app->bind('flarum.frontend.admin', function () {
|
|
/** @var \Flarum\Frontend\Frontend $frontend */
|
|
$frontend = $this->app->make('flarum.frontend.factory')('admin');
|
|
|
|
$frontend->content($this->app->make(Content\AdminPayload::class));
|
|
|
|
return $frontend;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function boot()
|
|
{
|
|
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.admin');
|
|
|
|
$events = $this->app->make('events');
|
|
|
|
$events->listen(
|
|
[Enabled::class, Disabled::class, ClearingCache::class],
|
|
function () {
|
|
$recompile = new RecompileFrontendAssets(
|
|
$this->app->make('flarum.assets.admin'),
|
|
$this->app->make(LocaleManager::class)
|
|
);
|
|
$recompile->flush();
|
|
}
|
|
);
|
|
|
|
$events->listen(
|
|
Saved::class,
|
|
function (Saved $event) {
|
|
$recompile = new RecompileFrontendAssets(
|
|
$this->app->make('flarum.assets.admin'),
|
|
$this->app->make(LocaleManager::class)
|
|
);
|
|
$recompile->whenSettingsSaved($event);
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param RouteCollection $routes
|
|
*/
|
|
protected function populateRoutes(RouteCollection $routes)
|
|
{
|
|
$factory = $this->app->make(RouteHandlerFactory::class);
|
|
|
|
$callback = include __DIR__.'/routes.php';
|
|
$callback($routes, $factory);
|
|
}
|
|
}
|