go first commit
This commit is contained in:
44
go_cloud/internal/audit/audit.go
Normal file
44
go_cloud/internal/audit/audit.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"go.b0esche.cloud/backend/internal/database"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
db *database.DB
|
||||
}
|
||||
|
||||
func NewLogger(db *database.DB) *Logger {
|
||||
return &Logger{db: db}
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
UserID *uuid.UUID
|
||||
OrgID *uuid.UUID
|
||||
Action string
|
||||
Resource *string
|
||||
Success bool
|
||||
Metadata map[string]interface{}
|
||||
}
|
||||
|
||||
func (l *Logger) Log(ctx context.Context, entry Entry) {
|
||||
metadataJSON, err := json.Marshal(entry.Metadata)
|
||||
if err != nil {
|
||||
log.Printf("Failed to marshal audit metadata: %v", err)
|
||||
metadataJSON = []byte("{}")
|
||||
}
|
||||
|
||||
_, err = l.db.ExecContext(ctx, `
|
||||
INSERT INTO audit_logs (user_id, org_id, action, resource, success, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
`, entry.UserID, entry.OrgID, entry.Action, entry.Resource, entry.Success, metadataJSON)
|
||||
if err != nil {
|
||||
log.Printf("Failed to log audit entry: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user