From 9cac6fad7309fcaceff82e1a6f556e969c009276 Mon Sep 17 00:00:00 2001 From: Zero Date: Tue, 9 Nov 2021 12:05:02 +0800 Subject: [PATCH 01/15] Add IP address search field mock --- resources/views/settings/audit.blade.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/resources/views/settings/audit.blade.php b/resources/views/settings/audit.blade.php index 84f180f3b..ebebff1da 100644 --- a/resources/views/settings/audit.blade.php +++ b/resources/views/settings/audit.blade.php @@ -47,6 +47,13 @@ @include('form.user-select', ['user' => $listDetails['user'] ? \BookStack\Auth\User::query()->find($listDetails['user']) : null, 'name' => 'user', 'compact' => true]) + + +
+ + @include('form.text', ['name' => 'ip']) +
From 0e25298db9f82555564db7f1703757a24024938e Mon Sep 17 00:00:00 2001 From: Zero Date: Sun, 14 Nov 2021 12:48:33 +0800 Subject: [PATCH 02/15] Fix label and input box error --- resources/views/settings/audit.blade.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/resources/views/settings/audit.blade.php b/resources/views/settings/audit.blade.php index ebebff1da..e982e6319 100644 --- a/resources/views/settings/audit.blade.php +++ b/resources/views/settings/audit.blade.php @@ -49,9 +49,8 @@ -
- +
+ @include('form.text', ['name' => 'ip'])
From 8dee3d3a837f1474c9f2045b02a2270dcc658883 Mon Sep 17 00:00:00 2001 From: Zero Date: Sun, 14 Nov 2021 12:48:50 +0800 Subject: [PATCH 03/15] Add label translation --- resources/lang/en/settings.php | 1 + resources/views/settings/audit.blade.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/resources/lang/en/settings.php b/resources/lang/en/settings.php index 688b0aad8..385e661ab 100755 --- a/resources/lang/en/settings.php +++ b/resources/lang/en/settings.php @@ -123,6 +123,7 @@ return [ 'audit_table_date' => 'Activity Date', 'audit_date_from' => 'Date Range From', 'audit_date_to' => 'Date Range To', + 'ip_address' => 'IP', // Role Settings 'roles' => 'Roles', diff --git a/resources/views/settings/audit.blade.php b/resources/views/settings/audit.blade.php index e982e6319..c50c8e9e0 100644 --- a/resources/views/settings/audit.blade.php +++ b/resources/views/settings/audit.blade.php @@ -49,7 +49,7 @@
-
+
@include('form.text', ['name' => 'ip'])
From a12e346439553b956d55e973e3a75184f7859031 Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 26 Nov 2021 13:08:04 +0800 Subject: [PATCH 04/15] Add filter of user IP --- app/Http/Controllers/AuditLogController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Http/Controllers/AuditLogController.php b/app/Http/Controllers/AuditLogController.php index 11efbfc23..ab419cc45 100644 --- a/app/Http/Controllers/AuditLogController.php +++ b/app/Http/Controllers/AuditLogController.php @@ -20,6 +20,7 @@ class AuditLogController extends Controller 'date_from' => $request->get('date_from', ''), 'date_to' => $request->get('date_to', ''), 'user' => $request->get('user', ''), + 'ip' => $request->get('ip', ''), ]; $query = Activity::query() @@ -44,6 +45,9 @@ class AuditLogController extends Controller if ($listDetails['date_to']) { $query->where('created_at', '<=', $listDetails['date_to']); } + if ($listDetails['ip']) { + $query->where('ip', '=', $listDetails['ip']); + } $activities = $query->paginate(100); $activities->appends($listDetails); From 882f1959278ca1f62e0b17b4222d36230027cdcf Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 26 Nov 2021 13:17:49 +0800 Subject: [PATCH 05/15] Add margin right for IP input box --- resources/views/settings/audit.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/settings/audit.blade.php b/resources/views/settings/audit.blade.php index c50c8e9e0..05a41e5af 100644 --- a/resources/views/settings/audit.blade.php +++ b/resources/views/settings/audit.blade.php @@ -41,7 +41,7 @@
@endforeach -
From 4e63554cc61fee91d7f3a3e3d26f8c207c86c47b Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 26 Nov 2021 13:58:00 +0800 Subject: [PATCH 06/15] Add an hidden submit This hidden submit makes auto submit while user press Enter on IP input field. --- resources/views/settings/audit.blade.php | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/views/settings/audit.blade.php b/resources/views/settings/audit.blade.php index 05a41e5af..0130c3119 100644 --- a/resources/views/settings/audit.blade.php +++ b/resources/views/settings/audit.blade.php @@ -52,6 +52,7 @@
@include('form.text', ['name' => 'ip']) +
From e686b2cf3ce5fcc2a822746a0573623ecbcb91ec Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 26 Nov 2021 14:38:06 +0800 Subject: [PATCH 07/15] Show current search IP --- resources/views/settings/audit.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/settings/audit.blade.php b/resources/views/settings/audit.blade.php index 0130c3119..b9760fc01 100644 --- a/resources/views/settings/audit.blade.php +++ b/resources/views/settings/audit.blade.php @@ -51,7 +51,7 @@
- @include('form.text', ['name' => 'ip']) + @include('form.text', ['name' => 'ip', 'model' => $listDetails['ip'] ? \BookStack\Actions\Activity::query()->where(['ip' => $listDetails['ip']])->first() : null])
From a93254430c3ab643175d9caec20d60bb5a2e3bd2 Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 26 Nov 2021 15:10:39 +0800 Subject: [PATCH 08/15] Add index for user IP address --- ...021_11_26_070438_add_index_for_user_ip.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 database/migrations/2021_11_26_070438_add_index_for_user_ip.php diff --git a/database/migrations/2021_11_26_070438_add_index_for_user_ip.php b/database/migrations/2021_11_26_070438_add_index_for_user_ip.php new file mode 100644 index 000000000..c4681a9a3 --- /dev/null +++ b/database/migrations/2021_11_26_070438_add_index_for_user_ip.php @@ -0,0 +1,32 @@ +index('ip', 'ip_address_index'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('activities', function (Blueprint $table) { + $table->dropIndex('ip_address_index'); + }); + } +} From 2fd7b1f0d5e543f8ccfca0d8014f9574a5847494 Mon Sep 17 00:00:00 2001 From: Zero Date: Mon, 29 Nov 2021 15:50:55 +0800 Subject: [PATCH 09/15] Update index name to 'activities_ip_index' --- .../migrations/2021_11_26_070438_add_index_for_user_ip.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/migrations/2021_11_26_070438_add_index_for_user_ip.php b/database/migrations/2021_11_26_070438_add_index_for_user_ip.php index c4681a9a3..eebab7958 100644 --- a/database/migrations/2021_11_26_070438_add_index_for_user_ip.php +++ b/database/migrations/2021_11_26_070438_add_index_for_user_ip.php @@ -14,7 +14,7 @@ class AddIndexForUserIp extends Migration public function up() { Schema::table('activities', function (Blueprint $table) { - $table->index('ip', 'ip_address_index'); + $table->index('ip', 'activities_ip_index'); }); } @@ -26,7 +26,7 @@ class AddIndexForUserIp extends Migration public function down() { Schema::table('activities', function (Blueprint $table) { - $table->dropIndex('ip_address_index'); + $table->dropIndex('activities_ip_index'); }); } } From 72d19968dd777a90c7f2318797a464dac312c6ee Mon Sep 17 00:00:00 2001 From: Zero Date: Tue, 7 Dec 2021 17:04:11 +0800 Subject: [PATCH 10/15] Search IP by partial-equal --- app/Http/Controllers/AuditLogController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/AuditLogController.php b/app/Http/Controllers/AuditLogController.php index ab419cc45..7cab6540e 100644 --- a/app/Http/Controllers/AuditLogController.php +++ b/app/Http/Controllers/AuditLogController.php @@ -46,7 +46,7 @@ class AuditLogController extends Controller $query->where('created_at', '<=', $listDetails['date_to']); } if ($listDetails['ip']) { - $query->where('ip', '=', $listDetails['ip']); + $query->where('ip', 'like', $listDetails['ip'] . "%"); } $activities = $query->paginate(100); From 93e80e5d4ec935d939f4ac0f79ee0f7697019085 Mon Sep 17 00:00:00 2001 From: Zero Date: Tue, 7 Dec 2021 17:21:50 +0800 Subject: [PATCH 11/15] Delete duplicated translation --- resources/lang/en/settings.php | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/lang/en/settings.php b/resources/lang/en/settings.php index 385e661ab..688b0aad8 100755 --- a/resources/lang/en/settings.php +++ b/resources/lang/en/settings.php @@ -123,7 +123,6 @@ return [ 'audit_table_date' => 'Activity Date', 'audit_date_from' => 'Date Range From', 'audit_date_to' => 'Date Range To', - 'ip_address' => 'IP', // Role Settings 'roles' => 'Roles', From ee1106630ea0108a4377b5b46383af022661f2e0 Mon Sep 17 00:00:00 2001 From: Zero Date: Tue, 7 Dec 2021 17:22:49 +0800 Subject: [PATCH 12/15] Update translation setting in blade --- resources/views/settings/audit.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/settings/audit.blade.php b/resources/views/settings/audit.blade.php index b9760fc01..2a4addb68 100644 --- a/resources/views/settings/audit.blade.php +++ b/resources/views/settings/audit.blade.php @@ -50,7 +50,7 @@
- + @include('form.text', ['name' => 'ip', 'model' => $listDetails['ip'] ? \BookStack\Actions\Activity::query()->where(['ip' => $listDetails['ip']])->first() : null])
From 2342f0c1c722c707735c59d2013a24e9de066ceb Mon Sep 17 00:00:00 2001 From: Zero Date: Tue, 7 Dec 2021 17:29:41 +0800 Subject: [PATCH 13/15] Fix UI error of IP searching input box --- resources/views/settings/audit.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/settings/audit.blade.php b/resources/views/settings/audit.blade.php index 2a4addb68..d7c31b0dd 100644 --- a/resources/views/settings/audit.blade.php +++ b/resources/views/settings/audit.blade.php @@ -51,7 +51,7 @@
- @include('form.text', ['name' => 'ip', 'model' => $listDetails['ip'] ? \BookStack\Actions\Activity::query()->where(['ip' => $listDetails['ip']])->first() : null]) + @include('form.text', ['name' => 'ip', 'model' => (object) $listDetails])
From e6e92618b1a8558bad10428a1cb274e2977294ca Mon Sep 17 00:00:00 2001 From: Zero Date: Tue, 7 Dec 2021 17:42:10 +0800 Subject: [PATCH 14/15] Fix PHP CS --- app/Http/Controllers/AuditLogController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/AuditLogController.php b/app/Http/Controllers/AuditLogController.php index 7cab6540e..ec3f36975 100644 --- a/app/Http/Controllers/AuditLogController.php +++ b/app/Http/Controllers/AuditLogController.php @@ -46,7 +46,7 @@ class AuditLogController extends Controller $query->where('created_at', '<=', $listDetails['date_to']); } if ($listDetails['ip']) { - $query->where('ip', 'like', $listDetails['ip'] . "%"); + $query->where('ip', 'like', $listDetails['ip'] . '%'); } $activities = $query->paginate(100); From 7d136660398447aefb87eaeae9f4a7b86f27b1ea Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 10 Dec 2021 15:03:17 +0800 Subject: [PATCH 15/15] Add unit test for ip addess searching --- tests/AuditLogTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/AuditLogTest.php b/tests/AuditLogTest.php index f909cd79a..26eb2b9f9 100644 --- a/tests/AuditLogTest.php +++ b/tests/AuditLogTest.php @@ -166,6 +166,24 @@ class AuditLogTest extends TestCase $resp->assertSee('192.123.45.1'); } + public function test_ip_address_is_searchable() + { + config()->set('app.proxies', '*'); + $editor = $this->getEditor(); + /** @var Page $page */ + $page = Page::query()->first(); + + $this->actingAs($editor)->put($page->getUrl(), [ + 'name' => 'Updated page', + 'html' => '

Updated content

', + ], [ + 'X-Forwarded-For' => '192.123.45.1', + ])->assertRedirect($page->refresh()->getUrl()); + + $resp = $this->asAdmin()->get('/settings/audit?&ip=192.123'); + $resp->assertSee('192.123.45.1'); + } + public function test_ip_address_not_logged_in_demo_mode() { config()->set('app.proxies', '*');