audit

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package audit writes who changed what into an app's audit_events table (schema.sql), in the transaction of the change, and pages through it for a back office.

The account actions of package backoffice are listed here; an app adds the codes of its own domain.

Index

Constants

View Source
const (
	SignedIn             = "signed_in"
	SignedUp             = "signed_up"
	SignedOut            = "signed_out"
	RoleChanged          = "role_changed"
	UserRestrained       = "user_restrained"
	UserRestored         = "user_restored"
	UserApproved         = "user_approved"
	ImpersonationStarted = "impersonation_started"
	ImpersonationStopped = "impersonation_stopped"
	AnnouncementSet      = "announcement_set"
	AnnouncementCleared  = "announcement_cleared"
	// FeatureSet stays out of AccountActions: a back office lists it only when the app has features.
	FeatureSet = "feature_set"
)

Codes of the account actions. Rows keep them: never rename one, add a new code.

View Source
const Layout = "2006-01-02T15:04:05.000Z"

Layout is the fixed-width UTC layout of audit_events.at.

Variables

AccountActions lists the account codes in the order a back office filter shows them.

Functions

func After

func After(ctx context.Context, db Execer, actorID, action, target string, details map[string]any)

After writes the audit row of a committed change and logs a failure. Never inside a transaction: on a database with a single pooled connection, the write would wait for the transaction forever.

func Since

func Since(ctx context.Context, db Querier) (string, error)

Since returns the time of the first event, "" when there is none.

func Write

func Write(ctx context.Context, db Execer, actorID, action, target string, details map[string]any) error

Write writes one audit row through db; given the transaction of the change, both commit or neither does.

Types

type Event

type Event struct {
	ID      int64           `json:"id"`
	At      string          `json:"at"`
	ActorID string          `json:"actor_id"`
	Action  string          `json:"action"`
	Target  string          `json:"target"`
	Details json.RawMessage `json:"details"`
}

Event is one audit row.

func List

func List(ctx context.Context, db Querier, f Filter) ([]Event, int64, error)

List returns a page of events and the id to pass as Before for the next one, 0 after the last page.

type Execer

type Execer interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

Execer is a *sql.DB or a *sql.Tx.

type Filter

type Filter struct {
	Before  int64
	Action  string
	ActorID string
	Limit   int
}

Filter selects a page of events, newest first: those before the event id Before (0 for the newest), of one action and one actor when set, Limit at most.

type Querier

type Querier interface {
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

Querier is a *sql.DB or a *sql.Tx.

Jump to

Keyboard shortcuts

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