# AGENTS.md — Utilities & Access Control > **Parent**: `../../AGENTS.md` — RBAC overview, permission groups, event system. ## Overview 10 files across 3 subdirectories. Core cross-cutting concerns: three-layer RBAC, fire-and-forget event logging, XP resolution. ## Structure ``` utils/ access-control/ 8 files Permission checking, role gates, qualification queries event-log/ 3 files Event emitter, type constants, formatting xp/ 1 file Level resolver ``` ## Access control layers Three independent access mechanisms, each used in different contexts: ### 1. `hasPermission(payload, user, "domain:action")` Full RBAC check against `src/permissions/index.ts` (100+ permissions). Superuser bypass. Cached 30s via `loadUserPermissions`. Used in server actions and collection access functions. ### 2. `isRole(role)` / `hasRoles(roles[])` Lightweight role checks. Used for quick conditional rendering (e.g., `isRole("admin")` for admin-only UI). No Payload call — reads from the user object directly. ### 3. `hasLogisticsQualification()` / `hasIntelligenceQualification()` Queries `Profiles.progression.qualifications` for specific qualification strings (case-insensitive), with domain write permissions as a pass. Admin/developer always pass. Used to gate moderation and management surfaces (wiki moderation, tech tree editing, division admin pages, logistics-only UI). ### 4. `canViewIntelligence()` Read-permission visibility check (`missions:read` / `campaigns:read` / `factions:read` / `technologies:read`, all granted by the standard user role) with the strict intelligence qualification as fallback. Gates the Intelligence sidebar section, command palette, keyboard shortcuts, dashboard widgets, and tour visibility. Never use it for moderation decisions; use `hasIntelligenceQualification()` for those. ### Permission tiers and admin panel gates - `system:admin-access` (registry label "Superuser Tier (user management, notification oversight, final approval states)") is the functional superuser tier: final approval states (`approvalStatusFieldAccess` in `divisionAccess.ts`), the Users admin page, read-all notifications. Held ONLY by the built-in `admin` role and superuser roles. NEVER grant it to division roles; a hand grant once made officers approval-superusers. - Admin panel ENTRY is Users collection `access.admin` and uses `canAccessAdminPanel` (superuser OR any `admin::manage`). Per-page sidebar visibility is `requireAdminPageAccess` (`:read` + `admin::manage`). Two gates, do not collapse them. - Payload 3 field-level access never rejects an update: a `false` return silently strips the field (`node_modules/payload/dist/fields/hooks/beforeValidate/promise.js`) and the save succeeds with the old value. No 403, success toast, other fields save. - Access bugs visible in the admin panel but contradicted by passing API tests: inspect the real role permission rows in the DB first (hand-granted data has diverged from the registry before), then the field-strip semantics. Tests build clean roles, so they cannot catch stray grants. ## Event log system ### Emitter: `emitGameEvent(payload, { type, message, ... })` Fire-and-forget create on `game-event-logs`. Always sets `system: true`. Silent error catch (no throw). Always called AFTER successful mutation in server actions. Stamps `visibility` + `requiredPermissions` from the policy map (see below); callers may override per event. ### Visibility policies: `visibility.ts` `EVENT_VISIBILITY_POLICIES` maps event types to defaults (`public`, or `restricted` with an any-of `requiredPermissions` gate); `resolveEventVisibility(type, override)` merges per-emit overrides with independent fallbacks. Restricted today: `npc:dialogue`, `flag:set`. The `game-event-logs` read access turns this into a Where clause (public OR required-permission OR actor-is-me; staff/superusers see all). Local API consumers must pass `user` + `overrideAccess: false` to respect it. ### Event types: `EventTypes` constants (~105 types across 15+ categories) Defined in `eventTypes.ts`. Use these constants for TypeScript narrowing on the `type` field. Categories include: `mission:*`, `finance:*`, `market:*`, `structure:*`, `staff:*`, `logistics:*`, `bank:*`, `notification:*`, `locker:*`, `contribution:*`, `ticket:*`, `wiki:*`, `minigame:*`, `attendance:*`, `system:*`. ### Display: `formatType(type)` — human-readable label for event types. ## Where to look | Task | Path | |------|------| | Add new RBAC permission | `src/permissions/index.ts` + use in `hasPermission` calls | | Add qualification gate | `access-control/hasLogisticsQualification.ts` (or create similar) | | Add event type constant | `event-log/eventTypes.ts` (add to `EventTypes` object) | | Gate an event type's visibility | `event-log/visibility.ts` (`EVENT_VISIBILITY_POLICIES`) | | Emit event from action | `import { emitGameEvent } from "@/utils/event-log/emit"` | | Check qualification in layout | Use `hasLogisticsQualification(payload, user)` | | Modify XP calculation | `xp/resolveLevel.ts` | ## Anti-patterns - **NEVER** throw in `emitGameEvent` — it's fire-and-forget by design - **NEVER** use `isRole` for permission-sensitive operations — use `hasPermission` instead - **NEVER** hardcode qualification strings — use the constants in the qualification collection - **NEVER** add event types without adding to `EventTypes` constants (breaks TypeScript narrowing)