稽核日誌
Doggy 提供全鏈路的系統操作稽核能力,透過 AuditService 記錄所有關鍵操作,儲存在 App\Entity\System\AuditLog。
概述
稽核日誌系統由以下部分組成:
AuditLog實體(src/Entity/System/AuditLog.php):日誌持久化AuditService(src/Service/System/AuditService.php):日誌記錄服務- 稽核日誌入口在
App\Controller\Admin\SecurityConfigController
記錄內容
每筆稽核日誌包含:
action:操作類型(login_success、login_failure、create、update、delete)category:分類(security、business、system)details:操作詳情(JSON 格式)performer:執行操作的使用者
使用方式
php
use App\Service\System\AuditService;
class SomeService
{
public function __construct(private AuditService $auditService) {}
public function doSomething(): void
{
// 登录成功
$this->auditService->log('login_success', 'security', [], $user);
// 登录失败
$this->auditService->log('login_failure', 'security', [
'username' => $email,
'error' => $exception->getMessage(),
]);
// 数据操作
$this->auditService->log('update', 'business', [
'entity' => Employee::class,
'id' => $employee->getId(),
'changes' => $changes,
]);
}
}整合
稽核日誌已自動整合至主要安全流程中:
登入認證
App\Security\AppCustomAuthenticator 自動記錄:
php
// 登录成功时
$this->auditService->log('login_success', 'security', [], $user);
// 登录失败时
$this->auditService->log('login_failure', 'security', [
'username' => $email,
'error' => $exception->getMessage(),
]);安全需求
- 稽核日誌僅稽核員可檢視(
ROLE_AUDITOR,透過ThreeOfficersVoter的AUDIT_VIEW權限) - 日誌儲存至資料庫,支援追溯與匯出
- 支援依時間範圍、使用者、操作類型搜尋