visa.buildfor.life keeps the immigration records of the foreigners B4L looks after. This first phase is the base the deadlines and extension packs build on (docs/PLAN.md, phases 2 to 7). - One Rust binary (axum 0.8, minijinja, sqlx on PostgreSQL) with its templates, static files and migrations compiled in. - Login through Kanidm (OpenID Connect with PKCE and a nonce, ES256), client visa-web, group visa; sessions in PostgreSQL keyed by the SHA-256 of the cookie. VISA_INSECURE_NO_LOGIN=1 for loopback development only. - Same-site check and form token on every POST; a CSP without inline script or style; security headers. - People: list with filter, page, create, edit, archive, restore. - Passports from the machine-readable zone with check digits (ICAO 9303 TD3); expiry warnings at 12 and 6 months. - Addresses in Thailand in the order Thai forms use, with the house master and the TM.30 receipt. - Append-only audit log of every view and change, with the names of the changed fields but never their values (PDPA); the Activity page. - Invented demo people (visa seed-demo, refused when login is on). - 40 tests: unit tests, pages with demo data, the forms, the audit log and the whole login against a fake OpenID provider. - CI: gitleaks over the history, fmt, clippy and the tests against PostgreSQL 18; the pre-commit secret scan.
111 lines
4.0 KiB
PL/PgSQL
111 lines
4.0 KiB
PL/PgSQL
-- visa.buildfor.life, phase 1: people, passports, addresses in Thailand,
|
|
-- sessions and the audit log (docs/PLAN.md "Data model").
|
|
--
|
|
-- Optional text is NOT NULL DEFAULT '' so the code has one empty value;
|
|
-- dates are NULL when unknown.
|
|
|
|
CREATE TABLE people (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
-- as printed in the passport
|
|
surname TEXT NOT NULL,
|
|
first_name TEXT NOT NULL,
|
|
middle_name TEXT NOT NULL DEFAULT '',
|
|
title TEXT NOT NULL DEFAULT '' CHECK (title IN ('', 'Mr', 'Mrs', 'Ms', 'Miss')),
|
|
sex TEXT NOT NULL DEFAULT '' CHECK (sex IN ('', 'F', 'M', 'X')),
|
|
date_of_birth DATE,
|
|
place_of_birth TEXT NOT NULL DEFAULT '',
|
|
-- ISO 3166-1 alpha-3, as in the passport's machine-readable zone
|
|
nationality TEXT NOT NULL DEFAULT '',
|
|
occupation TEXT NOT NULL DEFAULT '',
|
|
employer TEXT NOT NULL DEFAULT '',
|
|
phone TEXT NOT NULL DEFAULT '',
|
|
email TEXT NOT NULL DEFAULT '',
|
|
notes TEXT NOT NULL DEFAULT '',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
archived_at TIMESTAMPTZ
|
|
);
|
|
|
|
CREATE TABLE passports (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
person_id BIGINT NOT NULL REFERENCES people (id),
|
|
number TEXT NOT NULL,
|
|
issuing_country TEXT NOT NULL,
|
|
issued_on DATE,
|
|
expires_on DATE NOT NULL,
|
|
-- the issuing authority or place as printed ("issued at" on TM.7)
|
|
issued_at TEXT NOT NULL DEFAULT '',
|
|
-- the machine-readable zone as read, two lines
|
|
mrz TEXT NOT NULL DEFAULT '',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
CHECK (issued_on IS NULL OR issued_on < expires_on)
|
|
);
|
|
CREATE UNIQUE INDEX passports_number_key ON passports (issuing_country, number);
|
|
CREATE INDEX passports_person ON passports (person_id);
|
|
|
|
CREATE TABLE addresses (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
person_id BIGINT NOT NULL REFERENCES people (id),
|
|
-- in the order Thai forms use
|
|
house_no TEXT NOT NULL DEFAULT '',
|
|
moo TEXT NOT NULL DEFAULT '',
|
|
building TEXT NOT NULL DEFAULT '',
|
|
soi TEXT NOT NULL DEFAULT '',
|
|
road TEXT NOT NULL DEFAULT '',
|
|
subdistrict TEXT NOT NULL DEFAULT '',
|
|
district TEXT NOT NULL DEFAULT '',
|
|
province TEXT NOT NULL DEFAULT '',
|
|
postal_code TEXT NOT NULL DEFAULT '',
|
|
moved_in_on DATE,
|
|
moved_out_on DATE,
|
|
-- the owner or landlord who files TM.30
|
|
house_master TEXT NOT NULL DEFAULT '',
|
|
house_master_phone TEXT NOT NULL DEFAULT '',
|
|
tm30_reported_on DATE,
|
|
tm30_receipt_no TEXT NOT NULL DEFAULT '',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
CHECK (moved_out_on IS NULL OR moved_in_on IS NULL OR moved_in_on <= moved_out_on)
|
|
);
|
|
CREATE INDEX addresses_person ON addresses (person_id);
|
|
|
|
-- Signed-in sessions. The key is the SHA-256 of the cookie's value, so a dump
|
|
-- of this table holds no usable session.
|
|
CREATE TABLE sessions (
|
|
id_hash BYTEA PRIMARY KEY,
|
|
login TEXT NOT NULL,
|
|
name TEXT NOT NULL,
|
|
csrf TEXT NOT NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
expires_at TIMESTAMPTZ NOT NULL
|
|
);
|
|
CREATE INDEX sessions_expires ON sessions (expires_at);
|
|
|
|
-- Who viewed or changed which record (PDPA). The names of changed fields
|
|
-- only, never their values. Append-only.
|
|
CREATE TABLE audit_log (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
actor TEXT NOT NULL,
|
|
action TEXT NOT NULL CHECK (
|
|
action IN ('view', 'create', 'update', 'delete', 'archive', 'restore', 'sign-in', 'sign-out')
|
|
),
|
|
entity TEXT NOT NULL CHECK (entity IN ('person', 'passport', 'address', 'session')),
|
|
entity_id BIGINT,
|
|
person_id BIGINT,
|
|
fields TEXT[] NOT NULL DEFAULT '{}'
|
|
);
|
|
CREATE INDEX audit_log_person ON audit_log (person_id, at);
|
|
|
|
CREATE FUNCTION audit_log_append_only() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION 'audit_log is append-only';
|
|
END
|
|
$$;
|
|
|
|
CREATE TRIGGER audit_log_no_change
|
|
BEFORE UPDATE OR DELETE ON audit_log
|
|
FOR EACH ROW EXECUTE FUNCTION audit_log_append_only();
|