fix(devices): read device users from user_list, mirror writes to device_users

The Manage tab and Onboarding page list users via GET /devices/{id}/users,
which read the device_users sub-collection first and only fell back to
user_list when it was empty. Every write (add/remove on those pages, the
Users page, PUT with user_list) went to user_list only, so on any device
that had a device_users doc the list showed stale data: users added in the
Console never appeared and removed users never went away. A production
survey found 6 device_users docs (all role "admin") on 6 devices; 2 of them
disagree with user_list.

user_list is now the single source of truth:
- get_device_users reads only user_list; role comes from the mirror if any.
- Every user_list write also adds/removes the device_users mirror doc
  ({role: "admin", user_reference}) in the same atomic batch, in case the
  phone app still reads the sub-collection. It is meant to go away later.
- users.service.assign_device and PUT /devices/{id} now store
  DocumentReferences (the app's format) instead of "users/{id}" strings.

scripts/sync_device_users_mirror.py (dry run by default) reports and fixes
pre-existing drift between the two. Not run yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-07 10:37:21 +02:00
co-authored by Claude Opus 5.5
parent fba5110cc0
commit 2a6f9697b8
7 changed files with 330 additions and 142 deletions
+11 -5
View File
@@ -201,11 +201,17 @@ When those lines stop appearing, set `MQTT_ALLOW_LEGACY_PASSWORD=false`.
Firestore (e.g. a claim flow), it must also update `device_serials` the same way
(ArrayUnion / ArrayRemove of the serial on the user doc). Otherwise that user
cannot reach the device over MQTT. The app owner will handle this in the app.
- **The `device_users` subcollection** on devices (older way of recording device users,
read first by `devices.service.get_device_users`) is **intentionally ignored** by
the backfill and the MQTT ACL. Left as is by decision.
- The two Console assignment paths store `user_list` entries in different formats
(path strings vs DocumentReferences). This predates this work; all readers accept both.
- **The `device_users` subcollection** on devices (`{role, user_reference}`, the older
way of recording device users) is a **write-only mirror** since 2026-11. `user_list`
is the only source of truth: the Console reads assignments only from it
(`devices.service.get_device_users`, which takes just the `role` from the mirror).
Every Console path that edits `user_list` also adds/removes the mirror doc in the
same batch (`devices.service.stage_device_users_mirror`). The MQTT ACL and the
backfill ignore it. It is kept only in case the phone app still reads it, and is
meant to be removed. `scripts/sync_device_users_mirror.py` (dry run by default)
fixes drift from before 2026-11.
- All Console paths now write `user_list` entries as DocumentReferences (what the app
writes). Older data may still hold `"users/{id}"` path strings; all readers accept both.
- ACL denies are logged at INFO, which is not shown by default. Auth denies and
legacy-password logins are WARNING.