# Design Full design with approaches and the user's approved calls: `docs/superpowers/specs/2026-11-30-admin-audit-log-design.md`. ## Context - **`AppAccessAspect`** (`solvway-mobile-api/.../useraccess/AppAccessAspect.java`) wraps every controller method. It already: - knows the admin from the JWT; - resolves `@AdminModule` from the method, falling back to the class; - refuses with 403 on a missing module, super admin required or pending reset; - publishes a `ResponseTime` record per call to services-log over Pub/Sub (`response-time-log-topic`). - **services-log** stores in MySQL schema `service-log` with JPA. It creates its topics and subscriptions at startup (`configuration/PubSubConfig.java`), listens in `pubsub/PubSubListener.java`, and archives response times on a schedule (`scheduler/ResponseTimeLogArchiveScheduler.java`). - **Local Pub/Sub:** the emulator creates the topics listed in `local/pubsub/topics.txt`. - **Client IP:** the portal already forwards `X-Client-Ip` on every call (`app/lib/server/mobileApi.ts`). - **babyboo reference:** - core-libs `model/AuditEvent.java`; - mobile-api `useraccess/Audited.java`, `useraccess/AuditChanges.java` and the audit part of `useraccess/AppAccessAspect.java`; - `service/AuditLogService.java` and `controller/AdminAuditLogController.java`; - services-log `controller/AuditEventController.java`, `model/AuditEventLog.java`, `repo/AuditEventLogRepository.java` and `service/AuditEventLogPurge.java`; - portal `app/audit-log/page.tsx` and `app/api/audit-log/**`. ## Goals / Non-Goals **Goals:** - record admin writes, refusals and three sensitive reads; - record allow-listed changes; - delete events after 730 days; - an Audit log screen. **Non-Goals:** - member and public calls; - page views; - sign-ins (covered by `login_audit`); - legacy `/log/save` cleanup. ## Decisions 1. **`AuditEvent` in core-libs:** fields `companyId`, `occurredAt`, `actor`, `module`, `action`, `httpMethod`, `pathTemplate`, `targetId`, `outcome`, `httpStatus`, `clientIp` and `changes` (a JSON string). It is the shared contract, so it lands first. 2. **Table `service-log`.`audit_event`:** - Columns follow the event fields. - `changes` is TEXT. - Indexes: `occurred_at`, `(actor, occurred_at)` and `(module, action, occurred_at)`. 3. **What the aspect records:** - **Writes:** every handler under a class mapped to `/api/admin` whose HTTP method is not GET. - **Sensitive GETs:** handlers annotated `@Audited` (lead export, event-lead export, email detail). - **Skipped:** handlers annotated `@Audited(skip = true)` (event-lead `match`) and every `AdminAuthController` handler except `firstLogin`. - **Actor required:** only calls whose admin token resolved to a user are recorded. A refusal at the access check (403 from the module or super-admin check, or `PASSWORD_RESET_REQUIRED`) for such a user is recorded as `FAILURE`. 4. **Event fields, as the aspect builds them:** - **Action:** `@Audited(action)`; otherwise derived from the handler: drop an `Admin` prefix and a `Controller` suffix, then UPPER_SNAKE both parts and join them, so `AdminUserController.update` becomes `USER_UPDATE`. - **Path template:** the class `@RequestMapping` plus the method mapping, for example `/api/admin/users/{id}`. - **Target id:** the first `@PathVariable` value. - **Outcome:** - `SUCCESS` for a 2xx `ResponseEntity`, or when there is no `ResponseEntity`; - otherwise `FAILURE` with the status, taken from a non-2xx `ResponseEntity` or from a thrown `ResponseStatusException`; - any other exception is `FAILURE` with 500. - **Client IP:** the `X-Client-Ip` header. 5. **`AuditChanges`:** a request-scoped holder. Services call `AuditChanges.put(field, before, after)` or `AuditChanges.fields(names...)`, and the aspect serialises the result into `changes`. The allow-list is enforced where the values are supplied: - `AdminUserService` create and update: `roles` (role names) and `enabled`; - `AdminRoleService` set modules: `modules` with the codes added and removed; - `AdminLeadService` and `AdminEventLeadService` edits: changed field names only; - `AdminLoginSecurityService` config and password rules: the changed settings with before and after. 6. **Publishing:** to `audit-event-topic`, after the handler finishes, through the existing `PubSubUtil` and `PubSubTemplate` pattern used for `ResponseTime`. Any exception is logged at WARN and swallowed. 7. **services-log:** - Creates `audit-event-topic` and `audit-event-subscription` at startup, like the other topics. - The listener saves each `AuditEvent` as an `AuditEventLog`. - A scheduled purge (daily) deletes rows with `occurred_at` older than `audit.retention-days` (env `AUDIT_RETENTION_DAYS`, default 730), in batches. - **`POST /log/audit/search`:** body `{actor, module, action, outcome, from, to, page, size}`. `actor` and `action` match exactly; `from` and `to` are `yyyy-MM-dd` and inclusive. It returns newest first, caps the size at 100, and answers `{content, totalElements, number, size}`. - **`GET /log/audit/{id}`.** - Both need the admin's token at the gateway, like the other `/log/**` routes. 8. **mobile-api read API:** `AdminAuditLogController` (`@AdminModule("BO_AUDIT_LOG")`): - `GET /api/admin/audit-log?actor&module&action&outcome&from&to&page&size`; - `GET /api/admin/audit-log/{id}` (404 when missing). - The reads themselves are not audited (GET without `@Audited`). 9. **Portal `/audit-log`:** - **List:** time, who, module (label from the module's i18n key), action, target, outcome with status, and IP. - **Filters:** who, module select, action, outcome, and From/To `DatePicker`. - **Detail `/audit-log/[id]`:** the event fields, then a changes table (field, before, after), or a list of changed fields when only names were recorded. - **Gating:** `RequireModule` and `ROUTE_MODULES`. - **Proxy:** `app/api/audit-log/[[...path]]/route.ts`, GET only. 10. **Module row:** `(41100000, 'BO_AUDIT_LOG', 'Audit log', 'BO', 'M', 40000000, '/audit-log', 'ScrollText')`, with no role grants. ## Risks / Trade-offs - **Lost events:** Pub/Sub publishing is best-effort, so a broker outage loses events. This is accepted, as in babyboo; the application log still carries the existing `... by ...` lines. - **Role names and settings stored as values:** these are not personal data. Lead personal data is never stored; lead edits keep field names only. - **Path variables as targets:** the first path variable is a UUID or id. The concrete path is never stored, so no query string, and therefore no search text, ends up in the log. - **Volume:** one row per admin write. With one company and a handful of admins, 730 days is small.