NGU-Web/server/src/migrations/076_profile_accounts.sql
Aidan Dona e4c7e165d1 Profile accounts: invites, any-Google sign-in, claiming People, visibility, Discord
Migration 076 adds a member role below viewer (the site and its own
profile, never the admin: /api/admin now needs viewer), invites, person
claims, a Discord link on accounts and people.hide_* switches. Public reads
of people go through v_people_public, which nulls hidden fields, and the
views that read people are rebuilt over it.

Admins invite an email from Data -> Profile accounts (link shown once,
emailed on request). The invitee sets a password or signs in with any
Google account under that email, asks to claim their People record, and
once approved edits it and chooses what shows at /account.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 18:14:30 -05:00

264 lines
11 KiB
SQL

-- ═══════════════════════════════════════════════════════════════
-- 076 PROFILE ACCOUNTS
--
-- Accounts for people, not only for the admin. An admin invites an
-- email (Data → Profile accounts); nobody signs up on their own. The
-- invite's link lets them set a password, or they sign in with
-- Google under that email, any Google account, not only
-- @nextgenerationofunity.org. Then they claim their People record,
-- an admin approves it, and they edit their profile and choose what
-- of it shows.
--
-- admin_users is rebuilt (its CHECKs can't change in place):
-- role takes 'member', below viewer: signs in to the
-- site and their own profile, never to the admin
-- access_source takes 'invite', an account made by an invite
-- person_id the People record it claimed (one account each)
-- invite_hash SHA-256 of the invite link's token, until used
-- invite_expires_at, invited_at, invited_by
-- discord_id, discord_name the linked Discord account, set by
-- its owner through Discord or by an admin
--
-- person_claims: a request to be a People record, approved or
-- declined by an admin.
--
-- people.hide_*: what its owner (or an admin) took off the site.
-- The value stays, for the admin; v_people_public is the record as
-- the site shows it, the hidden fields null, and every public read
-- goes through it, the views below included.
-- ═══════════════════════════════════════════════════════════════
CREATE TABLE admin_users_new (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
-- Stored lowercased. The application lowercases on every read
-- and write, so the UNIQUE index is genuinely case-insensitive
-- without depending on a collation.
email TEXT NOT NULL UNIQUE,
name TEXT,
-- Null for a Google-only account, or an invite not yet taken up.
-- A row with both can use either route in.
password_hash TEXT,
-- Google's stable subject id. Nullable, unique when present,
-- SQLite allows any number of NULLs in a unique index.
google_sub TEXT UNIQUE,
-- Listed low to high. The application treats these as a ladder,
-- not a set: each one passes every check the one below it
-- passes. The default stays 'admin' so no existing tooling
-- starts creating accounts with different powers than it did
-- yesterday.
--
-- member their own profile, on the site; no admin
-- viewer read the admin
-- editor + create and update
-- admin + delete, invite members, approve claims
-- superadmin + accounts, roles and sessions
role TEXT NOT NULL DEFAULT 'admin'
CHECK (role IN ('member', 'viewer', 'editor', 'admin', 'superadmin')),
is_active INTEGER NOT NULL DEFAULT 1 CHECK (is_active IN (0, 1)),
last_login_at TEXT,
access_source TEXT NOT NULL DEFAULT 'manual'
CHECK (access_source IN ('manual', 'group', 'invite')),
group_disabled INTEGER NOT NULL DEFAULT 0 CHECK (group_disabled IN (0, 1)),
groups_checked_at TEXT,
person_id TEXT UNIQUE REFERENCES people (id) ON DELETE SET NULL,
invite_hash TEXT UNIQUE,
invite_expires_at TEXT,
invited_at TEXT,
invited_by INTEGER REFERENCES admin_users (id) ON DELETE SET NULL,
discord_id TEXT UNIQUE,
discord_name TEXT
) STRICT;
INSERT INTO admin_users_new
(id, created_at, email, name, password_hash, google_sub, role, is_active, last_login_at,
access_source, group_disabled, groups_checked_at)
SELECT id, created_at, email, name, password_hash, google_sub, role, is_active, last_login_at,
access_source, group_disabled, groups_checked_at
FROM admin_users;
-- sessions, media, clip_jobs and drive_files name admin_users;
-- legacy mode leaves them alone through the rename.
PRAGMA legacy_alter_table = ON;
DROP TABLE admin_users;
ALTER TABLE admin_users_new RENAME TO admin_users;
PRAGMA legacy_alter_table = OFF;
CREATE TABLE person_claims (
id INTEGER PRIMARY KEY AUTOINCREMENT,
account_id INTEGER NOT NULL REFERENCES admin_users (id) ON DELETE CASCADE,
person_id TEXT NOT NULL REFERENCES people (id) ON DELETE CASCADE,
message TEXT,
status TEXT NOT NULL DEFAULT 'pending'
CHECK (status IN ('pending', 'approved', 'declined')),
created_at TEXT NOT NULL DEFAULT (datetime('now')),
decided_at TEXT,
decided_by INTEGER REFERENCES admin_users (id) ON DELETE SET NULL
) STRICT;
CREATE INDEX person_claims_status_idx ON person_claims (status, created_at);
CREATE INDEX person_claims_account_idx ON person_claims (account_id);
-- One open request per account.
CREATE UNIQUE INDEX person_claims_one_pending ON person_claims (account_id) WHERE status = 'pending';
ALTER TABLE people ADD COLUMN hide_photo INTEGER NOT NULL DEFAULT 0 CHECK (hide_photo IN (0, 1));
ALTER TABLE people ADD COLUMN hide_pronouns INTEGER NOT NULL DEFAULT 0 CHECK (hide_pronouns IN (0, 1));
ALTER TABLE people ADD COLUMN hide_tagline INTEGER NOT NULL DEFAULT 0 CHECK (hide_tagline IN (0, 1));
ALTER TABLE people ADD COLUMN hide_bio INTEGER NOT NULL DEFAULT 0 CHECK (hide_bio IN (0, 1));
ALTER TABLE people ADD COLUMN hide_email INTEGER NOT NULL DEFAULT 0 CHECK (hide_email IN (0, 1));
ALTER TABLE people ADD COLUMN hide_phone INTEGER NOT NULL DEFAULT 0 CHECK (hide_phone IN (0, 1));
ALTER TABLE people ADD COLUMN hide_location INTEGER NOT NULL DEFAULT 0 CHECK (hide_location IN (0, 1));
ALTER TABLE people ADD COLUMN hide_links INTEGER NOT NULL DEFAULT 0 CHECK (hide_links IN (0, 1));
-- A person as the site shows them. Every public read uses this, not
-- people; the admin reads people.
CREATE VIEW v_people_public AS
SELECT
id,
display_name,
sort_name,
CASE WHEN hide_pronouns = 0 THEN pronouns END AS pronouns,
CASE WHEN hide_tagline = 0 THEN tagline END AS tagline,
CASE WHEN hide_photo = 0 THEN photo END AS photo,
CASE WHEN hide_email = 0 THEN public_email END AS public_email,
CASE WHEN hide_phone = 0 THEN public_phone END AS public_phone,
CASE WHEN hide_location = 0 THEN locality END AS locality,
CASE WHEN hide_location = 0 THEN state_code END AS state_code,
country,
CASE WHEN hide_location = 0 THEN location_label END AS location_label,
is_published,
created_at,
updated_at,
CASE WHEN hide_bio = 0 THEN bio END AS bio,
primary_org_id,
is_listed,
specialty,
hide_links
FROM people;
-- The views that read people's public fields, over v_people_public.
DROP VIEW v_event_people;
CREATE VIEW v_event_people AS
SELECT
ep.event_id, ep.person_id, ep.role, ep.title, ep.sort_order,
p.display_name, p.pronouns, p.tagline, p.photo
FROM event_people ep
JOIN v_people_public p ON p.id = ep.person_id AND p.is_published = 1
WHERE ep.is_public = 1;
DROP VIEW v_timeline;
CREATE VIEW v_timeline AS
SELECT
t.id,
t.kind,
t.ref_kind,
t.ref_id,
t.precision,
t.is_featured,
t.is_published,
t.sort_order,
COALESCE(t.occurred_on, e.starts_on, pa.awarded_on) AS effective_date,
COALESCE(
t.title,
e.title,
o.name,
aw.name,
p.display_name,
tm.name
) AS effective_title,
COALESCE(t.blurb, e.tagline, o.tagline, aw.description, p.tagline, tm.tagline)
AS effective_blurb,
t.meta,
t.link_url,
-- Filename only. The directory is the frontend's business.
COALESCE(e.event_logo, e.org_logo, o.logo, aw.logo, p.photo, tm.logo)
AS effective_logo,
o.kind AS org_kind,
tm.org_id AS team_org_id,
tm.name AS team_name,
-- Whether the referenced record is itself visible. An entry must not
-- outlive the thing it points at being unpublished, a draft event
-- would otherwise leak its title and date onto a public page. Null
-- for a standalone milestone, which answers to nothing but its own
-- is_published.
CASE t.ref_kind
WHEN 'event' THEN e.is_published
WHEN 'organization' THEN o.is_published
WHEN 'person' THEN p.is_published
WHEN 'team' THEN tm.is_published
ELSE NULL
END AS ref_is_published,
t.occurred_on,
t.title AS title_override
FROM timeline_entries t
LEFT JOIN events e ON t.ref_kind = 'event' AND e.id = t.ref_id
LEFT JOIN organizations o ON t.ref_kind = 'organization' AND o.id = t.ref_id
LEFT JOIN awards aw ON t.ref_kind = 'award' AND aw.id = t.ref_id
LEFT JOIN v_people_public p ON t.ref_kind = 'person' AND p.id = t.ref_id
LEFT JOIN teams tm ON t.ref_kind = 'team' AND tm.id = t.ref_id
LEFT JOIN person_awards pa ON t.ref_kind = 'award' AND pa.award_id = t.ref_id
AND pa.id = (SELECT MIN(id) FROM person_awards
WHERE award_id = t.ref_id);
DROP VIEW v_org_leadership;
CREATE VIEW v_org_leadership AS
SELECT
a.org_id,
a.team_id,
t.name AS team_name,
t.sort_order AS team_sort_order,
a.person_id,
a.title,
a.role,
a.is_owner,
a.sort_order,
p.display_name,
p.sort_name,
p.pronouns,
p.tagline,
p.photo,
p.public_email,
p.location_label,
p.bio,
o.id AS primary_org_id,
o.name AS primary_org_name
FROM affiliations a
JOIN v_people_public p ON p.id = a.person_id AND p.is_published = 1
LEFT JOIN teams t ON t.id = a.team_id
LEFT JOIN organizations o ON o.id = p.primary_org_id AND o.is_published = 1
WHERE a.is_public = 1
AND a.ended_on IS NULL
ORDER BY a.org_id, a.is_owner DESC, a.sort_order, p.sort_name;
DROP VIEW v_event_hosts;
CREATE VIEW v_event_hosts AS
SELECT
eh.id,
eh.event_id,
eh.sort_order,
CASE WHEN eh.person_id IS NULL THEN 'organization' ELSE 'person' END
AS host_kind,
COALESCE(eh.org_id, eh.person_id) AS host_id,
COALESCE(o.name, p.display_name) AS host_name,
o.kind AS host_org_kind,
o.logo AS host_logo,
o.color AS host_color,
p.photo AS host_photo,
COALESCE(o.is_published, p.is_published) AS host_is_published,
t.id AS host_team_id,
t.name AS host_team_name
FROM event_hosts eh
LEFT JOIN organizations o ON o.id = eh.org_id
LEFT JOIN v_people_public p ON p.id = eh.person_id
LEFT JOIN teams t ON t.id = eh.team_id AND eh.org_id IS NOT NULL AND t.is_published = 1;