Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0028

Authorization layer beyond the platform Role

Trạng thái
accepted
Ngày
2026-09-29
Người quyết định
SmartLearn team (architecture review 2026-09, item #8)

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 an AccessDecision, combined with allOf / anyOf. AuthzService loads 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 :orgId and @CurrentOrgAccess() hands the result over. @Roles() still gates the platform role and stays as it is.
  • Membership counts only when status = ACTIVE, the organization is isActive, 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 gets 403 with 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 explicit denyAs: 'forbidden'). So allOf lets hidden beat forbidden, and anyOf may let forbidden beat hidden. No new ERROR_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' / viaPlatformAdmin so callers can audit it. Acts that must be the user's own (submit an attempt, enrol) pass adminBypass: false. A missing organization is 404 even for ADMIN.
  • Enrollment (M2) extension point: AccessRelationRegistry. The owning module registers { name: 'enrollment.active', holds(userId, courseId) } in onModuleInit; handlers call checkRelation, 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.
  • @RequireOrgAccess on 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.