From 67e452dda5a6ae1954c0fe358be46557d79ccd1d Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Mon, 12 Apr 2021 18:42:22 +0100 Subject: [PATCH] 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')` --- .../core/src/Admin/AdminServiceProvider.php | 1 + framework/core/src/Api/ApiServiceProvider.php | 1 + .../core/src/Forum/ForumServiceProvider.php | 1 + framework/core/src/Http/ActorReference.php | 30 +++++++++++++++++++ .../Http/Middleware/InjectActorReference.php | 27 +++++++++++++++++ framework/core/src/Http/RequestUtil.php | 16 ++++++++-- 6 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 framework/core/src/Http/ActorReference.php create mode 100644 framework/core/src/Http/Middleware/InjectActorReference.php diff --git a/framework/core/src/Admin/AdminServiceProvider.php b/framework/core/src/Admin/AdminServiceProvider.php index a38a850ee..24ba91b29 100644 --- a/framework/core/src/Admin/AdminServiceProvider.php +++ b/framework/core/src/Admin/AdminServiceProvider.php @@ -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, diff --git a/framework/core/src/Api/ApiServiceProvider.php b/framework/core/src/Api/ApiServiceProvider.php index b18a71a7f..aea885adb 100644 --- a/framework/core/src/Api/ApiServiceProvider.php +++ b/framework/core/src/Api/ApiServiceProvider.php @@ -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, diff --git a/framework/core/src/Forum/ForumServiceProvider.php b/framework/core/src/Forum/ForumServiceProvider.php index 301cfa320..0fc3ed579 100644 --- a/framework/core/src/Forum/ForumServiceProvider.php +++ b/framework/core/src/Forum/ForumServiceProvider.php @@ -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, diff --git a/framework/core/src/Http/ActorReference.php b/framework/core/src/Http/ActorReference.php new file mode 100644 index 000000000..9d4a5c66e --- /dev/null +++ b/framework/core/src/Http/ActorReference.php @@ -0,0 +1,30 @@ +actor = $actor; + } + + public function getActor(): User + { + return $this->actor; + } +} diff --git a/framework/core/src/Http/Middleware/InjectActorReference.php b/framework/core/src/Http/Middleware/InjectActorReference.php new file mode 100644 index 000000000..eae7dfb69 --- /dev/null +++ b/framework/core/src/Http/Middleware/InjectActorReference.php @@ -0,0 +1,27 @@ +handle($request); + } +} diff --git a/framework/core/src/Http/RequestUtil.php b/framework/core/src/Http/RequestUtil.php index f14f79969..47f930868 100644 --- a/framework/core/src/Http/RequestUtil.php +++ b/framework/core/src/Http/RequestUtil.php @@ -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; } }