Add RLS policy validation for stats views
epic-coordinator-statistics-dashboard-foundation-task-008 — Verify that Supabase Row Level Security policies on the pre-aggregated views enforce org_id and coordinator_id boundaries at the database level. Document the dual-layer security model (RLS + RoleAccessValidator). Write SQL tests that confirm coordinators cannot read peer data outside their chapter even with direct Supabase client access.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
RLS policy pattern for coordinator stats view: CREATE POLICY coordinator_own_stats ON coordinator_stats_view FOR SELECT TO authenticated USING (org_id = (auth.jwt()->'app_metadata'->>'org_id')::uuid AND (coordinator_id = auth.uid() OR (auth.jwt()->'app_metadata'->>'role') = 'org_admin')). For org-admin: the role check in the USING clause is sufficient. Document the dual-layer model clearly: 'RLS is the authoritative enforcement layer at the database level. RoleAccessValidator in the application layer provides early rejection and clear error messages before the network call, improving UX and reducing unnecessary Supabase round trips.
Both layers must be maintained independently.' Store documentation in a migration comment and a SECURITY.md section.
Testing Requirements
Write SQL test scripts (usable with psql or Supabase's built-in SQL editor) that: (1) set LOCAL ROLE to a coordinator JWT and SELECT from each stats view — verify row count matches only that coordinator's data; (2) set LOCAL ROLE to another coordinator in the same org — verify zero rows from first coordinator's data; (3) set LOCAL ROLE to org-admin — verify all org rows returned; (4) set LOCAL ROLE to anon — verify zero rows. Document the test script in the migration file. Additionally write a Dart integration test that performs the same checks via the Supabase Flutter SDK.
Pre-aggregated Supabase views may still be slow for orgs with very large activity datasets (NHF with 1,400 chapters). If the view query plan performs sequential scans, dashboard load times could exceed acceptable thresholds and degrade the perceived value of the feature.
Mitigation & Contingency
Mitigation: Design views with composite indexes on (org_id, coordinator_id, month) from the start. Run EXPLAIN ANALYZE during development against a seeded dataset of realistic scale. Add materialized view refresh strategy if needed.
Contingency: If live view performance is insufficient, convert to materialized views refreshed on a schedule or on activity-write triggers. Expose the refresh delay transparently in the UI with a 'last updated' timestamp.
Supabase RLS policies for the stats views may not be configured correctly during initial migration, potentially allowing cross-coordinator data leakage before the RoleAccessValidator layer is reached. This is a security and compliance risk.
Mitigation & Contingency
Mitigation: Write RLS integration tests as part of this epic that explicitly verify a coordinator JWT cannot read another coordinator's stats rows. Apply RLS policies in the migration script itself, not as a manual step.
Contingency: If an RLS gap is discovered post-deployment, immediately disable the stats screen via a feature flag, apply the corrected RLS migration, and re-enable after verification. Log and audit all queries that ran during the gap window.
Cache invalidation logic may not be triggered correctly when a new activity is registered by a peer mentor or when an expense approval is granted. Stale data could cause coordinators to make decisions based on outdated KPIs, undermining trust in the dashboard.
Mitigation & Contingency
Mitigation: Define explicit invalidation event contracts with the activity registration and expense approval pipelines. Implement an event bus subscription within StatsCacheManager. Document the invalidation contract in code.
Contingency: If event-driven invalidation proves unreliable, add a manual 'Refresh' pull-to-refresh gesture on the dashboard and reduce TTL to 5 minutes as a fallback degradation strategy.