Implement RLS policies for contact_chapters
epic-multi-chapter-membership-handling-foundation-task-002 — Define and apply Row Level Security policies on the contact_chapters table to enforce multi-organisation data isolation. Policies must restrict read and write access so that coordinators can only query memberships within their own organisation scope, and peer mentors can only read their own chapter affiliations. Validate policies against the existing RLS pattern used elsewhere in the Supabase project.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Follow the existing RLS pattern in the project — inspect other tables (e.g., `activities`, `contacts`) to understand how `auth.jwt() ->> 'organisation_id'` is used and replicate the same helper function if one exists (e.g., `get_user_organisation_id()`). For the peer mentor SELECT policy, a subquery join through `contacts` table is needed: `contact_id IN (SELECT id FROM contacts WHERE user_id = auth.uid())`. Ensure this subquery is covered by an index on `contacts.user_id`. Avoid `SECURITY DEFINER` functions in policies unless already established as a pattern — they can bypass RLS on the called function's tables.
Test with at least two organisations seeded to confirm cross-org isolation.
Testing Requirements
Write SQL test scripts using `SET LOCAL role = authenticated; SET LOCAL request.jwt.claims = ...` to simulate coordinator and peer mentor JWTs. Assert: (1) coordinator sees only their organisation's memberships; (2) peer mentor sees only their own rows; (3) unauthenticated request returns 0 rows; (4) coordinator from Org A cannot insert a membership for a chapter in Org B; (5) peer mentor cannot delete any row. Run these assertions in the Supabase SQL editor or as a `pgTAP` test suite. Include test results in the PR as a screenshot or exported test output.
Supabase RLS policies for a junction table that spans organisations are non-trivial. An incorrectly scoped policy could expose chapter affiliations from other organisations to coordinators, constituting a data breach.
Mitigation & Contingency
Mitigation: Draft RLS policies in a staging environment and run an explicit cross-organisation isolation test suite before merging. Use Supabase policy testing tools and peer review all policy definitions.
Contingency: If a policy error reaches review, roll back the migration and apply a corrective patch. Ensure no production data has been exposed by auditing Supabase logs for cross-organisation query results.
The contact_chapters table migration may conflict with existing foreign key structures or require a backfill for contacts already assigned to a single chapter, causing migration failures in production.
Mitigation & Contingency
Mitigation: Write the migration as an additive, non-destructive operation. Backfill existing single-chapter assignments by deriving them from the existing contact records. Test the full migration on a production-sized dataset clone before release.
Contingency: Provide a rollback migration script that removes the new table without touching existing contact records. Coordinate with operations for a maintenance window if a re-run is needed.