Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access request actor in error handler #2410

Merged
merged 6 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Admin/AdminServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function register()

$this->container->singleton('flarum.admin.middleware', function () {
return [
HttpMiddleware\InjectActorReference::class,
'flarum.admin.error_handler',
HttpMiddleware\ParseJsonBody::class,
HttpMiddleware\StartSession::class,
Expand Down
1 change: 1 addition & 0 deletions src/Api/ApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function register()

$this->container->singleton('flarum.api.middleware', function () {
return [
HttpMiddleware\InjectActorReference::class,
'flarum.api.error_handler',
HttpMiddleware\ParseJsonBody::class,
Middleware\FakeHttpMethods::class,
Expand Down
1 change: 1 addition & 0 deletions src/Forum/ForumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function register()

$this->container->singleton('flarum.forum.middleware', function () {
return [
HttpMiddleware\InjectActorReference::class,
'flarum.forum.error_handler',
HttpMiddleware\ParseJsonBody::class,
HttpMiddleware\CollectGarbage::class,
Expand Down
30 changes: 30 additions & 0 deletions src/Http/ActorReference.php
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions src/Http/Middleware/InjectActorReference.php
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 2 deletions src/Http/RequestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
SychO9 marked this conversation as resolved.
Show resolved Hide resolved

$actorReference->setActor($actor);

// @deprecated in 1.0
$request = $request->withAttribute('actor', $actor);

return $request;
}
}