-- R-P6 · P6 CRM core — RLS ROLLOUT ARTIFACT (deferred, NOT in the auto-migration) -- ============================================================================= -- Companion to R-P2b's prisma/rls/tenant-isolation.sql, AK-P4a's file-isolation.sql -- and AK-P4b's task-isolation.sql. The CRM tables + enums + indexes ship in the -- tracked merge migration prisma/migrations/_p6_crm (forward-only, DoD 28). -- What lives in THIS file is the Row-Level-Security block, DELIBERATELY kept out -- of the auto-migration path (same stance as task/file-isolation.sql): turning RLS -- on before every access sets the GUC would break the feature, so activation is a -- coordinated cutover, not a schema diff. -- -- WHY DEFERRED: enabling RLS + FORCE makes every CRM query WITHOUT `app.tenant_id` -- return ZERO rows. Per R-P2b's rule a table may only be RLS-enabled once ALL its -- accesses set the GUC. The app-level scoping (crmService/crmAccess run every DB op -- inside withTenantRLS()) carries isolation meanwhile (DoD 1 hard path). -- -- CODE-SIDE PRECONDITIONS — DONE (R-P6, 2026-06-29): -- 1. [x] All CRM web services run every DB op inside withTenantRLS() -- (apps/web/app/utils/integrations/crm/*.server.ts). The polymorphic -- custom-field / tag / activity writes are tenant-scoped by construction. -- 2. [ ] Worker maintenance crons (DSGVO archive sweeps) set the tenant GUC per -- tenant — wired when P7/P8 add Lead/Projekt retention (no CRM cron yet). -- -- WHAT REMAINS — "Teil B" = the org-wide P5 RLS cutover (Raffael / R-P2b): -- a. Ship this block as its own forward-only migration. -- b. Switch the runtime DB connection to the restricted role `hivegrid_app` -- (NOSUPERUSER, NOBYPASSRLS); otherwise FORCE is a no-op for a superuser. -- The GRANTs below already cover that role for the CRM tables. -- c. Staging cutover + live cross-tenant 404 verification. -- -- Forward-only (DoD 28). Inverse SQL as comment per block. Order-dependent DDL is -- DO-guarded (P3018 protection: reproducible from an empty DB). Idempotent. -- ============================================================================= -- Row-level security on all tenant-scoped CRM tables -------------------------- -- Pattern: "tenantId" = current_setting('app.tenant_id', true). The second -- argument `true` returns NULL instead of erroring when the GUC is not (yet) -- set — the policy then matches nothing (fail-closed), and server-side scoping -- stays the hard path until the cutover. -- -- Inverse per table: -- REVOKE SELECT, INSERT, UPDATE, DELETE ON "" FROM hivegrid_app; -- DROP POLICY IF EXISTS "_tenant_isolation" ON ""; -- ALTER TABLE "" NO FORCE ROW LEVEL SECURITY; -- ALTER TABLE "" DISABLE ROW LEVEL SECURITY; DO $$ DECLARE tbl text; BEGIN FOREACH tbl IN ARRAY ARRAY[ 'Kontakt', 'KontaktRelation', 'KontaktEmail', 'KontaktTelefon', 'Adresse', 'CustomFieldDefinition', 'CustomFieldValue', 'Tag', 'TagAssignment' ] LOOP EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY;', tbl); -- FORCE so the table owner is subject to the policies too (otherwise the -- owner silently bypasses RLS). EXECUTE format('ALTER TABLE %I FORCE ROW LEVEL SECURITY;', tbl); IF NOT EXISTS ( SELECT 1 FROM pg_policies WHERE schemaname = current_schema() AND tablename = tbl AND policyname = tbl || '_tenant_isolation' ) THEN EXECUTE format( 'CREATE POLICY %I ON %I USING ("tenantId" = current_setting(''app.tenant_id'', true)) WITH CHECK ("tenantId" = current_setting(''app.tenant_id'', true));', tbl || '_tenant_isolation', tbl ); END IF; -- The restricted app role (R-P2b's hivegrid_app, NOBYPASSRLS) needs table -- privileges to reach the rows the policy then filters. EXECUTE format('GRANT SELECT, INSERT, UPDATE, DELETE ON %I TO hivegrid_app;', tbl); END LOOP; END $$; -- ============================================================================= -- Verify green before the cutover migration (AGENTS.md §Prisma): -- pnpm prisma validate -- pnpm prisma migrate status -- CI: `prisma migrate deploy` against an EMPTY DB (reproducible from scratch). -- Coordination: align ordering with R-P2b — P2 ships withTenantRLS() -- (SET LOCAL app.tenant_id); without a tenant context the policies match nothing -- (no data leak, just empty results) until the runtime role is hivegrid_app. -- =============================================================================