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,
|
||||
|
||||
Reference in New Issue
Block a user