Help us shape the future of Go! Take the 2026 Go Developer Survey

adminaudit

package
v0.36.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package adminaudit records admin actions: who did what to whom, when and from where. Rows are written before the action runs (so an action cannot happen unrecorded) and finished with the outcome afterwards.

Index

Constants

View Source
const Retention = 2 * 365 * 24 * time.Hour

Retention is how long rows are kept.

Variables

This section is empty.

Functions

func Clean

func Clean(in map[string]any) map[string]any

Clean keeps only what may be stored: booleans, numbers, short lowercase enum tokens, and lists of such tokens (field names). Everything else, free text above all, is dropped. At most 20 keys.

Types

type Entry

type Entry struct {
	Actor      uuid.UUID
	Action     string // e.g. "user.set_role", "flag.kill_switch"
	TargetType string // "user", "flag", "settings", "signup_request", ...
	TargetID   string
	TargetUser uuid.UUID // uuid.Nil when the action is not about a user
	Details    map[string]any
	IP         string
}

Entry is one admin action about to happen.

type Filter

type Filter struct {
	Action     string
	Actor      uuid.UUID
	TargetUser uuid.UUID
	Before     int64 // return rows with id < Before (0 = from the newest)
	Limit      int
}

Filter narrows a listing.

type Row

type Row struct {
	ID          int64          `json:"id"`
	At          time.Time      `json:"at"`
	Actor       uuid.UUID      `json:"actor_user_id"`
	Action      string         `json:"action"`
	TargetType  string         `json:"target_type"`
	TargetID    string         `json:"target_id"`
	TargetUser  *uuid.UUID     `json:"target_user_id,omitempty"`
	Details     map[string]any `json:"details"`
	IP          string         `json:"ip"`
	AccessEmail string         `json:"access_email,omitempty"`
	Outcome     *string        `json:"outcome"` // nil: in flight or unknown
	Status      *int           `json:"status,omitempty"`
	FinishedAt  *time.Time     `json:"finished_at,omitempty"`
}

Row is a stored entry, as the listing returns it.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is the Postgres side.

func New

func New(db *sql.DB) *Store

New wraps the pool.

func (*Store) Begin

func (s *Store) Begin(ctx context.Context, e Entry) (int64, error)

Begin inserts the row for an action about to run and returns its id. An error means the action must not run.

func (*Store) Finish

func (s *Store) Finish(ctx context.Context, id int64, status int) error

Finish records the HTTP status the action ended with.

func (*Store) List

func (s *Store) List(ctx context.Context, f Filter) ([]Row, int64, error)

List returns rows newest first and the cursor for the next page (0 = none).

func (*Store) NoteImpersonationStart

func (s *Store) NoteImpersonationStart(ctx context.Context, admin, target uuid.UUID, ip string, window time.Duration) error

NoteImpersonationStart records that admin began viewing as target, once per pair per window (every impersonated request would otherwise add a row). A successful impersonation.stop ends the session: the next view-as of the same target logs a fresh start, so a stop is never left without its start.

func (*Store) Purge

func (s *Store) Purge(ctx context.Context) (int64, error)

Purge deletes rows older than the retention window.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL