动态Firestore规则?

问题描述 投票:0回答:1

动态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
    }

  }

}

有没有办法可以通过使用集合名称变量来概括它

firebase google-cloud-firestore firebase-security-rules
1个回答
2
投票

您现在正在做的事情允许无条件地读取所有内容。这就是allow read: if true所做的。

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.