Convert forum app to be PSR-7 compatible.

I also installed one new dependency: a helper library that makes it
easier to read and write cookies, given that there are no helper methods
for these purposes in the PSR-7 standard.
This commit is contained in:
Franz Liedke 2015-06-03 02:04:57 +02:00
parent 7f83552cbb
commit d462eb585e
12 changed files with 165 additions and 100 deletions

View File

@ -16,7 +16,8 @@
"oyejorge/less.php": "dev-master",
"intervention/image": "dev-master",
"psr/http-message": "^1.0@dev",
"nikic/fast-route": "dev-master"
"nikic/fast-route": "dev-master",
"dflydev/fig-cookies": "0.0.*@dev"
},
"require-dev": {
"fzaninotto/faker": "1.4.0",

77
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "8b517a2c5b1d000a443c5ad91c49b2c1",
"hash": "b4919da43ec6d4286a38b1b95b32ae31",
"packages": [
{
"name": "danielstjules/stringy",
@ -62,6 +62,58 @@
],
"time": "2015-02-10 06:19:18"
},
{
"name": "dflydev/fig-cookies",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/dflydev/dflydev-fig-cookies.git",
"reference": "adc6bab48d09585f67338e0d00695bf64a9ee2b8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/dflydev/dflydev-fig-cookies/zipball/adc6bab48d09585f67338e0d00695bf64a9ee2b8",
"reference": "adc6bab48d09585f67338e0d00695bf64a9ee2b8",
"shasum": ""
},
"require": {
"php": ">=5.4",
"psr/http-message": "~1.0"
},
"require-dev": {
"codeclimate/php-test-reporter": "~0.1@dev",
"phpunit/phpunit": "~4.5",
"squizlabs/php_codesniffer": "~2.3"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "0.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Dflydev\\FigCookies\\": "src/Dflydev/FigCookies"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Beau Simensen",
"email": "beau@dflydev.com"
}
],
"description": "Cookies for PSR-7 HTTP Message Interface.",
"keywords": [
"cookies",
"psr-7",
"psr7"
],
"time": "2015-06-02 19:02:56"
},
{
"name": "doctrine/inflector",
"version": "dev-master",
@ -139,7 +191,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/container/zipball/55b81cfeb20745e74957d7ade2773e2bc2510bef",
"url": "https://api.github.com/repos/illuminate/container/zipball/c5a78e53ef15204469b5b072d390af9785a82d32",
"reference": "55b81cfeb20745e74957d7ade2773e2bc2510bef",
"shasum": ""
},
@ -224,7 +276,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/database/zipball/79b6de7d8b76265d5e765ecd828d9e57c7844959",
"url": "https://api.github.com/repos/illuminate/database/zipball/e8933b4765aaa1b65f3f4a3638b6a0ea61b76a5a",
"reference": "79ebeb4c169178a24c5eb7f17db94df01c7dd04d",
"shasum": ""
},
@ -532,7 +584,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/oyejorge/less.php/zipball/b7f01fb8e86f8d77e0f5367715ec756418232e19",
"url": "https://api.github.com/repos/oyejorge/less.php/zipball/fc971e6d3eb54dff3d3eba4734ff207d37cb4e0e",
"reference": "b7f01fb8e86f8d77e0f5367715ec756418232e19",
"shasum": ""
},
@ -701,7 +753,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/tobscure/json-api/zipball/31a74d27fd9ab6a9a9bc911614ceb696504d2c39",
"url": "https://api.github.com/repos/tobscure/json-api/zipball/d6c82a496289569e8907f3aa980ace407a35b45e",
"reference": "ec101f2b95cb3ef40489b778b01beb76c3a5c13f",
"shasum": ""
},
@ -737,7 +789,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/tobscure/permissible/zipball/ac146ee44be5b2c4b99ad065e2cdcd51de1f7860",
"url": "https://api.github.com/repos/tobscure/permissible/zipball/0ba23dd1ed6f5372bf86fa917450cb70d08c012b",
"reference": "ac146ee44be5b2c4b99ad065e2cdcd51de1f7860",
"shasum": ""
},
@ -776,7 +828,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Codeception/Codeception/zipball/cdc3569fedb017eea204b40eae0c82cf726e0107",
"url": "https://api.github.com/repos/Codeception/Codeception/zipball/b33bbcbf4344dbdbba86328c42fd3ef409bb286b",
"reference": "d3cf78c6053f3fdfa4025bfcdb713f91e3ccdbdf",
"shasum": ""
},
@ -1465,7 +1517,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/9ef4b8cbf3e839a44a9b375d8c59e109ac7aa020",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/3ee57a4fa5c2228da154f88239a921c8d54fcedd",
"reference": "9ef4b8cbf3e839a44a9b375d8c59e109ac7aa020",
"shasum": ""
},
@ -1711,7 +1763,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/3afe303d873a4d64c62ef84de491b97b006fbdac",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/ce9857103d2ec224382eb30847941d8322dc5570",
"reference": "3afe303d873a4d64c62ef84de491b97b006fbdac",
"shasum": ""
},
@ -1783,7 +1835,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/74ffb87f527f24616f72460e54b595f508dccb5c",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/253c005852591fd547fc18cd5b7b43a1ec82d8f7",
"reference": "74ffb87f527f24616f72460e54b595f508dccb5c",
"shasum": ""
},
@ -2701,7 +2753,7 @@
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zendframework/zend-escaper/zipball/84e0c15195adfb3fe3efebb459defc65a1e0314d",
"url": "https://api.github.com/repos/zendframework/zend-escaper/zipball/dea2d94022ce072831d3424fbd0fd5514e81eb75",
"reference": "84e0c15195adfb3fe3efebb459defc65a1e0314d",
"shasum": ""
},
@ -2745,7 +2797,8 @@
"oyejorge/less.php": 20,
"intervention/image": 20,
"psr/http-message": 20,
"nikic/fast-route": 20
"nikic/fast-route": 20,
"dflydev/fig-cookies": 20
},
"prefer-stable": false,
"prefer-lowest": false,

