Enforce coordinator and admin role access checks
epic-bufdir-report-history-services-task-002 — Integrate the permission system into ReportHistoryService so that only users with coordinator or organization admin roles can access report history. Add role validation logic at the service boundary, returning an access-denied error for unauthorized roles, and ensure the check is applied to all public service methods.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Implement role checking as a private guard method: `void _requireHistoryAccess() { final role = _authRepository.currentRole; if (role == null) throw AuthenticationException(); if (!_allowedRoles.contains(role)) throw AccessDeniedException('Report history requires coordinator or admin role'); }`. Call `_requireHistoryAccess()` as the first line of every public method. Define `static const _allowedRoles = {UserRole.coordinator, UserRole.orgAdmin}` at the top of the class. This single-point-of-definition makes it trivial to audit and extend.
Align `UserRole` enum values with those defined elsewhere in the project — check `lib/core/enums/` or equivalent for the canonical definition. Do not create a duplicate enum. If the project uses string-based roles from Supabase JWT claims, map them to the enum at the repository/auth layer boundary, so the service always works with typed enums.
Testing Requirements
Unit tests using Mockito mocks. Test matrix (role × method): peer_mentor × fetchHistory → AccessDeniedException, peer_mentor × any other public method → AccessDeniedException, coordinator × fetchHistory → success (delegates to repository mock), org_admin × fetchHistory → success. Additional tests: unauthenticated → AuthenticationException; repository mock is never invoked when role is unauthorized (`verifyNever(mockRepository.fetchPage(...))`). Implement a 'guard coverage' test that uses reflection or an explicit list to verify every public method name in `ReportHistoryService` appears in the test file's authorized-call test cases — flags new methods added without test coverage.
The ReportReexportCoordinator must invoke the Bufdir export pipeline defined in the bufdir-report-export feature. If that feature's internal API changes (renamed services, altered parameters), the re-export coordinator will break silently at runtime.
Mitigation & Contingency
Mitigation: Define a stable, versioned interface (abstract class or Dart interface) for the export pipeline entry point. The re-export coordinator depends only on this interface, not on concrete export service internals. Document the contract in both features.
Contingency: If the export pipeline breaks the re-export coordinator, fall back to surfacing a clear 'regeneration unavailable' message to the coordinator with instructions to use the primary export screen for the same period as a workaround, while the interface mismatch is fixed.
The audit trail must be immutable — coordinators must not be able to edit or delete past events. If the RLS policies allow UPDATE or DELETE on audit event rows, a coordinator could suppress evidence of a re-export or failed submission.
Mitigation & Contingency
Mitigation: Apply INSERT-only RLS policies to the audit events table (no UPDATE, no DELETE for any non-service-role user). Use a separate service-role key for writing audit events, never the user's JWT. Validate this in integration tests by asserting that UPDATE and DELETE calls from coordinator-role sessions are rejected with RLS errors.
Contingency: If immutability is compromised before detection, run a database audit comparing the audit log against the main history table timestamps to identify tampered records, restore from backup if needed, and issue a patch RLS migration immediately.
The user stories require filter state (year, period type, status) to persist within a session so coordinators do not lose context when navigating away. Implementing this with Riverpod state management could cause stale filter state if the provider is not properly scoped to the session lifecycle.
Mitigation & Contingency
Mitigation: Scope the filter state provider to the router's history route scope, not globally. Use autoDispose with a keepAlive flag tied to the session so filters reset on logout but persist on tab switches within the same session.
Contingency: If filter state becomes stale or leaks between sessions, add an explicit reset in the logout handler that disposes all scoped providers. This is a UX degradation (coordinator must re-apply filters) rather than a data integrity issue.