【问题标题】:Exists() not working with second variable Cloud Firestore rulesExists() 不适用于第二个变量 Cloud Firestore 规则
【发布时间】:2018-05-17 17:14:08
【问题描述】:

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

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

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

【问题讨论】:

  • 修改函数参数的名称是否有效?例如,function isVV (userseason, usercode)。可能参数名称与match 语句设置的变量冲突。
  • 很遗憾没有。通过为代码输入一个静态值,我确认季节变量正在工作。我又试了一次,但没有雪茄。感谢您的建议!
  • 如果您直接从规则中调用exists(),结果是否相同? -- allow read: if exists(...);
  • 是的,结果相同:/
  • 运气好了吗???

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


【解决方案1】:

由于某种原因,存在不喜欢在 Firestore 规则中评估除数据库之外的变量。查看文档,我发现 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);
}

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

【讨论】:

    猜你喜欢
    • 2019-09-08
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2019-12-14
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多