【问题标题】:Firebase Security Rules: Allow if user uid exists in a collection as a documentFirebase 安全规则:如果用户 uid 作为文档存在于集合中,则允许
【发布时间】:2021-12-30 08:41:08
【问题描述】:

我正在收集我的管理员用户 uid 作为“管理员”集合中的文档。我想让用户阅读他们的 uid 是否在该集合中作为文档。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /admins/{document=**} {
      allow write; // Everyone should be able to write
      allow read: if request.auth.uid == ; // Only read if /admins/{youruid}
    }
  }
}

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-security


    【解决方案1】:

    您可以使用exists() 来检查文档是否存在。尝试重构您的规则,如下所示:

    match /admins/{document=**} {
      allow read: if exists(/databases/$(database)/documents/admins/$(request.auth.uid)); 
      // Only read if /admins/{youruid}
    }
    

    您可以在documentation 中阅读有关exists() 的更多信息。

    【讨论】:

    • 这在我从匹配部分中删除 /admins/ 时起作用。所以我把它改成匹配 /{document=**} {...}
    • @lowint /admins/{document=**} 表示该规则仅适用于管理员集合及其子集合。您当前的规则适用于所有集合。
    • 我修复了当前规则并添加了另一个规则以满足其他需求。你真的很有帮助。
    猜你喜欢
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多