Access request actor in error handler (#2410)

* Add an ActorReference class to store the actor `$request->getAttribute('actorReference')->getActor()`
* Add a middleware to inject the actor reference
* Deprecate `$request->getAttribute('actor')`
This commit is contained in:
Sami Mazouz 2021-04-12 18:42:22 +01:00 committed by GitHub
parent 0fb3548f46
commit 67e452dda5
6 changed files with 74 additions and 2 deletions

View File

@ -49,6 +49,7 @@ class AdminServiceProvider extends AbstractServiceProvider
$this->container->singleton('flarum.admin.middleware', function () {
return [
HttpMiddleware\InjectActorReference::class,
'flarum.admin.error_handler',
HttpMiddleware\ParseJsonBody::class,
HttpMiddleware\StartSession::class,

View File

@ -57,6 +57,7 @@ class ApiServiceProvider extends AbstractServiceProvider
$this->container->singleton('flarum.api.middleware', function () {
return [
HttpMiddleware\InjectActorReference::class,
'flarum.api.error_handler',
HttpMiddleware\ParseJsonBody::class,
Middleware\FakeHttpMethods::class,

View File

@ -58,6 +58,7 @@ class ForumServiceProvider extends AbstractServiceProvider
$this->container->singleton('flarum.forum.middleware', function () {
return [
HttpMiddleware\InjectActorReference::class,
'flarum.forum.error_handler',
HttpMiddleware\ParseJsonBody::class,
HttpMiddleware\CollectGarbage::class,

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Http;
use Flarum\User\User;
class ActorReference
{
/**
* @var User
*/
private $actor;
public function setActor(User $actor)
{
$this->actor = $actor;
}
public function getActor(): User
{
return $this->actor;
}
}

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Http\Middleware;
use Flarum\Http\RequestUtil;
use Flarum\User\Guest;
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 InjectActorReference implements Middleware
{
public function process(Request $request, Handler $handler): Response
{
$request = RequestUtil::withActor($request, new Guest);
return $handler->handle($request);
}
}

View File

@ -16,11 +16,23 @@ class RequestUtil
{
public static function getActor(Request $request): User
{
return $request->getAttribute('actor');
return $request->getAttribute('actorReference')->getActor();
}
public static function withActor(Request $request, User $actor): Request
{
return $request->withAttribute('actor', $actor);
$actorReference = $request->getAttribute('actorReference');
if (! $actorReference) {
$actorReference = new ActorReference;
$request = $request->withAttribute('actorReference', $actorReference);
}
$actorReference->setActor($actor);
// @deprecated in 1.0
$request = $request->withAttribute('actor', $actor);
return $request;
}
}