Configure RLS Policy Manager for unit assignments
epic-organizational-hierarchy-management-assignment-aggregation-task-006 — Implement and configure Supabase Row Level Security policies governing access to unit_assignments and organization_units tables. Coordinators see only units within their scope, peer mentors see only their own assignments, national admins see the full tree. Integrate with the assignment repository so all queries automatically apply tenant-scoped filters.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Use PostgreSQL ltree extension for efficient subtree membership checks in coordinator policies — store the materialized path (e.g., 'national.region_oslo.county_akershus.local_baerum') on each organization_units row. The coordinator RLS policy SELECT check becomes: path <@ (SELECT path FROM organization_units WHERE id = coordinator_root_unit_id). Store the coordinator's root unit ID in auth.jwt() under app_metadata.root_unit_id, set during user provisioning. For the national_admin SELECT policy use: (auth.jwt() ->> 'role') = 'national_admin'.
Define separate policies for SELECT, INSERT, UPDATE, DELETE rather than combining them — separate policies are easier to audit and modify independently. All policies go in a single migration file named YYYYMMDDHHMMSS_rls_unit_assignments.sql. Document each policy with a SQL comment explaining the business rule it enforces.
Testing Requirements
RLS policies must be tested with three distinct Supabase test user accounts (one per role) using the supabase_test helper or direct psql sessions with SET ROLE. Test matrix: for each role × operation (SELECT, INSERT, UPDATE, DELETE) × scope (own data, in-scope other, out-of-scope) assert the exact expected result (rows returned or permission denied). Write these as integration tests in the repository's test/integration/ directory, runnable against a local Supabase instance via supabase start. Also add a Dart integration test using flutter_test that boots a real Supabase client with each test user's credentials and asserts the repository methods return only permitted data.
Recursive aggregation queries across four hierarchy levels (national → region → local) with 1,400 leaf nodes may be too slow for real-time dashboard requests, exceeding the 200ms target and causing spinner timeouts.
Mitigation & Contingency
Mitigation: Implement aggregation as a Supabase RPC using a single recursive CTE rather than multiple round-trip queries. Pre-compute aggregations nightly via a scheduled Edge Function and cache results. For real-time needs, aggregate only the immediate subtree on demand.
Contingency: Surface a 'Refreshing...' indicator and serve stale cached aggregations immediately. Queue an async recalculation and push updated data via Supabase Realtime when ready, avoiding blocking the admin dashboard.
The 5-chapter limit and primary-assignment constraint are NHF-specific. Applying these rules globally may break HLF and Blindeforbundet configurations where different limits apply, requiring per-organization configuration that was not initially scoped.
Mitigation & Contingency
Mitigation: Make the maximum assignment count a configurable value stored in the organization's feature-flag or settings table rather than a hardcoded constant. Design the assignment service to read this limit at runtime per organization.
Contingency: Default the limit to a high value (e.g., 100) for organizations other than NHF, effectively making it non-restrictive, while keeping the enforcement logic intact for when per-org configuration is fully implemented.
The searchable parent dropdown in HierarchyNodeEditor must search across up to 1,400 units efficiently. Client-side filtering of the full hierarchy may be slow; server-side search adds complexity and latency.
Mitigation & Contingency
Mitigation: Use the in-memory hierarchy cache as the search corpus — since the cache already holds the flat unit list, client-side filtering with a debounced input is sufficient and avoids extra Supabase calls. Pre-build a search index on cache load.
Contingency: Cap the dropdown to showing the 50 most recently accessed units by default, with a 'search all' option that triggers a server-side full-text query. This keeps the common case fast while supporting edge cases.