Files
jongryangje/app/Models/Traits/Auditable.php
taekyoungc 8d49247803 feat: 사용자 작업(감사) 로그 자동 기록 + 관리자 조회 화면
- Auditable 트레잇: 모델 insert/update/delete 시 activity_log 자동 기록
  (조회 제외, before/after JSON, 민감필드 마스킹, 사용자/IP)
- 도메인 모델 28개에 적용 (로그성 테이블 제외)
- 관리자 전용 조회: Admin\ActivityLog (기간/작업/대상/사용자 필터 + 상세 변경내역)
- 라우트 admin/access/activity-logs(+상세), 메뉴 '활동 로그' 추가
- 테넌트 분리: 슈퍼=전체, 지자체관리자=소속 지자체만

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 17:13:05 +09:00

149 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models\Traits;
/**
* 모델 CRUD(생성/수정/삭제) 자동 감사 로그 트레잇.
*
* 모델에 `use \App\Models\Traits\Auditable;` 만 추가하면
* insert/update/delete 시 activity_log 에 "누가·무엇을·언제" 자동 기록한다.
* (조회(select)는 기록하지 않음)
*
* - 변경 전(before)/후(after) 데이터를 JSON 으로 보관
* - 비밀번호·OTP 비밀키·이메일·전화 등 민감 필드는 마스킹
* - 기록 실패는 audit_log() 내부에서 흡수되어 본 로직에 영향 없음
*/
trait Auditable
{
/** 변경/삭제 전 원본 보관 (PK => row) */
protected array $auditBeforeRows = [];
/**
* 모델 이벤트(콜백) 등록.
* BaseModel 이 이미 $afterInsert 등 속성을 선언하므로(속성 충돌 방지),
* 트레잇에서는 속성 대신 initialize()에서 런타임에 콜백을 덧붙인다.
*/
protected function initialize()
{
$this->allowCallbacks = true;
$this->afterInsert[] = 'auditAfterInsert';
$this->beforeUpdate[] = 'auditBeforeUpdate';
$this->afterUpdate[] = 'auditAfterUpdate';
$this->beforeDelete[] = 'auditBeforeDelete';
$this->afterDelete[] = 'auditAfterDelete';
}
/** 마스킹 대상 필드명 */
protected array $auditMaskFields = ['mb_passwd', 'mb_totp_secret', 'mb_session_token', 'mb_email', 'mb_phone'];
private function auditMask(?array $row): ?array
{
if ($row === null) {
return null;
}
foreach ($this->auditMaskFields as $f) {
if (array_key_exists($f, $row) && (string) $row[$f] !== '') {
$row[$f] = '***';
}
}
return $row;
}
/** find 콜백 재귀를 피하려고 쿼리빌더로 원본 행을 직접 조회 */
private function auditFetchRaw(int|string $id): ?array
{
try {
$row = $this->db->table($this->table)
->where($this->primaryKey, $id)
->get()
->getRowArray();
return $row ?: null;
} catch (\Throwable $e) {
return null;
}
}
public function auditAfterInsert(array $eventData): array
{
helper('audit');
$after = is_array($eventData['data'] ?? null) ? $eventData['data'] : null;
audit_log('create', $this->table, (int) ($eventData['id'] ?? 0), null, $this->auditMask($after));
return $eventData;
}
public function auditBeforeUpdate(array $eventData): array
{
$this->auditBeforeRows = [];
$ids = $eventData['id'] ?? null;
if ($ids !== null) {
foreach ((array) $ids as $one) {
$row = $this->auditFetchRaw($one);
if ($row !== null) {
$this->auditBeforeRows[(string) $one] = $row;
}
}
}
return $eventData;
}
public function auditAfterUpdate(array $eventData): array
{
helper('audit');
$ids = $eventData['id'] ?? null;
$changes = is_array($eventData['data'] ?? null) ? $eventData['data'] : [];
if ($ids !== null) {
foreach ((array) $ids as $one) {
$before = $this->auditBeforeRows[(string) $one] ?? null;
$after = $before !== null ? array_merge($before, $changes) : $changes;
audit_log('update', $this->table, (int) $one, $this->auditMask($before), $this->auditMask($after));
}
} else {
// 조건(where) 기반 일괄 수정: 대상 PK 미상 → 변경값만 기록
audit_log('update', $this->table, 0, null, $this->auditMask($changes));
}
$this->auditBeforeRows = [];
return $eventData;
}
public function auditBeforeDelete(array $eventData): array
{
$this->auditBeforeRows = [];
$ids = $eventData['id'] ?? null;
if ($ids !== null) {
foreach ((array) $ids as $one) {
$row = $this->auditFetchRaw($one);
if ($row !== null) {
$this->auditBeforeRows[(string) $one] = $row;
}
}
}
return $eventData;
}
public function auditAfterDelete(array $eventData): array
{
helper('audit');
$ids = $eventData['id'] ?? null;
if ($ids !== null) {
foreach ((array) $ids as $one) {
$before = $this->auditBeforeRows[(string) $one] ?? null;
audit_log('delete', $this->table, (int) $one, $this->auditMask($before), null);
}
} else {
audit_log('delete', $this->table, 0, null, null);
}
$this->auditBeforeRows = [];
return $eventData;
}
}