Firebase 用于在用户之间共享数据的Firestore安全规则

Firebase 用于在用户之间共享数据的Firestore安全规则,firebase,google-cloud-firestore,firebase-security,Firebase,Google Cloud Firestore,Firebase Security,想象一下,我想创建一个应用程序,允许用户管理购物清单。用户应该能够与其他用户共享其购物清单,但未被“邀请”的用户不应该访问此数据 如果我想在不共享数据的情况下实现此功能,我可以非常轻松地保护用户数据: service cloud.firestore { match /databases/{database}/documents { match /users/{userId}/list { allow read, update, delete: if request.aut

想象一下,我想创建一个应用程序,允许用户管理购物清单。用户应该能够与其他用户共享其购物清单,但未被“邀请”的用户不应该访问此数据

如果我想在不共享数据的情况下实现此功能,我可以非常轻松地保护用户数据:

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId}/list {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
但我如何确保“受邀”用户也能够读写这些文档呢

数据将按如下方式存储:

- Collection: Lists
    - Subcollection: Items

列表
文档可能需要跟踪允许哪些用户添加或删除

在子集合中声明受邀请的用户,该子集合如下所示 /用户/{userId}/list/invested/{userId}

在Firestore规则中,您可以检查邀请用户子集合中是否存在具有用户id的文档

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId}/list {
      allow read, write: if request.auth != null && exists(/databases/$(database)/documents/users/$(userId)/list/invited/$(request.auth.uid));
    }
  }
}

您计划如何声明“受邀请”用户?在文档的列表中?作为集合中的文档?我想这取决于安全规则的实现,但我已经更新了我的问题。