fix(rbac): dedup pending join/upgrade requests per (org, tenant, type)
Plan 3 of #1303 changed the join-request lookup key from per-user to per-tenant (GetPendingRequestByTenantAndType), but didn't touch existing data or add a structural constraint. Result: any (org, tenant) that had multiple pending rows under the old per-user model now has ambiguous "which one wins" semantics — the LIMIT 1 query returns one row, the others stay pending and resurface in admin lists after the first is approved/rejected. Two fixes: 1. Migration 000045 up.sql now collapses pre-existing pending rows to one per (org, tenant, type), keeping the earliest by created_at. The losers are flagged status='rejected' with a '[Plan 3] superseded' review_message so the audit trail shows it was a system cleanup, not a human reject. down.sql drops the new index but leaves the data mutation in place — we don't have enough information to safely re-promote the discarded rows. 2. Add a partial unique index on (organization_id, tenant_id, request_type) WHERE status='pending' to both the PG migration and the SQLite init schema. This makes the invariant structural so SubmitJoinRequest / RequestRoleUpgrade cannot regress. Approved/rejected history is intentionally unconstrained so the audit trail remains complete.
This commit is contained in:
@@ -444,6 +444,11 @@ CREATE TABLE IF NOT EXISTS organization_join_requests (
|
||||
CREATE INDEX IF NOT EXISTS idx_org_join_requests_org_id ON organization_join_requests(organization_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_org_join_requests_user_id ON organization_join_requests(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_org_join_requests_status ON organization_join_requests(status);
|
||||
-- Plan 3 (#1303): at most one pending request per (org, tenant, type).
|
||||
-- Approved/rejected rows are not constrained so the audit trail stays.
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_org_join_requests_pending_per_tenant
|
||||
ON organization_join_requests(organization_id, tenant_id, request_type)
|
||||
WHERE status = 'pending';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS agent_shares (
|
||||
id VARCHAR(36) PRIMARY KEY,
|
||||
|
||||
@@ -24,4 +24,12 @@ ALTER INDEX IF EXISTS idx_org_members_role_pre_plan3 RENAME TO idx_org_
|
||||
-- Drop the new table. The unique/secondary indexes go with it.
|
||||
DROP TABLE IF EXISTS organization_tenant_members;
|
||||
|
||||
-- Drop the partial unique index added in step 5 of up.sql. The data
|
||||
-- mutation (pending → rejected) is intentionally NOT reverted: those
|
||||
-- duplicates were already wrong under Plan 3 semantics, and we don't
|
||||
-- have enough information to safely re-promote which one should win.
|
||||
-- The `[Plan 3] superseded` review_message tag makes the affected
|
||||
-- rows easy to audit if a manual fix is later needed.
|
||||
DROP INDEX IF EXISTS uq_org_join_requests_pending_per_tenant;
|
||||
|
||||
DO $$ BEGIN RAISE NOTICE '[Migration 000045] Plan 3 reverted'; END $$;
|
||||
|
||||
@@ -123,4 +123,41 @@ ALTER INDEX IF EXISTS idx_org_members_user_id RENAME TO idx_org_members_us
|
||||
ALTER INDEX IF EXISTS idx_org_members_tenant_id RENAME TO idx_org_members_tenant_id_pre_plan3;
|
||||
ALTER INDEX IF EXISTS idx_org_members_role RENAME TO idx_org_members_role_pre_plan3;
|
||||
|
||||
-- 5. Dedup pending join/upgrade requests at the (org, tenant, type)
|
||||
-- level. Plan 3 lifts the dedup key from per-user to per-tenant
|
||||
-- (see GetPendingRequestByTenantAndType), so any leftover duplicate
|
||||
-- pending rows from the old per-user model are now ambiguous: one
|
||||
-- row would silently shadow the others and admins would see a
|
||||
-- "ghost" entry remaining in the list after they approved one.
|
||||
--
|
||||
-- Strategy: keep the earliest pending row per (org, tenant, type),
|
||||
-- mark the rest as 'rejected' with a system review_message so the
|
||||
-- audit trail makes it clear this was a Plan 3 cleanup, not a
|
||||
-- human reject. Then add a partial unique index to keep the
|
||||
-- invariant going forward.
|
||||
DO $$ BEGIN RAISE NOTICE '[Migration 000045] Deduping pending join/upgrade requests at (org, tenant, type) level'; END $$;
|
||||
WITH ranked AS (
|
||||
SELECT id,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY organization_id, tenant_id, request_type
|
||||
ORDER BY created_at ASC, id ASC
|
||||
) AS rn
|
||||
FROM organization_join_requests
|
||||
WHERE status = 'pending'
|
||||
)
|
||||
UPDATE organization_join_requests r
|
||||
SET status = 'rejected',
|
||||
review_message = '[Plan 3] superseded: another pending request from the same tenant was kept',
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
FROM ranked
|
||||
WHERE r.id = ranked.id
|
||||
AND ranked.rn > 1;
|
||||
|
||||
-- Partial unique index: only one pending row per (org, tenant, type).
|
||||
-- Postgres lets approved/rejected rows coexist freely so the historical
|
||||
-- audit trail is preserved.
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_org_join_requests_pending_per_tenant
|
||||
ON organization_join_requests (organization_id, tenant_id, request_type)
|
||||
WHERE status = 'pending';
|
||||
|
||||
DO $$ BEGIN RAISE NOTICE '[Migration 000045] Plan 3 setup ready'; END $$;
|
||||
|
||||
Reference in New Issue
Block a user