framework/src/Http/Middleware/AuthenticateWithSession.php
Franz Liedke 3680d88fb7
Use PSR-15 middleware standard
This finally adopts the new standardized interfaces instead of the
work-in-progress ones with the `Interop\` prefix.

Since we have now updated to PHP 7.1, we can also use Stratigility
3.0 as the middleware dispatcher.
2018-05-29 00:18:24 +02:00

48 lines
1.2 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\Http\Middleware;
use Flarum\User\Guest;
use Flarum\User\User;
use Illuminate\Contracts\Session\Session;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler;
class AuthenticateWithSession implements Middleware
{
public function process(Request $request, Handler $handler): Response
{
$session = $request->getAttribute('session');
$actor = $this->getActor($session);
$actor->setSession($session);
$request = $request->withAttribute('actor', $actor);
return $handler->handle($request);
}
private function getActor(Session $session)
{
$actor = User::find($session->get('user_id')) ?: new Guest;
if ($actor->exists) {
$actor->updateLastSeen()->save();
}
return $actor;
}
}