You've already forked Atomcms-edit
94 lines
2.9 KiB
PHP
Executable File
94 lines
2.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\StaffActivity;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class LogStaffActivity
|
|
{
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
return $next($request);
|
|
}
|
|
|
|
public function terminate(Request $request, Response $response): void
|
|
{
|
|
if (auth()->check() && auth()->user()->rank >= (int) setting('min_staff_rank', 3)) {
|
|
$this->logRequest($request);
|
|
}
|
|
}
|
|
|
|
private function logRequest(Request $request): void
|
|
{
|
|
$user = auth()->user();
|
|
$path = $request->path();
|
|
|
|
if (str_contains($path, 'housekeeping') || str_starts_with($path, 'hk')) {
|
|
$action = $this->determineAction($request);
|
|
$description = $this->generateDescription($request);
|
|
|
|
if ($action && $description) {
|
|
StaffActivity::log(
|
|
$user->id,
|
|
$action,
|
|
$description,
|
|
$request->route()?->getName(),
|
|
null,
|
|
[
|
|
'method' => $request->method(),
|
|
'path' => $path,
|
|
'route' => $request->route()?->getName(),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function determineAction(Request $request): ?string
|
|
{
|
|
$method = $request->method();
|
|
$path = $request->path();
|
|
|
|
if ($method === 'POST') {
|
|
if (str_contains($path, 'ban')) {
|
|
return 'user_ban';
|
|
} elseif (str_contains($path, 'delete')) {
|
|
return 'content_delete';
|
|
} elseif (str_contains($path, 'create') || str_contains($path, 'store')) {
|
|
return 'content_create';
|
|
} elseif (str_contains($path, 'edit') || str_contains($path, 'update')) {
|
|
return 'content_edit';
|
|
} elseif (str_contains($path, 'rank')) {
|
|
return 'rank_change';
|
|
} elseif (str_contains($path, 'settings')) {
|
|
return 'settings_update';
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function generateDescription(Request $request): ?string
|
|
{
|
|
$action = $this->determineAction($request);
|
|
|
|
if (! $action) {
|
|
return null;
|
|
}
|
|
|
|
return match ($action) {
|
|
'user_ban' => 'Banned user: ' . ($request->input('user_id') ?? $request->input('username') ?? 'unknown'),
|
|
'user_unban' => 'Unbanned user',
|
|
'content_delete' => 'Deleted content: ' . $request->path(),
|
|
'content_create' => 'Created new content',
|
|
'content_edit' => 'Updated content: ' . $request->path(),
|
|
'rank_change' => 'Changed user rank',
|
|
'settings_update' => 'Updated settings',
|
|
default => 'Performed action: ' . $action,
|
|
};
|
|
}
|
|
}
|