Exists() 不适用于第二个变量 Cloud Firestore 规则

Vin*_*der 1 firebase firebase-security google-cloud-firestore

在我的项目中,我想通过检查用户的 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);
Run Code Online (Sandbox Code Playgroud)

当我用我当前正在测试的值替换 $(code) 时,规则通过,一切都按预期工作。当我使用变量时,它不会。

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

dan*_*znz 5

由于某种原因,在 Firestore 规则中,exists 不喜欢评估除数据库之外的变量。查看文档,我发现 exists 函数实际上采用了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);
}
Run Code Online (Sandbox Code Playgroud)

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