From 1de5fe1ff95a341c6c266f96af1a7e6edb44fbc2 Mon Sep 17 00:00:00 2001 From: tradewind Date: Mon, 1 Jun 2026 14:35:23 +0800 Subject: [PATCH] #bugfix: sync operation log user labels --- .../Admin/IpUserMappingController.php | 18 ++++- tests/Feature/IpUserMappingTest.php | 68 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/IpUserMappingTest.php diff --git a/app/Http/Controllers/Admin/IpUserMappingController.php b/app/Http/Controllers/Admin/IpUserMappingController.php index bf88b76..cff6467 100644 --- a/app/Http/Controllers/Admin/IpUserMappingController.php +++ b/app/Http/Controllers/Admin/IpUserMappingController.php @@ -49,11 +49,13 @@ class IpUserMappingController extends Controller ]); $mapping = IpUserMapping::query()->create($data); + $syncedCount = $this->syncMissingOperationLogUserLabels($mapping->ip_address, $mapping->user_name); return response()->json([ 'success' => true, 'data' => [ 'mapping' => $mapping, + 'synced_operation_logs_count' => $syncedCount, ], ]); } @@ -72,11 +74,14 @@ class IpUserMappingController extends Controller ]); $mapping->update($data); + $mapping->refresh(); + $syncedCount = $this->syncMissingOperationLogUserLabels($mapping->ip_address, $mapping->user_name); return response()->json([ 'success' => true, 'data' => [ - 'mapping' => $mapping->refresh(), + 'mapping' => $mapping, + 'synced_operation_logs_count' => $syncedCount, ], ]); } @@ -89,4 +94,15 @@ class IpUserMappingController extends Controller 'success' => true, ]); } + + private function syncMissingOperationLogUserLabels(string $ipAddress, string $userName): int + { + return DB::table('operation_logs') + ->where('ip_address', $ipAddress) + ->where(function ($query): void { + $query->whereNull('user_label') + ->orWhere('user_label', ''); + }) + ->update(['user_label' => $userName]); + } } diff --git a/tests/Feature/IpUserMappingTest.php b/tests/Feature/IpUserMappingTest.php new file mode 100644 index 0000000..148bdaa --- /dev/null +++ b/tests/Feature/IpUserMappingTest.php @@ -0,0 +1,68 @@ + ['127.0.0.1']]); + + $missingLabelLog = OperationLog::query()->create($this->operationLogData([ + 'ip_address' => '192.168.1.10', + 'user_label' => null, + ])); + $emptyLabelLog = OperationLog::query()->create($this->operationLogData([ + 'ip_address' => '192.168.1.10', + 'user_label' => '', + ])); + $existingLabelLog = OperationLog::query()->create($this->operationLogData([ + 'ip_address' => '192.168.1.10', + 'user_label' => 'lisi', + ])); + $otherIpLog = OperationLog::query()->create($this->operationLogData([ + 'ip_address' => '192.168.1.11', + 'user_label' => null, + ])); + + $response = $this->postJson('/api/admin/ip-user-mappings', [ + 'ip_address' => '192.168.1.10', + 'user_name' => 'zhangsan', + 'remark' => 'dev', + ]); + + $response->assertOk() + ->assertJsonPath('success', true) + ->assertJsonPath('data.synced_operation_logs_count', 2); + + $this->assertSame('zhangsan', $missingLabelLog->refresh()->user_label); + $this->assertSame('zhangsan', $emptyLabelLog->refresh()->user_label); + $this->assertSame('lisi', $existingLabelLog->refresh()->user_label); + $this->assertNull($otherIpLog->refresh()->user_label); + } + + /** + * @param array $overrides + * @return array + */ + private function operationLogData(array $overrides = []): array + { + return array_merge([ + 'ip_address' => '127.0.0.1', + 'user_label' => null, + 'method' => 'POST', + 'path' => '/api/test', + 'route_name' => null, + 'status_code' => 200, + 'duration_ms' => 12, + 'request_payload' => [], + 'user_agent' => 'PHPUnit', + ], $overrides); + } +}