#feature: add ip operation log & sql generator
This commit is contained in:
24
app/Http/Controllers/Admin/AdminMetaController.php
Normal file
24
app/Http/Controllers/Admin/AdminMetaController.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Support\IpAccess;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AdminMetaController extends Controller
|
||||
{
|
||||
public function show(Request $request): JsonResponse
|
||||
{
|
||||
$ipAddress = $request->ip();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'is_admin' => IpAccess::isAdmin($ipAddress),
|
||||
'ip' => $ipAddress,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
92
app/Http/Controllers/Admin/IpUserMappingController.php
Normal file
92
app/Http/Controllers/Admin/IpUserMappingController.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\IpUserMapping;
|
||||
use App\Models\OperationLog;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class IpUserMappingController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$mappings = IpUserMapping::query()
|
||||
->orderBy('ip_address')
|
||||
->get();
|
||||
|
||||
$unmappedIps = OperationLog::query()
|
||||
->select(
|
||||
'operation_logs.ip_address',
|
||||
DB::raw('COUNT(*) as logs_count'),
|
||||
DB::raw('MAX(operation_logs.created_at) as last_seen_at')
|
||||
)
|
||||
->leftJoin('ip_user_mappings', 'operation_logs.ip_address', '=', 'ip_user_mappings.ip_address')
|
||||
->whereNull('ip_user_mappings.id')
|
||||
->where('operation_logs.ip_address', '!=', '')
|
||||
->groupBy('operation_logs.ip_address')
|
||||
->orderByDesc('last_seen_at')
|
||||
->get();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'mappings' => $mappings,
|
||||
'unmapped_ips' => $unmappedIps,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'ip_address' => ['required', 'ip', 'max:64', 'unique:ip_user_mappings,ip_address'],
|
||||
'user_name' => ['required', 'string', 'max:128'],
|
||||
'remark' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$mapping = IpUserMapping::query()->create($data);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'mapping' => $mapping,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, IpUserMapping $mapping): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'ip_address' => [
|
||||
'required',
|
||||
'ip',
|
||||
'max:64',
|
||||
Rule::unique('ip_user_mappings', 'ip_address')->ignore($mapping->id),
|
||||
],
|
||||
'user_name' => ['required', 'string', 'max:128'],
|
||||
'remark' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$mapping->update($data);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'mapping' => $mapping->refresh(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(IpUserMapping $mapping): JsonResponse
|
||||
{
|
||||
$mapping->delete();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
]);
|
||||
}
|
||||
}
|
||||
67
app/Http/Controllers/Admin/OperationLogController.php
Normal file
67
app/Http/Controllers/Admin/OperationLogController.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\OperationLog;
|
||||
use App\Support\IpAccess;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OperationLogController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'page' => ['nullable', 'integer', 'min:1'],
|
||||
'per_page' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
'q' => ['nullable', 'string', 'max:255'],
|
||||
'method' => ['nullable', 'string', 'max:10'],
|
||||
]);
|
||||
|
||||
$query = OperationLog::query()->orderByDesc('id');
|
||||
$isAdmin = IpAccess::isAdmin($request->ip());
|
||||
|
||||
$search = trim((string) ($data['q'] ?? ''));
|
||||
if ($search !== '') {
|
||||
$query->where(function ($builder) use ($search, $isAdmin): void {
|
||||
$builder->where('ip_address', 'like', "%{$search}%")
|
||||
->orWhere('path', 'like', "%{$search}%")
|
||||
->orWhere('route_name', 'like', "%{$search}%");
|
||||
|
||||
if ($isAdmin) {
|
||||
$builder->orWhere('user_label', 'like', "%{$search}%");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$method = strtoupper((string) ($data['method'] ?? ''));
|
||||
if ($method !== '') {
|
||||
$query->where('method', $method);
|
||||
}
|
||||
|
||||
$perPage = (int) ($data['per_page'] ?? 50);
|
||||
$logs = $query->paginate($perPage);
|
||||
$items = collect($logs->items())->map(function (OperationLog $log) use ($isAdmin): array {
|
||||
$payload = $log->toArray();
|
||||
if (!$isAdmin) {
|
||||
$payload['user_label'] = null;
|
||||
}
|
||||
|
||||
return $payload;
|
||||
})->all();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'logs' => $items,
|
||||
'pagination' => [
|
||||
'current_page' => $logs->currentPage(),
|
||||
'last_page' => $logs->lastPage(),
|
||||
'per_page' => $logs->perPage(),
|
||||
'total' => $logs->total(),
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user