如何将安全规则应用于特定集合

时间:2018-09-06 16:28:35

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

我对安全规则有疑问。

我知道我们可以这样做,以防止未经授权的用户修改Firestore中的任何节点

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

如果我只想从某些根集合中删除此预防措施,该怎么办?

我的意思是说我有两个称为跟踪,传入的根集合

任何人都可以对此进行写入或读取,因为实际上它们不需要身份验证。但是所有其他集合仅需要经过身份验证的用户才能进行读/写。

我该如何实现?

1 个答案:

答案 0 :(得分:1)

只需叫出他们并授予访问权限即可。最宽松的规则将覆盖所有其他规则。在这里,每个人都可以完全访问集合中称为all-access的文档:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
    match /all-access/{id} {
      allow read, write: if true;
    }
  }
}

但是您可能要考虑这是否真的是一个好主意。任何人都可以使用这些规则将数十亿个文档塞入馆藏。仔细考虑一下,您希望每个人都可以在这里做什么。