Implement RLS policies for badge tables
epic-achievement-badges-foundation-task-002 — Write and apply Row Level Security policies for all four badge-related tables (badge_definitions, earned_badges, recognition_tiers, tier_assignments). Policies must enforce organisation-scoped access so that volunteers and coordinators can only read/write records belonging to their own organisation. Superadmin bypass role must be documented and tested. Include both SELECT and INSERT/UPDATE/DELETE policies.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Use Supabase's `auth.jwt()` function to extract custom claims: `(auth.jwt() ->> 'organisation_id')::uuid` for organisation scoping and `auth.jwt() ->> 'role'` for role checks. Define policies in a separate migration file from the schema (e.g., `supabase migration new badge_rls_policies`) to keep schema and security concerns separated. For the superadmin bypass pattern, use `auth.jwt() ->> 'is_superadmin' = 'true'` as an OR clause in every policy. Document the expected JWT payload shape at the top of the migration file.
Be careful with `USING` vs `WITH CHECK`: `USING` controls which rows are visible for SELECT/UPDATE/DELETE, `WITH CHECK` controls which rows can be written. Both are needed for INSERT and UPDATE policies. Test with `set_config('request.jwt.claims', ...)` in pgTAP to simulate different user contexts.
Testing Requirements
Write Supabase RLS tests using the `supabase/tests` pgTAP framework or a dedicated test Dart file that exercises the Supabase client with different user JWT tokens. Required test scenarios: (1) volunteer from Org A can read own earned badges, (2) volunteer from Org A cannot read earned badges of another user in Org A, (3) volunteer from Org A gets empty result when querying Org B data, (4) coordinator can read all earned badges in their org, (5) coordinator INSERT succeeds for own org, fails for other org, (6) superadmin reads across all orgs successfully, (7) attempt to UPDATE organisation_id on earned_badge is blocked. Each scenario must have a passing assertion. Run these as part of CI against a local Supabase instance.
Badge criteria are stored as structured JSON in badge_definitions. If the JSON schema for criteria (threshold counts, streak lengths, training completion flags) is not well-defined upfront, the evaluation service will be built against a moving target, requiring costly migrations and refactors.
Mitigation & Contingency
Mitigation: Define and document the criteria JSON schema in a shared type file before any repository code is written. Review the schema with all three organisations' badge requirements — especially Blindeforbundet's honorar thresholds — and version the JSON schema using a 'criteria_version' field from day one.
Contingency: If the criteria schema must change after services are built, write a Supabase migration to backfill existing rows and add a migration version column. Keep the evaluation service criteria parser isolated behind an interface so only one function needs updating.
Badge icon assets may not yet exist or may fail WCAG 2.2 AA contrast validation (minimum 3:1 for graphical objects) when rendered over design-token backgrounds. Missing or non-compliant icons could block UI epic delivery for Blindeforbundet, for whom screen reader and visual accessibility is non-negotiable.
Mitigation & Contingency
Mitigation: During this epic, implement the contrast-ratio validator in badge-icon-asset-manager and run it as a Flutter test against all candidate icon assets early. Coordinate with the design team to provide WCAG-compliant SVG icons in both locked and unlocked variants before the UI epic begins.
Contingency: If assets are late or fail contrast checks, ship placeholder icons that are guaranteed compliant (solid design-token colour fills with text labels) and swap in final assets post-QA without requiring a code change.