Files
goclaw/internal/http/audit.go
T
viettranx 9115169c03 feat: expand audit logging via pub/sub event pattern
Replace direct ActivityStore injection with event-driven audit system.
Handlers emit audit events via msgBus.Broadcast(), a single subscriber
with buffered channel persists to activity_logs table.

Coverage expanded from 3 agent CRUD actions to ~65 audit points across
all HTTP handlers and WebSocket RPC methods including agents, providers,
skills, MCP servers, cron, sessions, teams, pairing, and more.
2026-03-12 18:34:56 +07:00

35 lines
796 B
Go

package http
import (
"net/http"
"github.com/nextlevelbuilder/goclaw/internal/bus"
"github.com/nextlevelbuilder/goclaw/internal/store"
"github.com/nextlevelbuilder/goclaw/pkg/protocol"
)
// emitAudit broadcasts an audit event via msgBus for async persistence.
func emitAudit(msgBus *bus.MessageBus, r *http.Request, action, entityType, entityID string) {
if msgBus == nil {
return
}
actorID := store.UserIDFromContext(r.Context())
if actorID == "" {
actorID = extractUserID(r)
}
if actorID == "" {
actorID = "system"
}
msgBus.Broadcast(bus.Event{
Name: protocol.EventAuditLog,
Payload: bus.AuditEventPayload{
ActorType: "user",
ActorID: actorID,
Action: action,
EntityType: entityType,
EntityID: entityID,
IPAddress: r.RemoteAddr,
},
})
}