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

Centralized IP Handler #2624

Merged
merged 4 commits into from
Feb 26, 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
2 changes: 1 addition & 1 deletion src/Api/Controller/CreateDiscussionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(Dispatcher $bus)
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = $request->getAttribute('actor');
$ipAddress = Arr::get($request->getServerParams(), 'REMOTE_ADDR', '127.0.0.1');
$ipAddress = $request->getAttribute('ipAddress');

$discussion = $this->bus->dispatch(
new StartDiscussion($actor, Arr::get($request->getParsedBody(), 'data', []), $ipAddress)
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Controller/CreatePostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function data(ServerRequestInterface $request, Document $document)
$actor = $request->getAttribute('actor');
$data = Arr::get($request->getParsedBody(), 'data', []);
$discussionId = Arr::get($data, 'relationships.discussion.data.id');
$ipAddress = Arr::get($request->getServerParams(), 'REMOTE_ADDR', '127.0.0.1');
$ipAddress = $request->getAttribute('ipAddress');

$post = $this->bus->dispatch(
new PostReply($discussionId, $actor, $data, $ipAddress)
Expand Down
1 change: 1 addition & 0 deletions src/Foundation/InstalledApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function getRequestHandler()

$pipe = new MiddlewarePipe;

$pipe->pipe(new HttpMiddleware\ProcessIp());
$pipe->pipe(new BasePath($this->basePath()));
$pipe->pipe(new OriginalMessages);
$pipe->pipe(
Expand Down
26 changes: 26 additions & 0 deletions src/Http/Middleware/ProcessIp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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 Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface;

class ProcessIp implements Middleware
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$ipAddress = Arr::get($request->getServerParams(), 'REMOTE_ADDR', '127.0.0.1');

return $handler->handle($request->withAttribute('ipAddress', $ipAddress));
}
}