Firestore 规则 - 允许“经理”读取/更新数据

发布于 2025-01-19 12:36:24 字数 500 浏览 2 评论 0 原文

我们在系统中有两个角色:管理者和用户,并希望允许管理者为用户读写数据。 我们为每个用户都有一个集合,保存该用户的报告对象。例如: 集合:用户/GUIDforTheUser/ 在这个集合中,我们保留谁向谁报告(AccountId 代表 userId 的管理者)。

我们还有一个数据集合:data/GUIDforTheUser/,我想允许经理(报告)向向他报告的用户读取和写入数据。我该如何写规则? users/ GUIDforTheUser/ data/ GUIDforTheUser/

We have 2 roles: manager and user in the system and want to allow manager to read and write data for the user.
We have a collection for each user where we keep to whom that user is reporting. For example:
collection: users/GUIDforTheUser/
In this collection, we keep who reports to whom (AccountId represents the mangager of the userId).

We also have a data collection: data/GUIDforTheUser/, and I want to allow manager(report) to read and write the data to the user who reports to him. How would I write the rule?
users/GUIDforTheUser/
data/GUIDforTheUser/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2025-01-26 12:36:24

您可以使用获取/user/{userId} 文档,并在该文档中检查 accountiD 是否匹配用户的UID,试图读取信息:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents { 
    match /data/{userId} {
      allow read: if request.auth.uid == get(/databases/$(database)/documents/users/$(userId)).data.accountId;
    }   
  }
}

You can use get() to fetch /user/{userId} document and check if accountId in that document matches UID of user trying to read the information:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents { 
    match /data/{userId} {
      allow read: if request.auth.uid == get(/databases/$(database)/documents/users/$(userId)).data.accountId;
    }   
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文