Firestore 安全规则:获取字符串形式的路径

tou*_*ody 5 firebase firebase-security google-cloud-firestore

使用 Firestore 安全规则(版本 2),如何将 Path 对象转换为字符串?整个路径作为一个字符串,而不是各个片段。

我正在尝试编写一个通用函数以在各种 Match 语句中使用。像这样的东西:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // "pathobj" is a rules.firestore.Path object
    function getPathAsString(pathobj) {
      // Does not work:
      return pathobj.toString();
    }

    // I would expect the following to evaluate to True:
    match /foo/{fooid} {
      allow read: if getPathAsString(path('one')) == 'one'
                  && getPathAsString(path('one/!two')) == 'one/!two';
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

有谁知道这是否可能?我阅读了Path 上的文档,并在Firestore Rules Simulator中尝试了各种方法,但都没有成功。

小智 3

火力战士在这里

由于这个问题,我们最近添加了对将路径转换为字符串的支持。

string()现在,您可以使用其他类型(null、float、int、bool)已经存在的强制转换方法通过强制转换路径来执行比较,即:

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {

    // Evaluates to true for the foo/fooid document:
    match /foo/{fooid} {
      allow read: if string(path('one')) == '/one'
                  && string(request.path) == '/databases/(default)/documents/foo/fooid';
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,有效路径始终以/. 如果您遇到此功能的任何问题,请发表评论。