Merge pull request #615 from oldskool/ip-logging

Minor changes:
- Rename/restyle migration, fix namespace
- Make IP address optional on PostReply command
This commit is contained in:
Toby Zerner 2015-10-31 10:04:06 +10:30
commit 2173d2d4a2
5 changed files with 48 additions and 4 deletions

View File

@ -0,0 +1,31 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint;
class AddIpAddressToPosts extends AbstractMigration
{
public function up()
{
$this->schema->table('posts', function (Blueprint $table) {
$table->string('ip_address', 45)->nullable();
});
}
public function down()
{
$this->schema->table('posts', function (Blueprint $table) {
$table->dropColumn(['ip_address']);
});
}
}

View File

@ -54,9 +54,10 @@ class CreatePostController extends AbstractCreateController
$actor = $request->getAttribute('actor');
$data = array_get($request->getParsedBody(), 'data');
$discussionId = array_get($data, 'relationships.discussion.data.id');
$ipAddress = array_get($request->getServerParams(), 'REMOTE_ADDR', '127.0.0.1');
$post = $this->bus->dispatch(
new PostReply($discussionId, $actor, $data)
new PostReply($discussionId, $actor, $data, $ipAddress)
);
// After replying, we assume that the user has seen all of the posts

View File

@ -35,15 +35,24 @@ class PostReply
*/
public $data;
/**
* The IP address of the actor.
*
* @var string
*/
public $ipAddress;
/**
* @param int $discussionId The ID of the discussion to post the reply to.
* @param User $actor The user who is performing the action.
* @param array $data The attributes to assign to the new post.
* @param string $ipAddress The IP address of the actor.
*/
public function __construct($discussionId, User $actor, array $data)
public function __construct($discussionId, User $actor, array $data, $ipAddress = null)
{
$this->discussionId = $discussionId;
$this->actor = $actor;
$this->data = $data;
$this->ipAddress = $ipAddress;
}
}

View File

@ -81,7 +81,8 @@ class PostReplyHandler
$post = CommentPost::reply(
$discussion->id,
array_get($command->data, 'attributes.content'),
$actor->id
$actor->id,
$command->ipAddress
);
$this->events->fire(

View File

@ -45,9 +45,10 @@ class CommentPost extends Post
* @param int $discussionId
* @param string $content
* @param int $userId
* @param string $ipAddress
* @return static
*/
public static function reply($discussionId, $content, $userId)
public static function reply($discussionId, $content, $userId, $ipAddress)
{
$post = new static;
@ -55,6 +56,7 @@ class CommentPost extends Post
$post->discussion_id = $discussionId;
$post->user_id = $userId;
$post->type = static::$type;
$post->ip_address = $ipAddress;
// Set content last, as the parsing may rely on other post attributes.
$post->content = $content;