#bugfix: sync operation log user labels
This commit is contained in:
@@ -49,11 +49,13 @@ class IpUserMappingController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$mapping = IpUserMapping::query()->create($data);
|
$mapping = IpUserMapping::query()->create($data);
|
||||||
|
$syncedCount = $this->syncMissingOperationLogUserLabels($mapping->ip_address, $mapping->user_name);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'data' => [
|
'data' => [
|
||||||
'mapping' => $mapping,
|
'mapping' => $mapping,
|
||||||
|
'synced_operation_logs_count' => $syncedCount,
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -72,11 +74,14 @@ class IpUserMappingController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$mapping->update($data);
|
$mapping->update($data);
|
||||||
|
$mapping->refresh();
|
||||||
|
$syncedCount = $this->syncMissingOperationLogUserLabels($mapping->ip_address, $mapping->user_name);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'data' => [
|
'data' => [
|
||||||
'mapping' => $mapping->refresh(),
|
'mapping' => $mapping,
|
||||||
|
'synced_operation_logs_count' => $syncedCount,
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -89,4 +94,15 @@ class IpUserMappingController extends Controller
|
|||||||
'success' => true,
|
'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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Models\OperationLog;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class IpUserMappingTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_store_syncs_missing_operation_log_user_labels(): void
|
||||||
|
{
|
||||||
|
config(['toolbox.admin_ips' => ['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<string, mixed> $overrides
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user