View File

@ -1,13 +1,13 @@
<?php namespace Flarum\Forum\Actions;
use Illuminate\Http\Request;
use Flarum\Core\Commands\ConfirmEmailCommand;
use Flarum\Core\Commands\GenerateAccessTokenCommand;
use Flarum\Core\Exceptions\InvalidConfirmationTokenException;
use Psr\Http\Message\ServerRequestInterface as Request;
class ConfirmAction extends BaseAction
{
use MakesRememberCookie;
use WritesRememberCookie;
public function handle(Request $request, $routeParams = [])
{
@ -20,11 +20,12 @@ class ConfirmAction extends BaseAction
return 'Invalid confirmation token';
}
$command = new GenerateAccessTokenCommand($user->id);
$token = $this->dispatch($command);
$token = $this->dispatch(new GenerateAccessTokenCommand($user->id));
return redirect('/')
->withCookie($this->makeRememberCookie($token->id))
->with('alert', ['type' => 'success', 'message' => 'Thanks for confirming!']);
return $this->withRememberCookie(
$this->redirectTo(''),
$token->id
);
// TODO: ->with('alert', ['type' => 'success', 'message' => 'Thanks for confirming!']);
}
}

View File

@ -1,18 +1,17 @@
<?php namespace Flarum\Forum\Actions;
use Illuminate\Http\Request;
use Flarum\Support\HtmlAction;
use Session;
use Auth;
use Cookie;
use Config;
use View;
use DB;
use Flarum\Forum\Events\RenderView;
use Flarum\Api\Request as ApiRequest;
use Psr\Http\Message\ServerRequestInterface as Request;
class IndexAction extends BaseAction
class IndexAction extends HtmlAction
{
public function handle(Request $request, $params = [])
public function render(Request $request, $params = [])
{
$config = DB::table('config')->whereIn('key', ['base_url', 'api_url', 'forum_title', 'welcome_title', 'welcome_message'])->lists('value', 'key');
$data = [];
@ -22,7 +21,7 @@ class IndexAction extends BaseAction
if (($user = $this->actor->getUser()) && $user->exists) {
$session = [
'userId' => $user->id,
'token' => Cookie::get('flarum_remember')
'token' => $request->getCookieParams()['flarum_remember'],
];
$response = app('Flarum\Api\Actions\Users\ShowAction')
@ -35,7 +34,7 @@ class IndexAction extends BaseAction
}
}
$view = View::make('flarum.forum::index')
$view = view('flarum.forum::index')
->with('title', Config::get('flarum::forum_title', 'Flarum Demo Forum'))
->with('config', $config)
->with('layout', 'flarum.forum::forum')

View File

@ -1,13 +1,13 @@
<?php namespace Flarum\Forum\Actions;
use Illuminate\Http\Request;
use Flarum\Forum\Events\UserLoggedIn;
use Flarum\Core\Repositories\UserRepositoryInterface;
use Flarum\Api\Request as ApiRequest;
use Psr\Http\Message\ServerRequestInterface as Request;
class LoginAction extends BaseAction
{
use MakesRememberCookie;
use WritesRememberCookie;
protected $users;
@ -18,13 +18,16 @@ class LoginAction extends BaseAction
public function handle(Request $request, $routeParams = [])
{
$response = app('Flarum\Api\Actions\TokenAction')
->handle(new ApiRequest($request->only('identification', 'password')));
$params = array_only($request->getAttributes(), ['identification', 'password']);
if ($response->getStatusCode() === 200 && ($data = $response->getData()) && ! empty($data->token)) {
$response->withCookie($this->makeRememberCookie($data->token));
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = app('Flarum\Api\Actions\TokenAction')->handle(new ApiRequest($params));
if ($response->getStatusCode() === 200) {
$data = json_decode($response->getBody());
event(new UserLoggedIn($this->users->findOrFail($data->userId), $data->token));
return $this->withRememberCookie($response, $data->token);
}
return $response;

View File

@ -1,11 +1,12 @@
<?php namespace Flarum\Forum\Actions;
use Illuminate\Http\Request;
use Flarum\Forum\Events\UserLoggedOut;
use Cookie;
use Psr\Http\Message\ServerRequestInterface as Request;
class LogoutAction extends BaseAction
{
use WritesRememberCookie;
public function handle(Request $request, $params = [])
{
$user = $this->actor->getUser();
@ -16,11 +17,6 @@ class LogoutAction extends BaseAction
event(new UserLoggedOut($user));
}
return redirect('')->withCookie($this->makeForgetCookie());
}
public function makeForgetCookie()
{
return Cookie::forget('flarum_remember');
return $this->withForgetCookie($this->redirectTo(''));
}
}

View File

@ -1,11 +0,0 @@
<?php namespace Flarum\Forum\Actions;
use Cookie;
trait MakesRememberCookie
{
protected function makeRememberCookie($token)
{
return Cookie::forever('flarum_remember', $token);
}
}

View File

@ -1,11 +1,12 @@
<?php namespace Flarum\Forum\Actions;
use Flarum\Core\Models\ResetToken;
use Illuminate\Http\Request;
use Flarum\Support\HtmlAction;
use Psr\Http\Message\ServerRequestInterface as Request;
class ResetPasswordAction extends BaseAction
class ResetPasswordAction extends HtmlAction
{
public function handle(Request $request, $routeParams = [])
public function render(Request $request, $routeParams = [])
{
$token = array_get($routeParams, 'token');

View File

@ -2,19 +2,19 @@
use Flarum\Core\Models\ResetToken;
use Flarum\Core\Commands\EditUserCommand;
use Illuminate\Http\Request;
use Psr\Http\Message\ServerRequestInterface as Request;
class SavePasswordAction extends BaseAction
{
public function handle(Request $request, $routeParams = [])
{
$token = ResetToken::findOrFail($request->get('token'));
$token = ResetToken::findOrFail($request->getAttribute('token'));
$password = $request->get('password');
$confirmation = $request->get('password_confirmation');
$password = $request->getAttribute('password');
$confirmation = $request->getAttribute('password_confirmation');
if (! $password || $password !== $confirmation) {
return redirect()->back();
return $this->redirectTo(''); // TODO: Redirect back
}
$this->dispatch(
@ -23,6 +23,6 @@ class SavePasswordAction extends BaseAction
$token->delete();
return redirect('');
return $this->redirectTo('');
}
}

View File

@ -0,0 +1,26 @@
<?php namespace Flarum\Forum\Actions;
use Dflydev\FigCookies\FigCookies;
use Dflydev\FigCookies\SetCookie;
use Psr\Http\Message\ResponseInterface;
trait WritesRememberCookie
{
protected function withRememberCookie(ResponseInterface $response, $token)
{
// Set a long-living cookie (two weeks) with the remember token
return FigCookies::setResponseSetCookie(
$response,
SetCookie::create('flarum_remember', $token)->withMaxAge(14 * 24 * 60 * 60)
);
}
protected function withForgetCookie(ResponseInterface $response)
{
// Delete the cookie by setting it to an expiration date in the past
return FigCookies::setResponseSetCookie(
$response,
SetCookie::create('flarum_remember')->withMaxAge(-2628000)
);
}
}

View File

@ -2,11 +2,15 @@
use Flarum\Support\Actor;
use Flarum\Core\Models\AccessToken;
use Auth;
use Closure;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class LoginWithCookie
class LoginWithCookie implements MiddlewareInterface
{
/**
* @var Actor
*/
protected $actor;
public function __construct(Actor $actor)
@ -14,16 +18,21 @@ class LoginWithCookie
$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')) &&
($accessToken = AccessToken::where('id', $token)->first()) &&
($user = $accessToken->user)) {
$this->actor->setUser($user);
$cookies = $request->getCookieParams();
if (($token = $cookies['flarum_remember']) &&
($accessToken = AccessToken::where('id', $token)->first())
) {
$this->actor->setUser($user = $accessToken->user);
$user->updateLastSeen()->save();
}
return $next($request);
return $out ? $out($request, $response) : $response;
}
}

View File

@ -1,44 +1,31 @@
<?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(['middleware' => 'Flarum\Forum\Middleware\LoginWithCookie'], function () use ($action) {
/** @var Flarum\Http\Router $router */
$router = $this->app->make('Flarum\Http\Router');
Route::get('/', [
'as' => 'flarum.forum.index',
'uses' => $action('Flarum\Forum\Actions\IndexAction')
]);
/**
* Route::group(['middleware' => 'Flarum\Forum\Middleware\LoginWithCookie'], function () use ($action) {
* For the two below
*/
Route::get('logout', [
'as' => 'flarum.forum.logout',
'uses' => $action('Flarum\Forum\Actions\LogoutAction')
]);
$router->get('/', 'flarum.forum.index', $action('Flarum\Forum\Actions\IndexAction'));
});
$router->get('/logout', 'flarum.forum.logout', $action('Flarum\Forum\Actions\LogoutAction'));
Route::post('login', [
'as' => 'flarum.forum.login',
'uses' => $action('Flarum\Forum\Actions\LoginAction')
]);
$router->post('/login', 'flarum.forum.login', $action('Flarum\Forum\Actions\LoginAction'));
Route::get('confirm/{id}/{token}', [
'as' => 'flarum.forum.confirm',
'uses' => $action('Flarum\Forum\Actions\ConfirmAction')
]);
$router->get('/confirm/{id}/{token}', 'flarum.forum.confirm', $action('Flarum\Forum\Actions\ConfirmAction'));
Route::get('reset/{token}', [
'as' => 'flarum.forum.resetPassword',
'uses' => $action('Flarum\Forum\Actions\ResetPasswordAction')
]);
$router->get('/reset/{token}', 'flarum.forum.resetPassword', $action('Flarum\Forum\Actions\ResetPasswordAction'));
Route::post('reset', [
'as' => 'flarum.forum.savePassword',
'uses' => $action('Flarum\Forum\Actions\SavePasswordAction')
]);
$router->post('/reset', 'flarum.forum.savePassword', $action('Flarum\Forum\Actions\SavePasswordAction'));