Add RLS policies for coordinator chapter scoping
epic-activity-statistics-dashboard-data-foundation-task-006 — Write and apply Row-Level Security policies on mv_peer_mentor_stats and mv_chapter_stats so that a coordinator can only read rows where chapter_id is within their authorized set (resolved via the coordinator_chapter_memberships join table). Peer mentors may only read their own rows. Service-role bypass must be explicitly locked to the Supabase edge function service key. Policies must be idempotent (CREATE POLICY IF NOT EXISTS). Include regression SQL that verifies cross-chapter reads are blocked.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Implementation Notes
Materialised views in PostgreSQL do not directly support RLS — you must either (a) convert them to views that internally join to a base table with RLS, or (b) apply RLS on a wrapper view that SELECTs from the materialised view with a policy filter. Approach (b) is simpler: create view v_peer_mentor_stats AS SELECT * FROM mv_peer_mentor_stats and enable RLS on the wrapper view. Enable RLS on the materialised view itself in newer PostgreSQL versions (15+) if available in Supabase's managed instance — check the Supabase PostgreSQL version before choosing. Use a SECURITY DEFINER function for the coordinator chapter lookup to avoid N+1 policy evaluations: CREATE OR REPLACE FUNCTION get_coordinator_chapter_ids() RETURNS SETOF uuid SECURITY DEFINER AS $$ SELECT chapter_id FROM coordinator_chapter_memberships WHERE coordinator_id = auth.uid() $$ LANGUAGE sql STABLE.
Index coordinator_chapter_memberships(coordinator_id) before enabling RLS to prevent full-table scans on every row evaluation.
Testing Requirements
SQL regression test script (regression_rls_test.sql): (1) CREATE two test coordinator users with non-overlapping chapter sets using SET LOCAL ROLE / SET LOCAL jwt.claims; (2) SELECT from mv_peer_mentor_stats as each coordinator and assert only their chapters are returned using ASSERT or a pgTAP ok() call; (3) SELECT from mv_chapter_stats as a peer mentor and assert zero rows; (4) SELECT own row from mv_peer_mentor_stats as a peer mentor and assert exactly one row; (5) attempt cross-chapter read as coordinator A for coordinator B's chapter and assert zero rows. Run this script in CI after every migration. Additionally, write a Flutter integration test using Supabase's test client to verify that a real coordinator JWT scopes the PostgREST response correctly.
Materialized views over large activity tables may have refresh latency exceeding the 2-second SLA under high insert load, causing stale data to appear on the dashboard immediately after a peer mentor registers an activity.
Mitigation & Contingency
Mitigation: Design the materialized view refresh trigger to run asynchronously via a Supabase Edge Function rather than a synchronous trigger, and set a maximum staleness tolerance of 5 seconds documented in the feature spec. Add a CONCURRENTLY refresh strategy so reads are never blocked.
Contingency: If refresh latency cannot meet SLA, fall back to a regular (non-materialized) view for the dashboard and accept slightly higher query cost per request. Revisit materialized approach once Supabase pg_cron or background workers are available.
The aggregation counting rules for the dashboard may diverge from those used in the Bufdir export pipeline (e.g., which activity types count, how duplicate registrations are handled), creating a reconciliation burden for coordinators at reporting time.
Mitigation & Contingency
Mitigation: Run the BufDir Alignment Validator against a shared reference dataset before any view is merged to main. Encode the counting rules as a shared Supabase function called by both the stats views and the export query builder so there is a single source of truth.
Contingency: If divergence is discovered post-launch, ship a visible banner on the dashboard stating that numbers are indicative and may differ from the export until the reconciliation fix is deployed. Prioritize the fix as a P0 defect.
Multi-chapter coordinators (up to 5 chapters per NHF requirement) require RLS policies that filter on an array of chapter IDs, which is more complex than single-value RLS and could be misconfigured, leaking data across chapters or blocking legitimate access.
Mitigation & Contingency
Mitigation: Write integration tests that verify cross-chapter isolation for a coordinator assigned to chapters A and B cannot see data from chapter C. Use parameterized RLS policies with auth.uid()-based chapter lookup to avoid hardcoded values.
Contingency: If RLS misconfiguration is detected in testing, temporarily restrict coordinator queries to single-chapter scope (coordinator's primary chapter) and ship multi-chapter support as a fast-follow patch once RLS logic is verified.