feat(consent): add versioned append-only consent ledger C1-C5
This commit is contained in:
1 parent
008254e44a
commit
de49f93b0b
64 files changed
+9244
-132
No files matched your search
@@ -0,0 +1,413 @@
|
||||
-- +goose Up
|
||||
-- Versioned consent ledger for the independent consents C1-C5 (PRD FR01,
|
||||
-- FR12, and 7.3): the published consent texts and an append-only record of
|
||||
-- every grant and revocation. Purposes are independent: a grant or a
|
||||
-- revocation of one purpose never changes another.
|
||||
--
|
||||
-- consent_texts holds the published text of each purpose and version. A
|
||||
-- grant references the text it was given for, so a client cannot invent a
|
||||
-- version and the stored hash always belongs to a published text.
|
||||
CREATE TABLE consent_texts (
|
||||
-- c1 personal data processing, c2 publication of the candidate card, c3
|
||||
-- automatic categorization, c4 transfer of selected contact data to one
|
||||
-- organization, c5 import of FSP results.
|
||||
purpose text NOT NULL,
|
||||
-- Identifier that clients see as ConsentTextVersion, such as
|
||||
-- "c1-2026-10-01".
|
||||
version text NOT NULL,
|
||||
-- Exact text in plain UTF-8. The C4 text contains the placeholder
|
||||
-- {organization}, which the shown text replaces with the name of the
|
||||
-- organization.
|
||||
body text NOT NULL,
|
||||
-- SHA-256 of the UTF-8 bytes of body, 32 raw bytes.
|
||||
body_sha256 bytea NOT NULL,
|
||||
-- "demo" marks a synthetic text for the demonstration on synthetic data
|
||||
-- without a legal review; "operator" a text published by the actual
|
||||
-- personal data operator, required before real data are accepted.
|
||||
kind text NOT NULL,
|
||||
-- The text applies to grants given at or after this time. The current
|
||||
-- text of a purpose is the one with the latest published_at that is not
|
||||
-- after the grant time.
|
||||
published_at timestamptz NOT NULL,
|
||||
CONSTRAINT consent_texts_pkey PRIMARY KEY (purpose, version),
|
||||
-- Target of the hash binding of consent_records.
|
||||
CONSTRAINT consent_texts_purpose_version_body_sha256_key UNIQUE (
|
||||
purpose, version, body_sha256
|
||||
),
|
||||
CONSTRAINT consent_texts_purpose_published_at_key UNIQUE (
|
||||
purpose, published_at
|
||||
),
|
||||
CONSTRAINT consent_texts_purpose_known CHECK (
|
||||
purpose IN ('c1', 'c2', 'c3', 'c4', 'c5')
|
||||
),
|
||||
CONSTRAINT consent_texts_version_format CHECK (
|
||||
version ~ '^[A-Za-z0-9._-]{1,64}$'
|
||||
),
|
||||
CONSTRAINT consent_texts_body_length CHECK (
|
||||
char_length(body) BETWEEN 1 AND 100000
|
||||
),
|
||||
-- The database encoding is fixed for the life of a database, so
|
||||
-- convert_to gives the same bytes on every evaluation.
|
||||
CONSTRAINT consent_texts_body_sha256_matches CHECK (
|
||||
body_sha256 = sha256(convert_to(body, 'UTF8'))
|
||||
),
|
||||
-- Only the C4 text names an organization.
|
||||
CONSTRAINT consent_texts_organization_placeholder CHECK (
|
||||
(purpose = 'c4') = (strpos(body, '{organization}') > 0)
|
||||
),
|
||||
CONSTRAINT consent_texts_kind_known CHECK (kind IN ('demo', 'operator'))
|
||||
);
|
||||
|
||||
-- Refuses every UPDATE, DELETE, and TRUNCATE of consent texts: a grant
|
||||
-- proves which text the subject saw only while that text stays unchanged.
|
||||
-- A changed text is a new version. The error uses SQLSTATE 23000 and the
|
||||
-- constraint name consent_texts_immutable, which the adapter maps without
|
||||
-- reading the message.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION consent_texts_refuse_change() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'consent_texts_immutable',
|
||||
TABLE = 'consent_texts',
|
||||
MESSAGE = 'consent texts are immutable: publish a new version instead';
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER consent_texts_refuse_update_delete
|
||||
BEFORE UPDATE OR DELETE ON consent_texts
|
||||
FOR EACH ROW EXECUTE FUNCTION consent_texts_refuse_change();
|
||||
|
||||
CREATE TRIGGER consent_texts_refuse_truncate
|
||||
BEFORE TRUNCATE ON consent_texts
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION consent_texts_refuse_change();
|
||||
|
||||
-- Demonstration texts, version 1 of every purpose. They are synthetic and
|
||||
-- carry no legal review; the operator publishes its own texts as new
|
||||
-- versions in a later migration before real data are accepted (PRD 7.3).
|
||||
INSERT INTO consent_texts (
|
||||
purpose, version, body, body_sha256, kind, published_at
|
||||
)
|
||||
SELECT
|
||||
seed.purpose,
|
||||
seed.version,
|
||||
seed.body,
|
||||
sha256(convert_to(seed.body, 'UTF8')),
|
||||
'demo',
|
||||
'2026-10-01T00:00:00Z'::timestamptz
|
||||
FROM (
|
||||
VALUES
|
||||
(
|
||||
'c1',
|
||||
'c1-2026-10-01',
|
||||
'I consent to the processing of my personal data for registration '
|
||||
|| 'and the use of the service.' || E'\n\n'
|
||||
|| 'The full text is published by the operator.'
|
||||
),
|
||||
(
|
||||
'c2',
|
||||
'c2-2026-10-01',
|
||||
'I consent to the publication of my candidate card with the fields '
|
||||
|| 'I select in the publication settings.' || E'\n\n'
|
||||
|| 'The full text is published by the operator.'
|
||||
),
|
||||
(
|
||||
'c3',
|
||||
'c3-2026-10-01',
|
||||
'I consent to the automatic categorization of my assessment '
|
||||
|| 'results. Without this consent I can request a manual assessment '
|
||||
|| 'by the same rubric.' || E'\n\n'
|
||||
|| 'The full text is published by the operator.'
|
||||
),
|
||||
(
|
||||
'c4',
|
||||
'c4-2026-10-01',
|
||||
'I consent to the transfer of the contact data I select to '
|
||||
|| '{organization} for this invitation.' || E'\n\n'
|
||||
|| 'The full text is published by the operator.'
|
||||
),
|
||||
(
|
||||
'c5',
|
||||
'c5-2026-10-01',
|
||||
'I consent to the import of my FSP results into my profile.'
|
||||
|| E'\n\n'
|
||||
|| 'The full text is published by the operator.'
|
||||
)
|
||||
) AS seed (purpose, version, body);
|
||||
|
||||
-- Reports whether a C4 field set is canonical: one to four distinct known
|
||||
-- contact kinds (ContactFieldKind) in ascending byte order without NULLs.
|
||||
-- A canonical form makes equal sets equal arrays.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION consent_field_set_is_canonical(fields text []) RETURNS boolean
|
||||
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
||||
SELECT
|
||||
array_ndims(fields) = 1
|
||||
AND cardinality(fields) BETWEEN 1 AND 4
|
||||
AND array_position(fields, NULL) IS NULL
|
||||
AND fields <@ ARRAY['email', 'full_name', 'phone', 'telegram']::text []
|
||||
AND fields = ARRAY(
|
||||
SELECT DISTINCT field COLLATE "C"
|
||||
FROM unnest(fields) AS field
|
||||
ORDER BY field COLLATE "C"
|
||||
);
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- One row per grant. A grant is never updated except for the one-time
|
||||
-- revocation stamp: a new text version or a grant after a revocation is a
|
||||
-- new row, so the history of a purpose is the sequence of its rows. Every
|
||||
-- time comes from the application clock.
|
||||
--
|
||||
-- The rows are personal data of the subject: never copy them into logs,
|
||||
-- metrics, or events beyond the record ID and purpose.
|
||||
CREATE TABLE consent_records (
|
||||
-- UUIDv7 generated in Go.
|
||||
id uuid NOT NULL,
|
||||
-- The account that gave the consent.
|
||||
subject_id uuid NOT NULL,
|
||||
purpose text NOT NULL,
|
||||
text_version text NOT NULL,
|
||||
-- SHA-256 of exactly the text the subject saw, 32 raw bytes. For C1-C3
|
||||
-- and C5 it equals the hash of the published text; for C4 it is the
|
||||
-- hash of the published text with organization_name filled in, which a
|
||||
-- trigger checks.
|
||||
text_hash bytea NOT NULL,
|
||||
-- The hash that must equal the published text: text_hash except for
|
||||
-- C4, NULL for C4, so the composite foreign key below skips C4.
|
||||
published_text_hash bytea GENERATED ALWAYS AS (
|
||||
CASE WHEN purpose <> 'c4' THEN text_hash END
|
||||
) STORED,
|
||||
-- Where the grant was given: registration (C1 only), consent_settings
|
||||
-- (C1-C3 and C5), or invitation_acceptance (C4 only).
|
||||
source text NOT NULL,
|
||||
-- C4 scope: the organization, the name of the organization as the
|
||||
-- shown text named it (a snapshot that a later rename never changes),
|
||||
-- the canonical set of contact kinds, and the invitation and its terms
|
||||
-- version that the acceptance accepted. NULL for every other purpose.
|
||||
-- The organization and invitation tables do not exist yet; their
|
||||
-- foreign keys arrive with them.
|
||||
organization_id uuid,
|
||||
organization_name text,
|
||||
field_set text [],
|
||||
invitation_id uuid,
|
||||
invitation_terms_version bigint,
|
||||
granted_at timestamptz NOT NULL,
|
||||
-- Set once; the row is never updated again.
|
||||
revoked_at timestamptz,
|
||||
-- consent_settings: the subject revoked it in the product;
|
||||
-- support_request: an operator recorded a revocation the subject asked
|
||||
-- for through support.
|
||||
revocation_source text,
|
||||
CONSTRAINT consent_records_pkey PRIMARY KEY (id),
|
||||
-- Consent evidence must outlive a careless account delete: an account
|
||||
-- with consent history cannot be deleted until the deleting procedure
|
||||
-- handles the ledger explicitly (PRD 7.3 keeps consents for three
|
||||
-- years). The unverified account cleanup deletes the records of the
|
||||
-- accounts it removes in the same statement, and the re-registration of
|
||||
-- an unverified account deletes its registration C1.
|
||||
CONSTRAINT consent_records_subject_id_fkey FOREIGN KEY (subject_id)
|
||||
REFERENCES users (id) ON DELETE RESTRICT,
|
||||
CONSTRAINT consent_records_text_fkey FOREIGN KEY (
|
||||
purpose, text_version
|
||||
) REFERENCES consent_texts (purpose, version),
|
||||
-- MATCH SIMPLE skips the check when published_text_hash is NULL (C4).
|
||||
CONSTRAINT consent_records_published_text_fkey FOREIGN KEY (
|
||||
purpose, text_version, published_text_hash
|
||||
) REFERENCES consent_texts (purpose, version, body_sha256),
|
||||
CONSTRAINT consent_records_purpose_known CHECK (
|
||||
purpose IN ('c1', 'c2', 'c3', 'c4', 'c5')
|
||||
),
|
||||
CONSTRAINT consent_records_text_version_format CHECK (
|
||||
text_version ~ '^[A-Za-z0-9._-]{1,64}$'
|
||||
),
|
||||
CONSTRAINT consent_records_text_hash_sha256 CHECK (
|
||||
octet_length(text_hash) = 32
|
||||
),
|
||||
CONSTRAINT consent_records_source_of_purpose CHECK (
|
||||
(purpose = 'c1' AND source IN ('registration', 'consent_settings'))
|
||||
OR (purpose IN ('c2', 'c3', 'c5') AND source = 'consent_settings')
|
||||
OR (purpose = 'c4' AND source = 'invitation_acceptance')
|
||||
),
|
||||
-- C4 always has its scope; no other purpose has one.
|
||||
CONSTRAINT consent_records_c4_scope CHECK (
|
||||
(
|
||||
purpose = 'c4'
|
||||
AND organization_id IS NOT NULL
|
||||
AND organization_name IS NOT NULL
|
||||
AND char_length(organization_name) BETWEEN 1 AND 200
|
||||
AND organization_name !~ '[[:cntrl:]]'
|
||||
AND field_set IS NOT NULL
|
||||
AND consent_field_set_is_canonical(field_set)
|
||||
AND invitation_id IS NOT NULL
|
||||
AND invitation_terms_version IS NOT NULL
|
||||
AND invitation_terms_version > 0
|
||||
)
|
||||
OR (
|
||||
purpose <> 'c4'
|
||||
AND organization_id IS NULL
|
||||
AND organization_name IS NULL
|
||||
AND field_set IS NULL
|
||||
AND invitation_id IS NULL
|
||||
AND invitation_terms_version IS NULL
|
||||
)
|
||||
),
|
||||
CONSTRAINT consent_records_revocation_stamp CHECK (
|
||||
(revoked_at IS NULL) = (revocation_source IS NULL)
|
||||
AND (
|
||||
revocation_source IS NULL
|
||||
OR revocation_source IN ('consent_settings', 'support_request')
|
||||
)
|
||||
AND (revoked_at IS NULL OR revoked_at >= granted_at)
|
||||
)
|
||||
);
|
||||
|
||||
-- Serves the subject's history, newest first, and the foreign key from
|
||||
-- users.
|
||||
CREATE INDEX consent_records_subject_granted_idx
|
||||
ON consent_records (subject_id, granted_at DESC, id DESC);
|
||||
|
||||
-- One unrevoked grant per subject, purpose, and text version: repeating a
|
||||
-- grant of the same version returns the existing record. A newer version is
|
||||
-- a new row next to the older one.
|
||||
CREATE UNIQUE INDEX consent_records_active_version_key
|
||||
ON consent_records (subject_id, purpose, text_version)
|
||||
WHERE revoked_at IS NULL AND purpose <> 'c4';
|
||||
|
||||
-- One C4 grant per invitation, revoked or not: a repeated acceptance
|
||||
-- returns the existing record, and a revoked C4 never comes back.
|
||||
CREATE UNIQUE INDEX consent_records_invitation_key
|
||||
ON consent_records (invitation_id)
|
||||
WHERE invitation_id IS NOT NULL;
|
||||
|
||||
-- Serves the current C4 grants of a subject for one organization and their
|
||||
-- revocation.
|
||||
CREATE INDEX consent_records_active_organization_idx
|
||||
ON consent_records (subject_id, organization_id)
|
||||
WHERE purpose = 'c4' AND revoked_at IS NULL;
|
||||
|
||||
-- Refuses a grant for a text version that is not published at the grant
|
||||
-- time (SQLSTATE 23514, constraint consent_records_text_published) and a C4
|
||||
-- grant whose hash is not the hash of the published text with its
|
||||
-- organization_name filled in (23514, consent_records_c4_text_hash). A
|
||||
-- version that does not exist at all is left to the foreign key, and a C4
|
||||
-- row without a name to consent_records_c4_scope.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION consent_records_require_published_text() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM consent_texts AS text
|
||||
WHERE
|
||||
text.purpose = NEW.purpose
|
||||
AND text.version = NEW.text_version
|
||||
AND text.published_at > NEW.granted_at
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'consent_records_text_published',
|
||||
TABLE = 'consent_records',
|
||||
MESSAGE = 'the consent text is not published at the grant time';
|
||||
END IF;
|
||||
IF NEW.purpose = 'c4' AND NEW.organization_name IS NOT NULL AND EXISTS (
|
||||
SELECT 1 FROM consent_texts AS text
|
||||
WHERE
|
||||
text.purpose = NEW.purpose
|
||||
AND text.version = NEW.text_version
|
||||
AND sha256(convert_to(
|
||||
replace(text.body, '{organization}', NEW.organization_name),
|
||||
'UTF8'
|
||||
)) IS DISTINCT FROM NEW.text_hash
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'consent_records_c4_text_hash',
|
||||
TABLE = 'consent_records',
|
||||
MESSAGE = 'the C4 hash does not match the published text for the organization name';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER consent_records_require_published_text
|
||||
BEFORE INSERT ON consent_records
|
||||
FOR EACH ROW EXECUTE FUNCTION consent_records_require_published_text();
|
||||
|
||||
-- Allows exactly one UPDATE of a record: setting revoked_at and
|
||||
-- revocation_source on an unrevoked record, with every other column
|
||||
-- unchanged. Every other UPDATE is refused with SQLSTATE 23000 and the
|
||||
-- constraint name consent_records_immutable; the message quotes no row
|
||||
-- values. Deletes are guarded separately below.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION consent_records_refuse_change() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF OLD.revoked_at IS NOT NULL
|
||||
OR NEW.revoked_at IS NULL
|
||||
OR (
|
||||
NEW.id, NEW.subject_id, NEW.purpose, NEW.text_version,
|
||||
NEW.text_hash, NEW.source, NEW.organization_id,
|
||||
NEW.organization_name, NEW.field_set,
|
||||
NEW.invitation_id, NEW.invitation_terms_version, NEW.granted_at
|
||||
) IS DISTINCT FROM (
|
||||
OLD.id, OLD.subject_id, OLD.purpose, OLD.text_version,
|
||||
OLD.text_hash, OLD.source, OLD.organization_id,
|
||||
OLD.organization_name, OLD.field_set,
|
||||
OLD.invitation_id, OLD.invitation_terms_version, OLD.granted_at
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'consent_records_immutable',
|
||||
TABLE = 'consent_records',
|
||||
MESSAGE = 'consent records are append-only: only an unrevoked record can be revoked, once';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER consent_records_refuse_change
|
||||
BEFORE UPDATE ON consent_records
|
||||
FOR EACH ROW EXECUTE FUNCTION consent_records_refuse_change();
|
||||
|
||||
-- Allows deleting a record only while its subject is not a confirmed
|
||||
-- account: the unverified account cleanup and the re-registration of an
|
||||
-- unverified account. The ledger of a confirmed account is consent
|
||||
-- evidence and stays (SQLSTATE 23000, constraint consent_records_retained).
|
||||
-- The retention rule for consents (PRD 7.3, three years) and the account
|
||||
-- deletion procedure will extend this condition in their own migration.
|
||||
-- TRUNCATE fires no row trigger and stays allowed for test isolation.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION consent_records_refuse_delete() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM users
|
||||
WHERE users.id = OLD.subject_id AND users.email_verified_at IS NOT NULL
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'consent_records_retained',
|
||||
TABLE = 'consent_records',
|
||||
MESSAGE = 'consent records of a confirmed account are retained';
|
||||
END IF;
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER consent_records_refuse_delete
|
||||
BEFORE DELETE ON consent_records
|
||||
FOR EACH ROW EXECUTE FUNCTION consent_records_refuse_delete();
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE consent_records;
|
||||
DROP TABLE consent_texts;
|
||||
DROP FUNCTION consent_records_refuse_delete();
|
||||
DROP FUNCTION consent_records_refuse_change();
|
||||
DROP FUNCTION consent_records_require_published_text();
|
||||
DROP FUNCTION consent_field_set_is_canonical(text []);
|
||||
DROP FUNCTION consent_texts_refuse_change();
|
||||
@@ -0,0 +1,377 @@
|
||||
-- name: GetConsentText :one
|
||||
SELECT
|
||||
purpose,
|
||||
version,
|
||||
body,
|
||||
body_sha256,
|
||||
kind,
|
||||
published_at
|
||||
FROM consent_texts
|
||||
WHERE
|
||||
purpose = sqlc.arg(purpose)
|
||||
AND version = sqlc.arg(version);
|
||||
|
||||
-- The text of a purpose that applies at a time: the latest publication not
|
||||
-- after it.
|
||||
-- name: GetCurrentConsentText :one
|
||||
SELECT
|
||||
purpose,
|
||||
version,
|
||||
body,
|
||||
body_sha256,
|
||||
kind,
|
||||
published_at
|
||||
FROM consent_texts
|
||||
WHERE
|
||||
purpose = sqlc.arg(purpose)
|
||||
AND published_at <= sqlc.arg(at)
|
||||
ORDER BY published_at DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- Records a grant of C1, C2, C3, or C5. An unrevoked record of the same
|
||||
-- subject, purpose, and text version already exists when no row returns;
|
||||
-- the insert then changes nothing and does not abort the transaction.
|
||||
-- name: InsertConsentRecord :one
|
||||
INSERT INTO consent_records (
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
granted_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(subject_id),
|
||||
sqlc.arg(purpose),
|
||||
sqlc.arg(text_version),
|
||||
sqlc.arg(text_hash),
|
||||
sqlc.arg(source),
|
||||
sqlc.arg(granted_at)
|
||||
)
|
||||
ON CONFLICT (subject_id, purpose, text_version)
|
||||
WHERE revoked_at IS NULL AND purpose <> 'c4'
|
||||
DO NOTHING
|
||||
RETURNING
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
(
|
||||
CASE
|
||||
WHEN exists(
|
||||
SELECT 1 FROM consent_records AS newer
|
||||
WHERE
|
||||
newer.subject_id = consent_records.subject_id
|
||||
AND newer.purpose = consent_records.purpose
|
||||
AND newer.revoked_at IS NULL
|
||||
AND (newer.granted_at, newer.id)
|
||||
> (consent_records.granted_at, consent_records.id)
|
||||
) THEN 'superseded'
|
||||
ELSE 'active'
|
||||
END
|
||||
)::text AS state;
|
||||
|
||||
-- Records a C4 grant with its scope. The invitation already has a C4 record
|
||||
-- when no row returns; the insert then changes nothing and does not abort
|
||||
-- the transaction.
|
||||
-- name: InsertContactConsentRecord :one
|
||||
INSERT INTO consent_records (
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(subject_id),
|
||||
'c4',
|
||||
sqlc.arg(text_version),
|
||||
sqlc.arg(text_hash),
|
||||
'invitation_acceptance',
|
||||
sqlc.arg(organization_id),
|
||||
sqlc.arg(organization_name),
|
||||
sqlc.arg(field_set)::text [],
|
||||
sqlc.arg(invitation_id),
|
||||
sqlc.arg(invitation_terms_version),
|
||||
sqlc.arg(granted_at)
|
||||
)
|
||||
ON CONFLICT (invitation_id) WHERE invitation_id IS NOT NULL
|
||||
DO NOTHING
|
||||
RETURNING
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
'active'::text AS state;
|
||||
|
||||
-- name: GetUnrevokedConsentRecordByVersion :one
|
||||
SELECT
|
||||
record.id,
|
||||
record.subject_id,
|
||||
record.purpose,
|
||||
record.text_version,
|
||||
record.text_hash,
|
||||
record.source,
|
||||
record.organization_id,
|
||||
record.organization_name,
|
||||
record.field_set,
|
||||
record.invitation_id,
|
||||
record.invitation_terms_version,
|
||||
record.granted_at,
|
||||
record.revoked_at,
|
||||
record.revocation_source,
|
||||
(
|
||||
CASE
|
||||
WHEN exists(
|
||||
SELECT 1 FROM consent_records AS newer
|
||||
WHERE
|
||||
newer.subject_id = record.subject_id
|
||||
AND newer.purpose = record.purpose
|
||||
AND newer.revoked_at IS NULL
|
||||
AND (newer.granted_at, newer.id)
|
||||
> (record.granted_at, record.id)
|
||||
) THEN 'superseded'
|
||||
ELSE 'active'
|
||||
END
|
||||
)::text AS state
|
||||
FROM consent_records AS record
|
||||
WHERE
|
||||
record.subject_id = sqlc.arg(subject_id)
|
||||
AND record.purpose = sqlc.arg(purpose)
|
||||
AND record.text_version = sqlc.arg(text_version)
|
||||
AND record.revoked_at IS NULL;
|
||||
|
||||
-- name: GetContactConsentRecordByInvitation :one
|
||||
SELECT
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
(
|
||||
CASE WHEN revoked_at IS NULL THEN 'active' ELSE 'revoked' END
|
||||
)::text AS state
|
||||
FROM consent_records
|
||||
WHERE
|
||||
subject_id = sqlc.arg(subject_id)
|
||||
AND invitation_id = sqlc.arg(invitation_id);
|
||||
|
||||
-- The newest unrevoked record of a purpose other than C4.
|
||||
-- name: GetCurrentConsentRecord :one
|
||||
SELECT
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
'active'::text AS state
|
||||
FROM consent_records
|
||||
WHERE
|
||||
subject_id = sqlc.arg(subject_id)
|
||||
AND purpose = sqlc.arg(purpose)
|
||||
AND purpose <> 'c4'
|
||||
AND revoked_at IS NULL
|
||||
ORDER BY granted_at DESC, id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- name: ListCurrentContactConsentRecords :many
|
||||
SELECT
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
'active'::text AS state
|
||||
FROM consent_records
|
||||
WHERE
|
||||
subject_id = sqlc.arg(subject_id)
|
||||
AND purpose = 'c4'
|
||||
AND organization_id = sqlc.arg(organization_id)
|
||||
AND revoked_at IS NULL
|
||||
ORDER BY granted_at DESC, id DESC;
|
||||
|
||||
-- Revokes every unrevoked record of a purpose other than C4, superseded ones
|
||||
-- included, and returns them. A revocation time before the grant time, from
|
||||
-- clock skew between instances, is stamped at the grant time instead.
|
||||
-- name: RevokeConsentRecords :many
|
||||
UPDATE consent_records
|
||||
SET
|
||||
revoked_at = greatest(sqlc.arg(revoked_at)::timestamptz, granted_at),
|
||||
revocation_source = sqlc.arg(revocation_source)
|
||||
WHERE
|
||||
subject_id = sqlc.arg(subject_id)
|
||||
AND purpose = sqlc.arg(purpose)
|
||||
AND purpose <> 'c4'
|
||||
AND revoked_at IS NULL
|
||||
RETURNING
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
'revoked'::text AS state;
|
||||
|
||||
-- Revokes every unrevoked C4 record of the subject for one organization and
|
||||
-- returns them, with the same clamp of the revocation time as above.
|
||||
-- name: RevokeContactConsentRecords :many
|
||||
UPDATE consent_records
|
||||
SET
|
||||
revoked_at = greatest(sqlc.arg(revoked_at)::timestamptz, granted_at),
|
||||
revocation_source = sqlc.arg(revocation_source)
|
||||
WHERE
|
||||
subject_id = sqlc.arg(subject_id)
|
||||
AND purpose = 'c4'
|
||||
AND organization_id = sqlc.arg(organization_id)
|
||||
AND revoked_at IS NULL
|
||||
RETURNING
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
'revoked'::text AS state;
|
||||
|
||||
-- One page of the subject's ledger, newest grant first, optionally limited
|
||||
-- to one state. Every record query is scoped by the subject, so a caller
|
||||
-- never reads or revokes another subject's consent. The state is computed,
|
||||
-- never stored: revoked once revoked_at is set; superseded when a newer
|
||||
-- unrevoked record of the same subject and purpose exists (never for C4,
|
||||
-- whose records are scoped by invitation); active otherwise. The page continues after the (granted_at, id) position of
|
||||
-- the last record of the previous page.
|
||||
-- name: ListConsentRecords :many
|
||||
WITH ledger AS (
|
||||
SELECT
|
||||
record.id,
|
||||
record.subject_id,
|
||||
record.purpose,
|
||||
record.text_version,
|
||||
record.text_hash,
|
||||
record.source,
|
||||
record.organization_id,
|
||||
record.organization_name,
|
||||
record.field_set,
|
||||
record.invitation_id,
|
||||
record.invitation_terms_version,
|
||||
record.granted_at,
|
||||
record.revoked_at,
|
||||
record.revocation_source,
|
||||
(
|
||||
CASE
|
||||
WHEN record.revoked_at IS NOT NULL THEN 'revoked'
|
||||
WHEN record.purpose <> 'c4' AND exists(
|
||||
SELECT 1 FROM consent_records AS newer
|
||||
WHERE
|
||||
newer.subject_id = record.subject_id
|
||||
AND newer.purpose = record.purpose
|
||||
AND newer.revoked_at IS NULL
|
||||
AND (newer.granted_at, newer.id)
|
||||
> (record.granted_at, record.id)
|
||||
) THEN 'superseded'
|
||||
ELSE 'active'
|
||||
END
|
||||
)::text AS state
|
||||
FROM consent_records AS record
|
||||
WHERE record.subject_id = sqlc.arg(subject_id)
|
||||
)
|
||||
|
||||
SELECT
|
||||
ledger.id,
|
||||
ledger.subject_id,
|
||||
ledger.purpose,
|
||||
ledger.text_version,
|
||||
ledger.text_hash,
|
||||
ledger.source,
|
||||
ledger.organization_id,
|
||||
ledger.organization_name,
|
||||
ledger.field_set,
|
||||
ledger.invitation_id,
|
||||
ledger.invitation_terms_version,
|
||||
ledger.granted_at,
|
||||
ledger.revoked_at,
|
||||
ledger.revocation_source,
|
||||
ledger.state
|
||||
FROM ledger
|
||||
WHERE
|
||||
(sqlc.narg(state)::text IS NULL OR ledger.state = sqlc.narg(state)::text)
|
||||
AND (
|
||||
sqlc.narg(after_granted_at)::timestamptz IS NULL
|
||||
OR (ledger.granted_at, ledger.id) < (
|
||||
sqlc.narg(after_granted_at)::timestamptz,
|
||||
sqlc.narg(after_id)::uuid
|
||||
)
|
||||
)
|
||||
ORDER BY ledger.granted_at DESC, ledger.id DESC
|
||||
LIMIT sqlc.arg(row_limit);
|
||||
@@ -64,35 +64,64 @@ WHERE email_normalized = sqlc.arg(email_normalized);
|
||||
-- Replaces the registration of an unverified account and advances the
|
||||
-- registration version in the same statement, so no caller can choose it.
|
||||
-- The version check rejects a writer that read an older row; a verified
|
||||
-- account never changes.
|
||||
-- account never changes. The same statement deletes the C1 that the earlier
|
||||
-- registration of this account recorded, so the consent of an earlier
|
||||
-- registrant never stands for the new one; the new registration records its
|
||||
-- own C1 in the same transaction. A version conflict deletes nothing.
|
||||
-- name: UpdateUserRegistration :one
|
||||
UPDATE users
|
||||
SET
|
||||
email = sqlc.arg(email),
|
||||
password_hash = sqlc.arg(password_hash),
|
||||
scenario = sqlc.arg(scenario),
|
||||
onboarding_step = sqlc.arg(onboarding_step),
|
||||
registration_version = registration_version + 1,
|
||||
last_registered_at = sqlc.arg(last_registered_at),
|
||||
updated_at = sqlc.arg(updated_at),
|
||||
version = version + 1
|
||||
WHERE
|
||||
id = sqlc.arg(id)
|
||||
AND version = sqlc.arg(expected_version)
|
||||
AND email_verified_at IS NULL
|
||||
RETURNING
|
||||
id,
|
||||
email,
|
||||
email_normalized,
|
||||
password_hash,
|
||||
scenario,
|
||||
onboarding_step,
|
||||
email_verified_at,
|
||||
registration_version,
|
||||
last_registered_at,
|
||||
version,
|
||||
created_at,
|
||||
updated_at;
|
||||
WITH updated AS (
|
||||
UPDATE users
|
||||
SET
|
||||
email = sqlc.arg(email),
|
||||
password_hash = sqlc.arg(password_hash),
|
||||
scenario = sqlc.arg(scenario),
|
||||
onboarding_step = sqlc.arg(onboarding_step),
|
||||
registration_version = registration_version + 1,
|
||||
last_registered_at = sqlc.arg(last_registered_at),
|
||||
updated_at = sqlc.arg(updated_at),
|
||||
version = version + 1
|
||||
WHERE
|
||||
users.id = sqlc.arg(id)
|
||||
AND users.version = sqlc.arg(expected_version)
|
||||
AND users.email_verified_at IS NULL
|
||||
RETURNING
|
||||
users.id,
|
||||
users.email,
|
||||
users.email_normalized,
|
||||
users.password_hash,
|
||||
users.scenario,
|
||||
users.onboarding_step,
|
||||
users.email_verified_at,
|
||||
users.registration_version,
|
||||
users.last_registered_at,
|
||||
users.version,
|
||||
users.created_at,
|
||||
users.updated_at
|
||||
),
|
||||
|
||||
discarded_consent AS (
|
||||
DELETE FROM consent_records
|
||||
USING updated
|
||||
WHERE
|
||||
consent_records.subject_id = updated.id
|
||||
AND consent_records.purpose = 'c1'
|
||||
AND consent_records.source = 'registration'
|
||||
)
|
||||
|
||||
SELECT
|
||||
updated.id,
|
||||
updated.email,
|
||||
updated.email_normalized,
|
||||
updated.password_hash,
|
||||
updated.scenario,
|
||||
updated.onboarding_step,
|
||||
updated.email_verified_at,
|
||||
updated.registration_version,
|
||||
updated.last_registered_at,
|
||||
updated.version,
|
||||
updated.created_at,
|
||||
updated.updated_at
|
||||
FROM updated;
|
||||
|
||||
-- Verifies the email only for the registration version the consumed token
|
||||
-- was issued for.
|
||||
@@ -121,14 +150,17 @@ RETURNING
|
||||
updated_at;
|
||||
|
||||
-- Deletes one batch of accounts that stayed unverified since before cutoff,
|
||||
-- oldest first, with their tokens, sessions, mail requests, and mail
|
||||
-- deliveries (and through the deliveries their attempts). Every lock is taken
|
||||
-- oldest first, with their tokens, sessions, mail requests, mail deliveries
|
||||
-- (and through the deliveries their attempts), and consent records. The
|
||||
-- consent records reference the account with ON DELETE RESTRICT, so they are
|
||||
-- deleted explicitly in the same statement: a registration never confirmed
|
||||
-- by its email owner is not kept as consent evidence. Every lock is taken
|
||||
-- with SKIP LOCKED, so the statement never waits: an account whose row is
|
||||
-- locked, for example by a re-registration, a token insert, or a delivery
|
||||
-- insert, is skipped, and so is an account with any token, session, mail
|
||||
-- request, or delivery row locked, for example by a confirmation that
|
||||
-- consumed a token, the delivery consumer, or a worker recording an attempt.
|
||||
-- Skipped accounts stay for the next run.
|
||||
-- locked, for example by a re-registration, a token insert, a delivery
|
||||
-- insert, or a consent insert, is skipped, and so is an account with any
|
||||
-- token, session, mail request, delivery, or consent row locked, for example
|
||||
-- by a confirmation that consumed a token, the delivery consumer, or a
|
||||
-- worker recording an attempt. Skipped accounts stay for the next run.
|
||||
-- Taking the account lock before the token locks is the reverse of the
|
||||
-- documented order, which is safe only because no lock here waits.
|
||||
-- name: DeleteUnverifiedUsersRegisteredBefore :execrows
|
||||
@@ -171,8 +203,15 @@ locked_delivery AS (
|
||||
FOR UPDATE SKIP LOCKED
|
||||
),
|
||||
|
||||
-- An account qualifies only when every token, session, mail request, and
|
||||
-- delivery row visible to the statement was locked above.
|
||||
locked_consent AS (
|
||||
SELECT consent.subject_id
|
||||
FROM consent_records AS consent
|
||||
WHERE consent.subject_id IN (SELECT candidate.id FROM candidate)
|
||||
FOR UPDATE SKIP LOCKED
|
||||
),
|
||||
|
||||
-- An account qualifies only when every token, session, mail request,
|
||||
-- delivery, and consent row visible to the statement was locked above.
|
||||
deletable AS (
|
||||
SELECT candidate.id
|
||||
FROM candidate
|
||||
@@ -205,6 +244,21 @@ deletable AS (
|
||||
SELECT count(*) FROM locked_delivery
|
||||
WHERE locked_delivery.recipient_user_id = candidate.id
|
||||
)
|
||||
AND (
|
||||
SELECT count(*) FROM consent_records AS consent
|
||||
WHERE consent.subject_id = candidate.id
|
||||
) = (
|
||||
SELECT count(*) FROM locked_consent
|
||||
WHERE locked_consent.subject_id = candidate.id
|
||||
)
|
||||
),
|
||||
|
||||
-- The restricting foreign key is checked at the end of the statement, after
|
||||
-- these rows are gone.
|
||||
deleted_consent AS (
|
||||
DELETE FROM consent_records
|
||||
USING deletable
|
||||
WHERE consent_records.subject_id = deletable.id
|
||||
)
|
||||
|
||||
DELETE FROM users
|
||||
|
||||
Reference in new issue
Block a user