[英]Firebase Realtime Database Rules checking for array contains
我的授权令牌包括一个stores
属性。 此属性是一个数组,其中包含允许用户访问的所有商店的 ID。 例如: stores: ['abc123', 'def456']
。
现在,在我的 Firebase 实时数据库中,我正在保存特定于商店的数据,如下所示:
{
"stores": {
"abc123": {
"data": "mydata"
},
"def456": {
"data": "mydata"
}
}
}
如果用户的stores
属性数组包含他们想要访问的 storeID 特定数据,则允许用户访问实时数据库数据。
我希望我的规则看起来像:
{
"rules": {
"stores": {
"$store_id": {
".read": "auth.token.stores.includes($store_id)"
}
}
}
}
这是不可能的。 我收到以下错误:
Error saving rules - line 5: type error: function call on target that is not a function.
是否可以通过 firebase 规则中的令牌属性 arrays 进行搜索,或者我是否必须使用 object? 感谢您的任何回答!
Firebase 实时数据库没有任何includes()
来检查数组中是否存在值。 您可以将声明存储为 map 而不是数组,如下所示:
// custom claims
{
stores: {
store1: true,
store2: true,
store3: false
}
}
然后您可以使用以下规则来检查用户是否可以访问特定商店:
{
"rules": {
"stores": {
"$store_id": {
".read": "auth.token.stores[$store_id] == true"
}
}
}
}
您可以使用List in
运算符
v 在 x
检查值 v 是否存在于列表 x 中。
'a' in ['a','b'] == true
示例安全规则如何完成
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
match /stores/{storeId} {
allow read: if isAuthenticated() && isUserAllowed();
}
function isAuthenticated() {
return request.auth != null;
}
function isUserAllowed() {
return request.auth.uid in resource.data.stores
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.