Changeset 0.28.0.4 (#176)

This commit is contained in:
2026-03-12 12:12:17 +00:00
parent f5171d3bd3
commit 52bd36ba48
16 changed files with 520 additions and 292 deletions

View File

@@ -25,25 +25,28 @@ func (s *AuditStore) Log(ctx context.Context, entry *models.AuditEntry) error {
}
func (s *AuditStore) List(ctx context.Context, opts store.AuditListOptions) ([]models.AuditEntry, int, error) {
b := NewSelect("id, actor_id, action, resource_type, resource_id, metadata, ip_address, user_agent, created_at", "audit_log")
b := NewSelect(`al.id, al.actor_id, COALESCE(u.username, '') AS actor_name,
al.action, al.resource_type, al.resource_id, al.metadata,
al.ip_address, al.user_agent, al.created_at`, "audit_log al")
b.Join("LEFT JOIN users u ON al.actor_id = u.id")
if opts.ActorID != "" {
b.Where("actor_id = ?", opts.ActorID)
b.Where("al.actor_id = ?", opts.ActorID)
}
if opts.Action != "" {
b.Where("action = ?", opts.Action)
b.Where("al.action = ?", opts.Action)
}
if opts.ResourceType != "" {
b.Where("resource_type = ?", opts.ResourceType)
b.Where("al.resource_type = ?", opts.ResourceType)
}
if opts.ResourceID != "" {
b.Where("resource_id = ?", opts.ResourceID)
b.Where("al.resource_id = ?", opts.ResourceID)
}
if opts.Since != nil {
b.Where("created_at >= ?", *opts.Since)
b.Where("al.created_at >= ?", *opts.Since)
}
if opts.Until != nil {
b.Where("created_at <= ?", *opts.Until)
b.Where("al.created_at <= ?", *opts.Until)
}
// Count
@@ -53,7 +56,7 @@ func (s *AuditStore) List(ctx context.Context, opts store.AuditListOptions) ([]m
// Results
if opts.Sort == "" {
b.OrderBy("created_at", "DESC")
b.OrderBy("al.created_at", "DESC")
}
b.Paginate(opts.ListOptions)
@@ -67,16 +70,40 @@ func (s *AuditStore) List(ctx context.Context, opts store.AuditListOptions) ([]m
var result []models.AuditEntry
for rows.Next() {
var e models.AuditEntry
var actorID sql.NullString
var actorID, actorName sql.NullString
var metadataJSON []byte
err := rows.Scan(&e.ID, &actorID, &e.Action, &e.ResourceType, &e.ResourceID,
err := rows.Scan(&e.ID, &actorID, &actorName, &e.Action, &e.ResourceType, &e.ResourceID,
&metadataJSON, &e.IPAddress, &e.UserAgent, &e.CreatedAt)
if err != nil {
return nil, 0, err
}
e.ActorID = NullableStringPtr(actorID)
if actorName.Valid && actorName.String != "" {
e.ActorName = &actorName.String
}
json.Unmarshal(metadataJSON, &e.Metadata)
result = append(result, e)
}
return result, total, rows.Err()
}
func (s *AuditStore) ListActions(ctx context.Context) ([]string, error) {
rows, err := DB.QueryContext(ctx,
`SELECT DISTINCT action FROM audit_log ORDER BY action ASC`)
if err != nil {
return nil, err
}
defer rows.Close()
var actions []string
for rows.Next() {
var a string
if rows.Scan(&a) == nil {
actions = append(actions, a)
}
}
if actions == nil {
actions = []string{}
}
return actions, rows.Err()
}