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
@@ -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