Multi-permission Firestore安全规则

问题描述 投票:1回答:1

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

我使用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个回答
0
投票

in运算符仅适用于列表。此声明{"companyLocation": "testLocation2 ¤ testLocation3"}的值不是列表,而是字符串。因此in运算符在这里无法使用。


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

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.