动态Firestore规则?

时间:2019-03-23 17:04:39

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

动态Firestore规则?

我有2个收藏夹,我控制访问权限,但授予customClaim。现在,如果我有越来越多的收藏规则,则规则将变得更长。

示例

service cloud.firestore {
  match /databases/{database}/documents {
    match /india/{documentID} {
    allow read, write : if request.auth.token.india_admin == true
    allow read : if  true
    }

  }

  match /databases/{database}/documents {
    match /japan/{documentID} {
    allow read, write : if request.auth.token.japan_admin == true
    allow read : if  true
    }

  }

}

有没有一种方法可以使用集合名称变量

对其进行概括

1 个答案:

答案 0 :(得分:2)

您现在正在执行的操作允许无条件读取所有内容。这就是allow read: if true的作用。

使用通配符作为集合名称,试试这个:

match /{country}/{documentID} {
  allow read, write : if request.auth.token[country + "_admin"] == true;
}

请注意,这样做的副作用是将其应用于所有顶级收藏集,即使是不代表某个国家/地区的收藏集。如果您使用其他需要不同规则的顶级集合,则可能需要将所有特定于国家/地区的集合推送到单个顶级集合下的子集合中。