mirror of
https://github.com/flarum/core.git
synced 2025-10-12 23:44:27 +02:00
Change API to use PSR-7 style requests and responses
This required some interface changes (mostly changing Laravel's or Symfony's request and response classes to those of Zend's Diactoros. Some smaller changes to the execution flow in a few of the abstract action base classes, but nothing substantial. Note: The request and response classes are immutable, so we usually need to return new instances after modifying the old ones.
This commit is contained in:
@@ -2,12 +2,20 @@
|
||||
|
||||
use Flarum\Core\Models\AccessToken;
|
||||
use Flarum\Support\Actor;
|
||||
use Closure;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Zend\Stratigility\MiddlewareInterface;
|
||||
|
||||
class LoginWithHeader
|
||||
class LoginWithHeader implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @var Actor
|
||||
*/
|
||||
protected $actor;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix = 'Token ';
|
||||
|
||||
// @todo rather than using a singleton, we should have our own HTTP
|
||||
@@ -17,17 +25,21 @@ class LoginWithHeader
|
||||
$this->actor = $actor;
|
||||
}
|
||||
|
||||
public function handle($request, Closure $next)
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
||||
{
|
||||
$header = $request->headers->get('authorization');
|
||||
$header = $request->getHeaderLine('authorization');
|
||||
if (starts_with($header, $this->prefix) &&
|
||||
($token = substr($header, strlen($this->prefix))) &&
|
||||
($accessToken = AccessToken::where('id', $token)->first())) {
|
||||
($accessToken = AccessToken::where('id', $token)->first())
|
||||
) {
|
||||
$this->actor->setUser($user = $accessToken->user);
|
||||
|
||||
$user->updateLastSeen()->save();
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
return $out ? $out($request, $response) : $response;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user