存在()不使用第二个变量Cloud Firestore规则

时间:2018-05-17 17:14:08

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

在我的项目中,我想通过检查用户的uid是否是该文档的子集合(包含该文档的所有成员)的一部分来控制对文档的访问。 当我想用exists()方法检查它时,它不会在它应该的时候授予权限。

 match /events/{season}/events/{code} {
    function isVV (season, code) {
      return exists(/databases/$(database)/documents/events/$(season)/events/$(code)/vv/$(request.auth.uid));
    }

    allow read: if isVV(season, code);

当我用我正在测试的值替换$(代码)时,规则通过并且一切都按预期工作。当我使用变量时它没有。

任何可以帮助我的Cloud Firestore规则专家? 也许有更好的方法来做到这一点?

1 个答案:

答案 0 :(得分:0)

由于某些原因,不喜欢在Firestore规则中评估数据库以外的变量。查看文档,我发现存在功能实际上包含Path,如下所述:https://firebase.google.com/docs/reference/rules/rules.Path

使用此信息,我可以通过首先使用串联将路径构造为字符串,然后将其传递给exists函数,从而实现与您想要的相似的功能。

对于上面的示例,它看起来像:

match /events/{season}/events/{code} {
  function isVV (database, season, code) {
    return exists(path("/databases/" + database + "/documents/events/" + season + "/events/" + code + "/vv/" + request.auth.uid));
  }

  allow read: if isVV(database, season, code);
}

注意:您必须将数据库作为函数参数传递,因为我发现以这种方式使用字符串连接时,它不会像使用exists(/databases/$(database))

那样自动填充