WIP IP Logging

This commit is contained in:
Jan Dorsman 2015-10-24 19:14:15 +02:00
parent 5e2f659f54
commit 49fddbd450
5 changed files with 47 additions and 4 deletions

View File

@ -0,0 +1,30 @@
<?php
namespace Flarum\Migrations\Core;
use Illuminate\Database\Schema\Blueprint;
use Flarum\Database\AbstractMigration;
class AddIpToPosts extends AbstractMigration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$this->schema->table('posts', function (Blueprint $table) {
$table->string('ip_address', 45)->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
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)
{
$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;