feat(admin): bans, admin logout and time played (schema 64)
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / postgres-tools (push) Successful in 2m9s
Foundation / server-image (push) Successful in 5m23s
Foundation / image (push) Successful in 6m10s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / postgres-tools (push) Successful in 2m9s
Foundation / server-image (push) Successful in 5m23s
Foundation / image (push) Successful in 6m10s
ADR-0182 section 11. - Append-only account bans; a ban also revokes the account's grants (its live session closes at the next control renewal) and issue_account_grant refuses a banned account (EVL20 -> broker 403 account_banned). Lifting is another row. - admin_logout_account_v1 revokes by account id. - Session intervals recorded by claim/renew/release (signatures unchanged): time played, sessions and last seen per account. - The region view carries each cell's lineage root; the portal counts living wildlife descended from an account's lives. - Portal: Ban/Unban and Log out with reason and history; time played column. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
-- Account administration (ADR-0182 section 6, 2026-10-27): bans, an admin logout and time
|
||||
-- played. A ban is an append-only row; the newest row per account decides. A banned account
|
||||
-- gets no grant (SQLSTATE EVL20, which the broker reports as account_banned), and banning
|
||||
-- also revokes its grants, so a live session closes at its next control renewal. Session
|
||||
-- intervals are recorded by the session-control functions the regions already call: a claim
|
||||
-- opens one, a renewal moves its last-seen time, a release closes it. A session that is
|
||||
-- never released ends at its last renewal.
|
||||
|
||||
CREATE TABLE evolution.account_bans (
|
||||
sequence bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
account_id uuid NOT NULL,
|
||||
banned boolean NOT NULL,
|
||||
author_subject text NOT NULL CHECK (octet_length(author_subject) BETWEEN 1 AND 255),
|
||||
reason text NOT NULL DEFAULT '' CHECK (octet_length(reason) <= 500),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX account_bans_by_account ON evolution.account_bans(account_id, sequence);
|
||||
CREATE TRIGGER account_bans_immutable BEFORE UPDATE OR DELETE ON evolution.account_bans
|
||||
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite();
|
||||
|
||||
CREATE TABLE evolution.session_intervals (
|
||||
world_id evolution.u64 NOT NULL,
|
||||
account_id uuid NOT NULL,
|
||||
generation evolution.u64 NOT NULL,
|
||||
region_id evolution.u64 NOT NULL,
|
||||
started_at timestamptz NOT NULL,
|
||||
last_seen timestamptz NOT NULL,
|
||||
ended_at timestamptz,
|
||||
PRIMARY KEY (world_id, account_id, generation)
|
||||
);
|
||||
|
||||
CREATE FUNCTION evolution.account_banned(p_account uuid)
|
||||
RETURNS boolean LANGUAGE sql STABLE SET search_path = pg_catalog, pg_temp AS $$
|
||||
SELECT coalesce((SELECT b.banned FROM evolution.account_bans b
|
||||
WHERE b.account_id = p_account ORDER BY b.sequence DESC LIMIT 1), false)
|
||||
$$;
|
||||
REVOKE ALL ON FUNCTION evolution.account_banned(uuid) FROM PUBLIC;
|
||||
|
||||
-- Raises the account's accepted serial past every grant issued and expires its control, as
|
||||
-- revoke_account_grants does for a Keycloak identity.
|
||||
CREATE FUNCTION evolution.revoke_account_by_id(p_world evolution.u64, p_account uuid)
|
||||
RETURNS boolean LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp AS $$
|
||||
DECLARE r evolution.account_identities%ROWTYPE;
|
||||
BEGIN
|
||||
SELECT * INTO r FROM evolution.account_identities x WHERE x.account_id = p_account FOR UPDATE;
|
||||
IF NOT FOUND THEN
|
||||
UPDATE evolution.account_controls a
|
||||
SET expires_at = CASE WHEN a.region_id IS NULL THEN NULL
|
||||
ELSE least(a.expires_at, clock_timestamp()) END
|
||||
WHERE a.world_id = p_world AND a.account_id = p_account;
|
||||
RETURN FOUND;
|
||||
END IF;
|
||||
INSERT INTO evolution.account_controls AS a (world_id, account_id, serial, generation)
|
||||
VALUES (p_world, r.account_id, greatest(r.last_serial, 1), 1)
|
||||
ON CONFLICT (world_id, account_id) DO UPDATE
|
||||
SET serial = greatest(a.serial, EXCLUDED.serial),
|
||||
expires_at = CASE WHEN a.region_id IS NULL THEN NULL
|
||||
ELSE least(a.expires_at, clock_timestamp()) END;
|
||||
RETURN true;
|
||||
END $$;
|
||||
REVOKE ALL ON FUNCTION evolution.revoke_account_by_id(evolution.u64, uuid) FROM PUBLIC;
|
||||
|
||||
-- Admin: ban or lift a ban (recorded with who and why); a ban also logs the account out.
|
||||
CREATE FUNCTION evolution.admin_set_account_ban_v1(p_world evolution.u64, p_account uuid,
|
||||
p_banned boolean, p_subject text, p_reason text)
|
||||
RETURNS boolean LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
BEGIN
|
||||
IF p_world IS NULL OR p_world = 0 OR p_account IS NULL OR p_banned IS NULL THEN
|
||||
RAISE EXCEPTION 'invalid ban' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
INSERT INTO evolution.account_bans(account_id, banned, author_subject, reason)
|
||||
VALUES (p_account, p_banned, p_subject, coalesce(p_reason, ''));
|
||||
IF p_banned THEN
|
||||
PERFORM evolution.revoke_account_by_id(p_world, p_account);
|
||||
END IF;
|
||||
RETURN p_banned;
|
||||
END $$;
|
||||
REVOKE ALL ON FUNCTION evolution.admin_set_account_ban_v1(evolution.u64, uuid, boolean, text, text)
|
||||
FROM PUBLIC;
|
||||
|
||||
-- Admin: end the account's sessions now (its next login needs a new grant).
|
||||
CREATE FUNCTION evolution.admin_logout_account_v1(p_world evolution.u64, p_account uuid)
|
||||
RETURNS boolean LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
BEGIN
|
||||
IF p_world IS NULL OR p_world = 0 OR p_account IS NULL THEN
|
||||
RAISE EXCEPTION 'invalid account' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
RETURN evolution.revoke_account_by_id(p_world, p_account);
|
||||
END $$;
|
||||
REVOKE ALL ON FUNCTION evolution.admin_logout_account_v1(evolution.u64, uuid) FROM PUBLIC;
|
||||
|
||||
-- Admin: each account's ban state, time played and last session activity in the world.
|
||||
CREATE FUNCTION evolution.admin_account_status_v1(p_world evolution.u64)
|
||||
RETURNS TABLE (account_id uuid, banned boolean, played_seconds bigint, last_seen timestamptz,
|
||||
sessions bigint)
|
||||
LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
WITH accounts AS (
|
||||
SELECT i.account_id FROM evolution.account_identities i
|
||||
UNION SELECT s.account_id FROM evolution.session_intervals s WHERE s.world_id = p_world
|
||||
UNION SELECT b.account_id FROM evolution.account_bans b
|
||||
)
|
||||
SELECT a.account_id, evolution.account_banned(a.account_id),
|
||||
coalesce((SELECT floor(sum(extract(epoch FROM coalesce(s.ended_at, s.last_seen)
|
||||
- s.started_at)))::bigint
|
||||
FROM evolution.session_intervals s
|
||||
WHERE s.world_id = p_world AND s.account_id = a.account_id), 0),
|
||||
(SELECT max(coalesce(s.ended_at, s.last_seen)) FROM evolution.session_intervals s
|
||||
WHERE s.world_id = p_world AND s.account_id = a.account_id),
|
||||
(SELECT count(*) FROM evolution.session_intervals s
|
||||
WHERE s.world_id = p_world AND s.account_id = a.account_id)
|
||||
FROM accounts a
|
||||
LIMIT 10000
|
||||
$$;
|
||||
REVOKE ALL ON FUNCTION evolution.admin_account_status_v1(evolution.u64) FROM PUBLIC;
|
||||
|
||||
-- Admin: one account's ban history, newest first.
|
||||
CREATE FUNCTION evolution.admin_account_bans_v1(p_account uuid)
|
||||
RETURNS TABLE (banned boolean, author_subject text, reason text, created_at timestamptz)
|
||||
LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
SELECT b.banned, b.author_subject, b.reason, b.created_at FROM evolution.account_bans b
|
||||
WHERE b.account_id = p_account ORDER BY b.sequence DESC LIMIT 50
|
||||
$$;
|
||||
REVOKE ALL ON FUNCTION evolution.admin_account_bans_v1(uuid) FROM PUBLIC;
|
||||
|
||||
-- A banned account gets no grant.
|
||||
ALTER FUNCTION evolution.issue_account_grant(evolution.u64, text, text)
|
||||
RENAME TO issue_account_grant_v63;
|
||||
REVOKE ALL ON FUNCTION evolution.issue_account_grant_v63(evolution.u64, text, text)
|
||||
FROM PUBLIC, :"runtime_role";
|
||||
CREATE FUNCTION evolution.issue_account_grant(p_world evolution.u64, p_issuer text,
|
||||
p_subject text)
|
||||
RETURNS TABLE(grant_account uuid, grant_serial evolution.u64, home_region evolution.u64)
|
||||
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
DECLARE known uuid;
|
||||
BEGIN
|
||||
SELECT x.account_id INTO known FROM evolution.account_identities x
|
||||
WHERE x.issuer = p_issuer AND x.subject = p_subject;
|
||||
IF known IS NOT NULL AND evolution.account_banned(known) THEN
|
||||
RAISE EXCEPTION 'account banned' USING ERRCODE = 'EVL20';
|
||||
END IF;
|
||||
RETURN QUERY SELECT * FROM evolution.issue_account_grant_v63(p_world, p_issuer, p_subject);
|
||||
END $$;
|
||||
REVOKE ALL ON FUNCTION evolution.issue_account_grant(evolution.u64, text, text) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION evolution.issue_account_grant(evolution.u64, text, text)
|
||||
TO :"runtime_role";
|
||||
|
||||
-- Session intervals from the control functions (signatures unchanged).
|
||||
ALTER FUNCTION evolution.claim_account_control(evolution.u64, evolution.u64, uuid, evolution.u64,
|
||||
uuid, evolution.u64, evolution.u64, integer) RENAME TO claim_account_control_v63;
|
||||
REVOKE ALL ON FUNCTION evolution.claim_account_control_v63(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid, evolution.u64, evolution.u64, integer) FROM PUBLIC, :"runtime_role";
|
||||
CREATE FUNCTION evolution.claim_account_control(p_world evolution.u64, p_region evolution.u64,
|
||||
p_holder uuid, p_epoch evolution.u64, p_account uuid, p_serial evolution.u64,
|
||||
p_session evolution.u64, p_duration_ms integer)
|
||||
RETURNS TABLE (control_generation evolution.u64, wait_ms integer, granted_ms integer)
|
||||
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
DECLARE r record;
|
||||
BEGIN
|
||||
FOR r IN SELECT * FROM evolution.claim_account_control_v63(p_world, p_region, p_holder,
|
||||
p_epoch, p_account, p_serial, p_session, p_duration_ms) LOOP
|
||||
IF r.granted_ms > 0 THEN
|
||||
INSERT INTO evolution.session_intervals(world_id, account_id, generation, region_id,
|
||||
started_at, last_seen)
|
||||
VALUES (p_world, p_account, r.control_generation, p_region, clock_timestamp(),
|
||||
clock_timestamp())
|
||||
ON CONFLICT (world_id, account_id, generation) DO NOTHING;
|
||||
END IF;
|
||||
control_generation := r.control_generation;
|
||||
wait_ms := r.wait_ms;
|
||||
granted_ms := r.granted_ms;
|
||||
RETURN NEXT;
|
||||
END LOOP;
|
||||
END $$;
|
||||
REVOKE ALL ON FUNCTION evolution.claim_account_control(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid, evolution.u64, evolution.u64, integer) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION evolution.claim_account_control(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid, evolution.u64, evolution.u64, integer) TO :"runtime_role";
|
||||
|
||||
ALTER FUNCTION evolution.renew_account_control(evolution.u64, evolution.u64, uuid, evolution.u64,
|
||||
uuid, evolution.u64, integer) RENAME TO renew_account_control_v63;
|
||||
REVOKE ALL ON FUNCTION evolution.renew_account_control_v63(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid, evolution.u64, integer) FROM PUBLIC, :"runtime_role";
|
||||
CREATE FUNCTION evolution.renew_account_control(p_world evolution.u64, p_region evolution.u64,
|
||||
p_holder uuid, p_epoch evolution.u64, p_account uuid, p_generation evolution.u64,
|
||||
p_duration_ms integer)
|
||||
RETURNS integer LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
DECLARE granted integer;
|
||||
BEGIN
|
||||
granted := evolution.renew_account_control_v63(p_world, p_region, p_holder, p_epoch, p_account,
|
||||
p_generation, p_duration_ms);
|
||||
UPDATE evolution.session_intervals s SET last_seen = clock_timestamp()
|
||||
WHERE s.world_id = p_world AND s.account_id = p_account AND s.generation = p_generation
|
||||
AND s.ended_at IS NULL;
|
||||
RETURN granted;
|
||||
END $$;
|
||||
REVOKE ALL ON FUNCTION evolution.renew_account_control(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid, evolution.u64, integer) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION evolution.renew_account_control(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid, evolution.u64, integer) TO :"runtime_role";
|
||||
|
||||
ALTER FUNCTION evolution.release_account_control(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid, evolution.u64) RENAME TO release_account_control_v63;
|
||||
REVOKE ALL ON FUNCTION evolution.release_account_control_v63(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid, evolution.u64) FROM PUBLIC, :"runtime_role";
|
||||
CREATE FUNCTION evolution.release_account_control(p_world evolution.u64, p_region evolution.u64,
|
||||
p_holder uuid, p_epoch evolution.u64, p_account uuid, p_generation evolution.u64)
|
||||
RETURNS boolean LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
DECLARE released boolean;
|
||||
BEGIN
|
||||
released := evolution.release_account_control_v63(p_world, p_region, p_holder, p_epoch,
|
||||
p_account, p_generation);
|
||||
UPDATE evolution.session_intervals s SET last_seen = clock_timestamp(),
|
||||
ended_at = clock_timestamp()
|
||||
WHERE s.world_id = p_world AND s.account_id = p_account AND s.generation = p_generation
|
||||
AND s.ended_at IS NULL;
|
||||
RETURN released;
|
||||
END $$;
|
||||
REVOKE ALL ON FUNCTION evolution.release_account_control(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid, evolution.u64) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION evolution.release_account_control(evolution.u64, evolution.u64, uuid,
|
||||
evolution.u64, uuid, evolution.u64) TO :"runtime_role";
|
||||
|
||||
INSERT INTO evolution.schema_migrations(version,sha256) VALUES(64,:'migration_sha_64');
|
||||
+22
-4
@@ -139,6 +139,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
|
||||
SELECT :'history_v60' || ',61:' || :'migration_sha_61' AS history_v61 \gset
|
||||
SELECT :'history_v61' || ',62:' || :'migration_sha_62' AS history_v62 \gset
|
||||
SELECT :'history_v62' || ',63:' || :'migration_sha_63' AS history_v63 \gset
|
||||
SELECT :'history_v63' || ',64:' || :'migration_sha_64' AS history_v64 \gset
|
||||
SELECT :'history' = :'history_v22' AS at_v22,
|
||||
:'history' = :'history_v23' AS at_v23,
|
||||
:'history' = :'history_v24' AS at_v24,
|
||||
@@ -180,8 +181,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
|
||||
:'history' = :'history_v60' AS at_v60,
|
||||
:'history' = :'history_v61' AS at_v61,
|
||||
:'history' = :'history_v62' AS at_v62,
|
||||
:'history' = :'history_v63' AS at_v63 \gset
|
||||
\if :at_v63
|
||||
:'history' = :'history_v63' AS at_v63,
|
||||
:'history' = :'history_v64' AS at_v64 \gset
|
||||
\if :at_v64
|
||||
\elif :at_v63
|
||||
\elif :at_v62
|
||||
\elif :at_v61
|
||||
\elif :at_v60
|
||||
@@ -601,7 +604,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
|
||||
coalesce(max(version), 0) = 60 AS at_v60,
|
||||
coalesce(max(version), 0) = 61 AS at_v61,
|
||||
coalesce(max(version), 0) = 62 AS at_v62,
|
||||
coalesce(max(version), 0) = 63 AS at_v63
|
||||
coalesce(max(version), 0) = 63 AS at_v63,
|
||||
coalesce(max(version), 0) = 64 AS at_v64
|
||||
FROM evolution.schema_migrations \gset
|
||||
\if :at_v24
|
||||
\ir 0025_cellular_v2_bundle.sql
|
||||
@@ -833,6 +837,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
|
||||
\elif :at_v61
|
||||
\elif :at_v62
|
||||
\elif :at_v63
|
||||
\elif :at_v64
|
||||
\else
|
||||
\echo 'Unknown or changed migration history after v43'
|
||||
DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$;
|
||||
@@ -917,6 +922,10 @@ SELECT coalesce(max(version), 0) = 62 AS needs_v63 FROM evolution.schema_migrati
|
||||
\if :needs_v63
|
||||
\ir 0063_admin_commands.sql
|
||||
\endif
|
||||
SELECT coalesce(max(version), 0) = 63 AS needs_v64 FROM evolution.schema_migrations \gset
|
||||
\if :needs_v64
|
||||
\ir 0064_account_admin.sql
|
||||
\endif
|
||||
-- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes
|
||||
-- alone are insufficient: predefined roles such as pg_write_all_data, PUBLIC
|
||||
-- grants or SET-able memberships would bypass the fenced functions. Checks the
|
||||
@@ -1065,6 +1074,11 @@ GRANT EXECUTE ON FUNCTION evolution.admin_enqueue_command_v1(evolution.u64, evol
|
||||
evolution.u64, text, text) TO :"admin_role";
|
||||
GRANT EXECUTE ON FUNCTION evolution.admin_commands_v1(evolution.u64, evolution.u64)
|
||||
TO :"admin_role";
|
||||
GRANT EXECUTE ON FUNCTION evolution.admin_set_account_ban_v1(evolution.u64, uuid, boolean, text,
|
||||
text) TO :"admin_role";
|
||||
GRANT EXECUTE ON FUNCTION evolution.admin_logout_account_v1(evolution.u64, uuid) TO :"admin_role";
|
||||
GRANT EXECUTE ON FUNCTION evolution.admin_account_status_v1(evolution.u64) TO :"admin_role";
|
||||
GRANT EXECUTE ON FUNCTION evolution.admin_account_bans_v1(uuid) TO :"admin_role";
|
||||
SELECT pg_catalog.set_config('evolution.admin_role', :'admin_role', true);
|
||||
DO $$
|
||||
DECLARE
|
||||
@@ -1089,7 +1103,11 @@ BEGIN
|
||||
('evolution.admin_session_windows_v1(evolution.u64,evolution.u64)'::regprocedure::oid),
|
||||
('evolution.admin_set_session_window_v1(evolution.u64,evolution.u64,integer,text,text)'::regprocedure::oid),
|
||||
('evolution.admin_enqueue_command_v1(evolution.u64,evolution.u64,integer,integer,evolution.u64,evolution.u64,evolution.u64,evolution.u64,evolution.u64,evolution.u64,text,text)'::regprocedure::oid),
|
||||
('evolution.admin_commands_v1(evolution.u64,evolution.u64)'::regprocedure::oid)
|
||||
('evolution.admin_commands_v1(evolution.u64,evolution.u64)'::regprocedure::oid),
|
||||
('evolution.admin_set_account_ban_v1(evolution.u64,uuid,boolean,text,text)'::regprocedure::oid),
|
||||
('evolution.admin_logout_account_v1(evolution.u64,uuid)'::regprocedure::oid),
|
||||
('evolution.admin_account_status_v1(evolution.u64)'::regprocedure::oid),
|
||||
('evolution.admin_account_bans_v1(uuid)'::regprocedure::oid)
|
||||
), violations(reason) AS (
|
||||
SELECT 'admin role is the runtime role' WHERE admin = runtime
|
||||
UNION ALL
|
||||
|
||||
Reference in New Issue
Block a user