# Admin Audit Log — Design Date: 2026-11-30. Change id: `add-admin-audit-log`. ## Intent Nothing in Co-Lite records who did what in the admin portal. When an admin edits a user, changes a role's grants, edits or exports leads, or opens a saved email (which can hold a temporary password), the only trace is a line in the application log. Sign-ins are the exception: the `login_audit` table covers them (`add-admin-login-security`). The user wants an audit trail before go-live (2027-01-15) and a screen to read it. Scope was agreed on 2026-11-30, with lead edits recording field names only, not values. Reference: babyboo's `add-portal-audit-log` (archive `2026-11-19`) and `audit-log-readable-actions` (`2026-11-20`). This design ports the admin half of that work, trimmed. ## Goals - Record an audit event for every admin write through mobile-api, and for chosen sensitive reads. - Record refused attempts (a missing module grant, for example) when the token is valid, so the actor is known. - Store before/after values only for an allow-list of fields. - Provide an `/audit-log` screen in the admin portal with filters and a detail view, behind its own module. - Delete events after 2 years, automatically. ## Non-goals - Member and public calls. The customer portal's public lead capture has no actor. - Sign-in, sign-out and own password change. `login_audit` already records these; the audit log neither duplicates nor links to them. - Page views and ordinary reads (the existing response-time log covers traffic). - Reviving or cleaning up the legacy `back_office_access_log`, `/log/save` and similar. They are unused; removing them belongs in a separate change. ## Anti-goals - Any password, token, raw request body, or lead personal data (name, phone, email, notes) in an audit row. - An audit write that slows down or fails the admin's action. - Audit rows that anyone without `BO_AUDIT_LOG` can read. ## Approaches considered - **A. Pub/Sub from mobile-api's `AppAccessAspect` to services-log (chosen).** The aspect already sees every admin call (the user, its `@AdminModule` code, the outcome) and already publishes a `ResponseTime` record per call to services-log over Pub/Sub. One more message type on the same path is the smallest change. It is asynchronous, and losing an event is acceptable. - **B. A synchronous REST call from mobile-api to services-log, like `login_audit`.** Simpler to test, but it adds a network hop to every admin write, and it needs its own error handling. - **C. Explicit calls inside each service method.** Easy to forget on new endpoints. Rejected: the aspect covers new endpoints by default. ## Design ### Event table `service-log`.`audit_event` | Column | Meaning | |---|---| | `id` | PK | | `company_id` | the actor's company (nullable) | | `occurred_at` | server time of the call | | `actor` | admin username | | `module` | the handler's `@AdminModule` code (for example `BO_USERS`) | | `action` | readable action code (see below) | | `http_method`, `path_template` | for example `PUT /api/admin/users/{id}`: the template, never the concrete path or the query | | `target_id` | the record acted on: the first path variable, when there is one | | `outcome`, `http_status` | `SUCCESS` or `FAILURE`, plus the status code | | `client_ip` | forwarded browser IP (nullable) | | `changes` | JSON, nullable: `{field: {before, after}}` for allow-listed fields, or `{fields: [..]}` for lead edits | Indexes: `occurred_at`, `(actor, occurred_at)`, `(module, action, occurred_at)`. The DDL and the module row go in the change's `sql/` folder and in `local/seed/03-colite.sql`. ### What is recorded - **Every non-GET handler under `/api/admin/**`,** except: - `AdminAuthController`'s login, logout, refresh, forgot-password and change-password. Sign-in history covers these, or they have no actor. - `first-login` is recorded. - POSTs that only read, marked with `@Audited(skip = true)`. Today that is event-lead `match`. - **Sensitive GETs, marked with `@Audited`:** - the lead export; - the event-lead export; - the email detail (opening an email). - **Refusals:** a valid admin token refused by the access check (missing module, not super admin, pending first login) is recorded as `FAILURE` with the status. Calls without a valid token are not recorded, because there is no actor. ### Action codes `@Audited(action = "...")` names an action explicitly. Without one, the code is derived from the handler: drop an `Admin` prefix and a `Controller` suffix, then convert both parts to UPPER_SNAKE. For example, `AdminUserController.update` becomes `USER_UPDATE`. Explicit names are used where the derived one would be unclear: - `USER_SET_TEMPORARY_PASSWORD`; - `USER_SEND_PASSWORD_RESET`; - `ROLE_SET_MODULES`; - `LEAD_EXPORT`; - `EVENT_LEAD_EXPORT`; - `EMAIL_VIEW`. ### Before/after (allow-listed) The handler's service supplies the values through a small request-scoped holder (babyboo's `AuditChanges`). Nothing is diffed generically from request bodies. - **Admin user update and create:** `roles` (role names) and `enabled`. - **Role module grants:** module codes added and removed. - **Lead edit and event-lead edit:** the names of the fields that changed, no values. This was the user's call. - **Login-security settings** (lockout and password rules): the changed setting names with before and after. These are not personal data. ### Write path - After the handler returns or throws, `AppAccessAspect` builds an `AuditEvent` and publishes it to the new Pub/Sub topic `audit-event`. The `AuditEvent` message class lives in core-libs, shared by mobile-api and services-log. - services-log subscribes on `audit-event-log`, which it creates at startup like its existing subscriptions, and saves the event. - A publish failure is logged at WARN and swallowed. The existing response-time publishing is unchanged. ### Client IP - The portal's server-side calls to mobile-api already send `X-Client-Ip` on every request (`app/lib/server/mobileApi.ts`, the last `x-forwarded-for` hop), so the portal needs no change. - mobile-api stores the value for reading only. ### Retention A scheduled purge in services-log deletes events older than `audit.retention-days` (default 730) in batches, modelled on the existing response-time archive scheduler. The setting is an environment variable. ### Reading - services-log gets `POST /log/audit/search`. It takes filters (actor, module, action, outcome, date range) and paging, returns newest first, and caps the page size at 100. - services-log also gets `GET /log/audit/{id}`. - Both are reached through the gateway with the admin's token, like the other services. Only mobile-api holds a gateway key (`docs/deployment.md` section 7). - mobile-api adds `GET /api/admin/audit-log` and `GET /api/admin/audit-log/{id}` under `@AdminModule("BO_AUDIT_LOG")`. ### Admin portal - **`/audit-log`:** a paged list (newest first) with columns time, who, module, action, target, outcome and IP. - **Filters:** who, module (select), action, outcome and From/To date. - **Detail page:** the event, plus a before/after table, or a list of changed field names for lead edits. - **Copy and gating:** all copy goes through `t()`. The page is gated by `RequireModule` and `ROUTE_MODULES`. - **Module row:** `(41100000, 'BO_AUDIT_LOG', 'Audit log', 'BO', 'M', 40000000, '/audit-log', 'ScrollText')`. It has no role grants, so only super admins see it until it is granted. ## My calls (check these) - Pub/Sub, asynchronous, best-effort. A lost event is acceptable, as in babyboo. - Reads go through the gateway with the admin's token. babyboo switched to in-cluster calls because other api-key holders existed; Co-Lite has only mobile-api. - `first-login` is recorded and the other auth endpoints are not. - The login-security settings record values, since they are not personal data. User changes record role names and the enabled flag only. - Retention is 730 days, set by environment variable rather than on a screen. - Legacy `/log/save` and the old access-log tables are left alone. ## Left to the builder - The exact JSON shape of `changes`, as long as the detail view can render it. - Column widths, so the list fits at 1280 px without horizontal scrolling (babyboo hit this).