Switch admin app to new PSR-7 driven architecture

This commit is contained in:
Franz Liedke 2015-05-27 03:02:10 +02:00
parent c3c82a9b4f
commit bbd2625752
3 changed files with 30 additions and 23 deletions

View File

@ -1,18 +1,17 @@
<?php namespace Flarum\Admin\Actions;
use Flarum\Support\Action;
use Illuminate\Http\Request;
use Flarum\Support\HtmlAction;
use Session;
use Auth;
use Cookie;
use Config;
use View;
use DB;
use Flarum\Api\Request as ApiRequest;
use Psr\Http\Message\ServerRequestInterface as Request;
class IndexAction extends Action
class IndexAction extends HtmlAction
{
public function handle(Request $request, $params = [])
protected function render(Request $request, $routeParams = [])
{
$config = DB::table('config')->whereIn('key', ['base_url', 'api_url', 'forum_title', 'welcome_title', 'welcome_message'])->lists('value', 'key');
$data = [];
@ -35,7 +34,7 @@ class IndexAction extends Action
}
}
$view = View::make('flarum.admin::index')
$view = view('flarum.admin::index')
->with('title', 'Administration - '.Config::get('flarum::forum_title', 'Flarum Demo Forum'))
->with('config', $config)
->with('layout', 'flarum.admin::admin')

View File

@ -2,10 +2,15 @@
use Flarum\Support\Actor;
use Flarum\Core\Models\AccessToken;
use Closure;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class LoginWithCookieAndCheckAdmin
class LoginWithCookieAndCheckAdmin implements MiddlewareInterface
{
/**
* @var Actor
*/
protected $actor;
public function __construct(Actor $actor)
@ -13,16 +18,22 @@ class LoginWithCookieAndCheckAdmin
$this->actor = $actor;
}
public function handle($request, Closure $next)
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
if (($token = $request->cookie('flarum_remember')) &&
$cookies = $request->getCookieParams();
if (($token = $cookies['flarum_remember']) &&
($accessToken = AccessToken::where('id', $token)->first()) &&
$accessToken->user->isAdmin()) {
$accessToken->user->isAdmin()
) {
$this->actor->setUser($accessToken->user);
} else {
die('ur not an admin');
}
return $next($request);
return $out ? $out($request, $response) : $response;
}
}

View File

@ -1,19 +1,16 @@
<?php
use Psr\Http\Message\ServerRequestInterface;
$action = function ($class) {
return function () use ($class) {
return function (ServerRequestInterface $httpRequest, $routeParams) use ($class) {
$action = $this->app->make($class);
$request = $this->app['request']->instance();
$parameters = $this->app['router']->current()->parameters();
return $action->handle($request, $parameters);
return $action->handle($httpRequest, $routeParams);
};
};
Route::group(['prefix' => 'admin', 'middleware' => 'Flarum\Admin\Middleware\LoginWithCookieAndCheckAdmin'], function () use ($action) {
/** @var Flarum\Http\Router $router */
$router = $this->app->make('Flarum\Http\Router');
Route::get('/', [
'as' => 'flarum.admin.index',
'uses' => $action('Flarum\Admin\Actions\IndexAction')
]);
});
$router->get('/admin', 'flarum.admin.index', $action('Flarum\Admin\Actions\IndexAction'));