forked from baron/baron-sso
감사 로그 조회 기능 확장 (사용자 및 이벤트 필터링)
This commit is contained in:
@@ -24,6 +24,7 @@ type AuditLog struct {
|
|||||||
type AuditRepository interface {
|
type AuditRepository interface {
|
||||||
Create(log *AuditLog) error
|
Create(log *AuditLog) error
|
||||||
FindPage(ctx context.Context, limit int, cursor *AuditCursor) ([]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
|
Ping(ctx context.Context) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,11 @@ func (m *MockAuditRepository) FindPage(ctx context.Context, limit int, cursor *d
|
|||||||
return args.Get(0).([]domain.AuditLog), args.Error(1)
|
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 {
|
func (m *MockAuditRepository) Ping(ctx context.Context) error {
|
||||||
args := m.Called(ctx)
|
args := m.Called(ctx)
|
||||||
return args.Error(0)
|
return args.Error(0)
|
||||||
|
|||||||
@@ -151,6 +151,44 @@ func (r *ClickHouseRepository) FindPage(ctx context.Context, limit int, cursor *
|
|||||||
return logs, nil
|
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 {
|
func (r *ClickHouseRepository) Ping(ctx context.Context) error {
|
||||||
if r.conn == nil {
|
if r.conn == nil {
|
||||||
return fmt.Errorf("clickhouse connection is nil")
|
return fmt.Errorf("clickhouse connection is nil")
|
||||||
|
|||||||
Reference in New Issue
Block a user