【问题标题】:Multi-permission Firestore security rules多权限 Firestore 安全规则
【发布时间】:2020-02-20 10:35:14
【问题描述】:

我的网站有多个具有不同权限的帐户。

我在 Firestore 令牌中使用自定义声明来授予用户正确的访问权限。

到目前为止,它与此设置完美配合:

有权访问 1 个位置的用户 1 的声明如下所示:{"companyLocation": "testLocation1"}

很快我将拥有可以访问一个或多个位置的用户。例如,用户 2 可以访问“testLocation2”和“testLocation3”,而无需访问“testLocation1”。

例如,用户 2 声明可以有一个分隔符(“¤”),看起来像这样:{"companyLocation": "testLocation2 ¤ testLocation3"}

如何通过安全规则实现这一点?我试过了:

function checkMultipleLocations(){
  return request.auth.token.companyLocation.contains('testLocation2');
}

这给了我一个错误说明:

无效的函数名:包含

在文档中它声明您可以使用in: v in x(检查值 v 是否在列表 x 中),但这不适用于列表(不返回 true),仅适用于对象/地图(尝试通过拆分用户将字符串声明为数组,没有运气)。

有什么想法吗?

【问题讨论】:

标签: google-cloud-firestore firebase-security


【解决方案1】:

in 运算符仅适用于列表。这个声明{"companyLocation": "testLocation2 ¤ testLocation3"} 的值不是一个列表,而是一个字符串。所以in 运算符在这里不起作用。


有关支持的运算符列表,请参阅string type in security rules 的文档。这里没有提到contains 方法,但确实有一个matches 方法,可以让您完成这个用例。

request.auth.token.companyLocation.matches('.*testLocation2.*')

您也可以尝试将声明存储为数组:

{"companyLocation": ["testLocation2", "testLocation3"]}

如果设置这样的声明有效,in 运算符应该有效。我在这里说应该,因为最近有人在设置这样的声明时遇到了麻烦,而我自己还没有测试过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-20
    • 2020-03-22
    • 2018-07-07
    • 2021-02-13
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多