Context
JwtAuthGuard + @Roles() answer "which platform role is this?" only. M2+ asks "is this user an ACTIVE org ADMIN here?", "is this their attempt?", "are they enrolled?" — with no shared home, every endpoint would re-implement those queries and pick its own 404/403 answer.
Decision
modules/authz, global, no routes. The rules are pure functions (decideOrgAccess,decideOwnership,decideRelation) returning anAccessDecision, combined withallOf/anyOf.AuthzServiceloads the facts (checkOrg,assertOrg,isActiveMember,checkOwner,checkRelation) and owns the pattern every M2 endpoint uses:loadAuthorized('Course', load, decide).@RequireOrgAccess({ roles })checks a route's:orgIdand@CurrentOrgAccess()hands the result over.@Roles()still gates the platform role and stays as it is.- Membership counts only when
status = ACTIVE, the organization isisActive, and the user is live (deletedAt IS NULL,isActive). INVITED, SUSPENDED and LEFT rows grant nothing; M2's invite acceptance reads its row directly. - 404 vs 403. Decide visibility first. A caller who may not see the resource gets exactly what a missing id gets (
404 "<Resource> not found"), so existence never leaks: non-member, inactive member or organization, someone else's private row, malformed id. A caller who can see it but may not act gets403with a client-facing reason. Every rule fails closed to hidden; only a rule that has seen visibility emits forbidden (an ACTIVE member whose org role is short, or a relation given an explicitdenyAs: 'forbidden'). SoallOflets hidden beat forbidden, andanyOfmay let forbidden beat hidden. No newERROR_CODES: each status has one client move (ADR-0003). - Platform ADMIN bypasses org, ownership and relation checks by default — it operates the platform and must reach deactivated organizations. A pass is marked
via: 'platform-admin'/viaPlatformAdminso callers can audit it. Acts that must be the user's own (submit an attempt, enrol) passadminBypass: false. A missing organization is 404 even for ADMIN. - Enrollment (M2) extension point:
AccessRelationRegistry. The owning module registers{ name: 'enrollment.active', holds(userId, courseId) }inonModuleInit; handlers callcheckRelation, which passes it only well-formed UUIDs (a malformed id "does not hold"). Unknown names throw (a bug, not a deny); duplicates fail at boot. Nothing is registered in M1. @RequireOrgAccesson a method replaces, not adds to, the class-level options; the guard runs once per request either way.- Application-layer only today. Postgres RLS is not relied on; whether to make it real is ADR-0029 (item #9).
Alternatives considered
- CASL — expressive, but a dependency and a DSL for four rule shapes.
- 403 everywhere — simpler, but confirms which org and resource ids exist.
Consequences
Tested on mocks and on the real schema (test/integration/authz.int-spec.ts). Each M2 resource writes its decide from these rules; an endpoint that skips loadAuthorized is visible in review.