Kantoor.Audit (Kantoor v1.0.0)

The admin audit log: who did what to which record, and when.

record/5 is called for every admin mutation (by Kantoor.Admin). Metadata is a small JSON map describing the change (names, counts, new values). It must never carry secrets: keys that look secret (secret, token, password, api_key, hash, credential, authorization) are dropped and only JSON scalars, lists and maps are kept.

Reading is admin only and paginated.

Summary

Functions

One page of events, newest first, with the actor preloaded. Options: :page (1-based) and :per_page. Admin only.

Appends an event for the actor: a scope (an admin), {:api_key, key} (the provisioning API; the key's name, prefix and operator go into the metadata) or :system (tooling with no user, e.g. the task that makes the first API key).

Drops secret-looking keys and non-JSON values from metadata.

Functions

list(scope, opts \\ [])

@spec list(Kantoor.Accounts.Scope.t() | nil, keyword()) ::
  {:ok,
   %{
     events: [Kantoor.Audit.Event.t()],
     page: pos_integer(),
     total_pages: pos_integer(),
     total: non_neg_integer()
   }}
  | {:error, :unauthorized}

One page of events, newest first, with the actor preloaded. Options: :page (1-based) and :per_page. Admin only.

record(actor, action, target_type, target_id, metadata \\ %{})

@spec record(
  Kantoor.Accounts.Scope.t() | {:api_key, Kantoor.ApiKeys.ApiKey.t()} | :system,
  String.t(),
  String.t() | nil,
  term(),
  map()
) :: {:ok, Kantoor.Audit.Event.t()} | {:error, Ecto.Changeset.t()}

Appends an event for the actor: a scope (an admin), {:api_key, key} (the provisioning API; the key's name, prefix and operator go into the metadata) or :system (tooling with no user, e.g. the task that makes the first API key).

sanitize(map)

@spec sanitize(term()) :: term()

Drops secret-looking keys and non-JSON values from metadata.