mirror of
https://github.com/flarum/framework.git
synced 2025-01-30 22:38:43 +08:00
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:
parent
0fb3548f46
commit
67e452dda5
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
30
framework/core/src/Http/ActorReference.php
Normal file
30
framework/core/src/Http/ActorReference.php
Normal 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;
|
||||
}
|
||||
}
|
27
framework/core/src/Http/Middleware/InjectActorReference.php
Normal file
27
framework/core/src/Http/Middleware/InjectActorReference.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user