1
0
forked from baron/baron-sso

감사 로그 조회 기능 확장 (사용자 및 이벤트 필터링)

This commit is contained in:
2026-02-04 13:43:28 +09:00
parent 5cf784a2c2
commit b4241cb98b
3 changed files with 44 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ type AuditLog struct {
type AuditRepository interface {
Create(log *AuditLog) error
FindPage(ctx context.Context, limit int, cursor *AuditCursor) ([]AuditLog, error)
FindByUserAndEvents(ctx context.Context, userID string, eventTypes []string, limit int) ([]AuditLog, error)
Ping(ctx context.Context) error
}

View File

@@ -29,6 +29,11 @@ func (m *MockAuditRepository) FindPage(ctx context.Context, limit int, cursor *d
return args.Get(0).([]domain.AuditLog), args.Error(1)
}
func (m *MockAuditRepository) FindByUserAndEvents(ctx context.Context, userID string, eventTypes []string, limit int) ([]domain.AuditLog, error) {
args := m.Called(ctx, userID, eventTypes, limit)
return args.Get(0).([]domain.AuditLog), args.Error(1)
}
func (m *MockAuditRepository) Ping(ctx context.Context) error {
args := m.Called(ctx)
return args.Error(0)

View File

@@ -151,6 +151,44 @@ func (r *ClickHouseRepository) FindPage(ctx context.Context, limit int, cursor *
return logs, nil
}
func (r *ClickHouseRepository) FindByUserAndEvents(ctx context.Context, userID string, eventTypes []string, limit int) ([]domain.AuditLog, error) {
if limit <= 0 {
limit = 100
}
query := `
SELECT event_id, timestamp, user_id, event_type, status, ip_address, user_agent, device_id, details
FROM audit_logs
WHERE user_id = ? AND event_type IN (?)
ORDER BY timestamp DESC
LIMIT ?
`
rows, err := r.conn.Query(ctx, query, userID, eventTypes, limit)
if err != nil {
return nil, fmt.Errorf("failed to query audit logs by user/events: %w", err)
}
defer rows.Close()
var logs []domain.AuditLog
for rows.Next() {
var log domain.AuditLog
if err := rows.Scan(
&log.EventID,
&log.Timestamp,
&log.UserID,
&log.EventType,
&log.Status,
&log.IPAddress,
&log.UserAgent,
&log.DeviceID,
&log.Details,
); err != nil {
return nil, fmt.Errorf("failed to scan audit log: %w", err)
}
logs = append(logs, log)
}
return logs, nil
}
func (r *ClickHouseRepository) Ping(ctx context.Context) error {
if r.conn == nil {
return fmt.Errorf("clickhouse connection is nil")