Initial commit
This commit is contained in:
110
app/Http/Controllers/AdminAuditLogController.php
Normal file
110
app/Http/Controllers/AdminAuditLogController.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\AuditLog;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AdminAuditLogController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = AuditLog::query()->with('user');
|
||||
|
||||
$search = $request->string('search')->toString();
|
||||
$action = $request->string('action')->toString();
|
||||
$userId = $request->integer('user_id');
|
||||
$start = $request->date('start_date');
|
||||
$end = $request->date('end_date');
|
||||
|
||||
if ($search) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('action', 'like', "%{$search}%")
|
||||
->orWhere('metadata', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
if ($action) {
|
||||
$query->where('action', $action);
|
||||
}
|
||||
|
||||
if ($userId) {
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
if ($start) {
|
||||
$query->whereDate('created_at', '>=', $start);
|
||||
}
|
||||
|
||||
if ($end) {
|
||||
$query->whereDate('created_at', '<=', $end);
|
||||
}
|
||||
|
||||
$logs = $query->orderByDesc('created_at')->paginate(25)->withQueryString();
|
||||
|
||||
$actions = AuditLog::select('action')->distinct()->orderBy('action')->pluck('action');
|
||||
$users = AuditLog::with('user')->whereNotNull('user_id')->select('user_id')->distinct()->get()->map(function ($log) {
|
||||
return $log->user;
|
||||
})->filter();
|
||||
|
||||
return view('admin.audit.index', [
|
||||
'logs' => $logs,
|
||||
'search' => $search,
|
||||
'actionFilter' => $action,
|
||||
'userFilter' => $userId,
|
||||
'startDate' => $start,
|
||||
'endDate' => $end,
|
||||
'actions' => $actions,
|
||||
'users' => $users,
|
||||
]);
|
||||
}
|
||||
|
||||
public function export(Request $request)
|
||||
{
|
||||
$query = AuditLog::query()->with('user');
|
||||
|
||||
if ($search = $request->string('search')->toString()) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('action', 'like', "%{$search}%")
|
||||
->orWhere('metadata', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
if ($action = $request->string('action')->toString()) {
|
||||
$query->where('action', $action);
|
||||
}
|
||||
|
||||
if ($userId = $request->integer('user_id')) {
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
if ($start = $request->date('start_date')) {
|
||||
$query->whereDate('created_at', '>=', $start);
|
||||
}
|
||||
|
||||
if ($end = $request->date('end_date')) {
|
||||
$query->whereDate('created_at', '<=', $end);
|
||||
}
|
||||
|
||||
return response()->stream(function () use ($query) {
|
||||
$handle = fopen('php://output', 'w');
|
||||
fputcsv($handle, ['Timestamp', 'User', 'Action', 'Metadata']);
|
||||
|
||||
$query->orderByDesc('created_at')->chunk(500, function ($logs) use ($handle) {
|
||||
foreach ($logs as $log) {
|
||||
fputcsv($handle, [
|
||||
$log->created_at,
|
||||
$log->user?->email ?? 'System',
|
||||
$log->action,
|
||||
json_encode($log->metadata, JSON_UNESCAPED_UNICODE),
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
fclose($handle);
|
||||
}, 200, [
|
||||
'Content-Type' => 'text/csv',
|
||||
'Content-Disposition' => 'attachment; filename="audit-logs-'.now()->format('Ymd_His').'.csv"',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user