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:
+17
-28
@@ -236,29 +236,23 @@ def assign_device(user_doc_id: str, device_doc_id: str) -> UserInDB:
|
||||
if not device_doc.exists:
|
||||
raise NotFoundError("Device")
|
||||
|
||||
# Add user path to device's user_list if not already there
|
||||
from devices import service as devices_service
|
||||
|
||||
# Add a user reference to device's user_list if not already there.
|
||||
# Stored as a DocumentReference, the format the FlutterFlow app writes.
|
||||
device_data = device_doc.to_dict()
|
||||
user_list = device_data.get("user_list", [])
|
||||
user_path = f"users/{user_doc_id}"
|
||||
user_list = device_data.get("user_list", []) or []
|
||||
already_assigned = any(devices_service.user_list_entry_id(e) == user_doc_id for e in user_list)
|
||||
|
||||
# Check if already assigned (handle both string paths and DocumentReferences)
|
||||
already_assigned = False
|
||||
for entry in user_list:
|
||||
if isinstance(entry, DocumentReference):
|
||||
if entry.path == user_path:
|
||||
already_assigned = True
|
||||
break
|
||||
elif entry == user_path:
|
||||
already_assigned = True
|
||||
break
|
||||
|
||||
# Device user_list and the user's device_serials are written in one atomic batch.
|
||||
# device_serials is re-synced even when already assigned, to heal drift.
|
||||
# Device user_list, the user's device_serials and the device_users mirror
|
||||
# are written in one atomic batch. device_serials and the mirror are
|
||||
# re-synced even when already assigned, to heal drift.
|
||||
batch = db.batch()
|
||||
if not already_assigned:
|
||||
user_list.append(user_path)
|
||||
user_list.append(user_ref)
|
||||
batch.update(device_ref, {"user_list": user_list})
|
||||
stage_device_serial_link(batch, user_ref, device_serial_of(device_data), linked=True)
|
||||
devices_service.stage_device_users_mirror(batch, device_ref, user_ref, linked=True)
|
||||
batch.commit()
|
||||
invalidate_mqtt_acl_cache(user_doc.to_dict())
|
||||
|
||||
@@ -281,22 +275,17 @@ def unassign_device(user_doc_id: str, device_doc_id: str) -> UserInDB:
|
||||
if not device_doc.exists:
|
||||
raise NotFoundError("Device")
|
||||
|
||||
# Remove user from device's user_list
|
||||
device_data = device_doc.to_dict()
|
||||
user_list = device_data.get("user_list", [])
|
||||
user_path = f"users/{user_doc_id}"
|
||||
from devices import service as devices_service
|
||||
|
||||
new_list = []
|
||||
for entry in user_list:
|
||||
if isinstance(entry, DocumentReference):
|
||||
if entry.path != user_path:
|
||||
new_list.append(entry)
|
||||
elif entry != user_path:
|
||||
new_list.append(entry)
|
||||
# Remove user from device's user_list (any entry format)
|
||||
device_data = device_doc.to_dict()
|
||||
user_list = device_data.get("user_list", []) or []
|
||||
new_list = [e for e in user_list if devices_service.user_list_entry_id(e) != user_doc_id]
|
||||
|
||||
batch = db.batch()
|
||||
batch.update(device_ref, {"user_list": new_list})
|
||||
stage_device_serial_link(batch, user_ref, device_serial_of(device_data), linked=False)
|
||||
devices_service.stage_device_users_mirror(batch, device_ref, user_ref, linked=False)
|
||||
batch.commit()
|
||||
invalidate_mqtt_acl_cache(user_doc.to_dict())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user