From 49fddbd450ad7169129f0a5f572abdd5f858c1ae Mon Sep 17 00:00:00 2001 From: Jan Dorsman Date: Sat, 24 Oct 2015 19:14:15 +0200 Subject: [PATCH] WIP IP Logging --- .../2015_10_24_194000_add_ip_to_posts.php | 30 +++++++++++++++++++ src/Api/Controller/CreatePostController.php | 3 +- src/Core/Command/PostReply.php | 11 ++++++- src/Core/Command/PostReplyHandler.php | 3 +- src/Core/Post/CommentPost.php | 4 ++- 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 migrations/2015_10_24_194000_add_ip_to_posts.php diff --git a/migrations/2015_10_24_194000_add_ip_to_posts.php b/migrations/2015_10_24_194000_add_ip_to_posts.php new file mode 100644 index 000000000..c75e924f6 --- /dev/null +++ b/migrations/2015_10_24_194000_add_ip_to_posts.php @@ -0,0 +1,30 @@ +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']); + }); + } +} diff --git a/src/Api/Controller/CreatePostController.php b/src/Api/Controller/CreatePostController.php index d32eb8307..f09120549 100644 --- a/src/Api/Controller/CreatePostController.php +++ b/src/Api/Controller/CreatePostController.php @@ -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 diff --git a/src/Core/Command/PostReply.php b/src/Core/Command/PostReply.php index f79bc1321..6f6de076e 100644 --- a/src/Core/Command/PostReply.php +++ b/src/Core/Command/PostReply.php @@ -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; } } diff --git a/src/Core/Command/PostReplyHandler.php b/src/Core/Command/PostReplyHandler.php index 78dda1bd5..937568a9a 100644 --- a/src/Core/Command/PostReplyHandler.php +++ b/src/Core/Command/PostReplyHandler.php @@ -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( diff --git a/src/Core/Post/CommentPost.php b/src/Core/Post/CommentPost.php index c89f38789..bea521d66 100755 --- a/src/Core/Post/CommentPost.php +++ b/src/Core/Post/CommentPost.php @@ -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;