如何根据指定的Array字段查询数据,但在Firestore中的安全规则中约束

时间:2018-04-02 09:53:09

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

例如,我制定了以下规则(有一个名为电子邮件的字段)

service cloud.firestore {
  match /databases/{database}/documents {
    match /collection/{documentId} {
      allow read: if request.auth.token.email in resource.data.email;
      //resource.data.email here is an Array!!!
    }
  }
}

但是如何通过where查询获取任何数据,我的意思是这样:

 db.collection("store").where("id", "==", "1").onSnapshot(function (querySnapshot) {
                querySnapshot.forEach(doc => {
                    console.log("realtime---->", doc.data().cities);
                })
 })

权限问题仍然存在...... documentation表明我应该在email字段中包含与上面定义的规则相同的约束。如何在Array中处理它?<​​/ p>

1 个答案:

答案 0 :(得分:0)

好吧,我终于发现,由于官方文档,查询数组是不可能https://firebase.google.com/docs/firestore/solutions/arrays已经说过了。 你可以做的是将{ hash1:1, hash2:0 } 作为一个对象,比如

//here I set the hash as user uid, and query through carId
allow read,write:if get(/databases/$(database)/documents/car/$(resource.data.carId)).data.authfield[request.auth.uid]=="1"

键值对可以表示具有不同权限级别的不同用户。    制定如下规则:

db.collection("col1").where("carId","==","ABC").where("authfield.hash1","==","1")

然后,通过

查询
hash1

顺便说一句,where字段不能设置为电子邮件格式或其他格式,因为它只识别第一个 dot 作为对象及其属性之间的分隔符号。

例如,对于名为“dou@company.com”的字段,如果将其放在{{1}}查询中,它会将其视为对象“dou @ company”,并读取其属性“ COM”。