英文:
Firebase Rules wildcard Key
问题
我在论坛上搜索了答案,但没有找到一个。
我正在向Firebase数据库写入键值对,并希望设置规则。
这是一个聊天应用程序,我正在为每个他是成员的聊天添加一个字段,例如user/$uid/chat/$chat_id。
我正在使用以下方式创建chatId:
FirebaseDatabase.getInstance().getReference().child("chat").push().getKey();
我正在尝试添加某种验证,以便数据必须包含$chat_id: true。
但是我在尝试使用通配符时遇到了困难。
我写入的测试数据 -
"-M5STgMVWyafV-LhPIT2": "true",但我需要Key作为通配符,因为它每次都是随机的。
数据结构如下。
chat
-M5STgMVWyafV-LhPIT2 // <<< $chat_id
info
admin: "zQvmGG2vPfTrdjZBfWP2ZotajYE2"
groupName: "Some Group"
id: "-MJD6cAtpxY_jmFyk-sb"
no_of_users: 2
timer: 60000
users
y1EKihfmIJTDCD9OJt0N89WDC642: true
zQvmGG2vPfTrdjZBfWP2ZotajYE2: true
messages
-MJ0G0gdecak8ACR7fb2
creator: "wjl8VNmcaPNHJAUQnCDRHWkJ33p2"
date: "Oct 06, 2020"
text: "Some Text"
time: "23:13"
userName: "Bob"
user
zQvmGG2vPfTrdjZBfWP2ZotajYE2
chat
-M5STgMVWyafV-LhPIT2: true // <<< $chat_id
-MGNk6fl8jneDhw7Jv02: true
-MGNkFGD4OYb7ZSzX8P2: true
-MGNl47kbPuNZFc3RLD2: true
-MGNyFIuhjrwyQT4VME2: true
-MGNydtnFHW60i5TbzW2: true
-MJ-ETJ-LcOxvYkZ8b72: true
info
email: "someemailaddress@me.com"
name: "Bob"
notificationKey: "be5df5fa-3a20-4704-986f-8a35d"
profilePic: "https://"
status: "Some status"
zQvmGG2vPfTrdjZBfWP2ZotajYE3
zQvmGG2vPfTrdjZBfWP2ZotajYE4
我尝试过的规则,如果我将$chat_id指定为字符串,这些规则将起作用。
{
"rules": {
"user": {
".read": "auth != null",
".indexOn": "info/email",
"$user_id": {
"chat": {
".write": "auth != null",
".validate": "newData.child($chat_id).val() == 'true'", // <<< 这不起作用 未知变量 '$chat_id'。
//".validate": "newData.child("-M5STgMVWyafV-LhPIT2").val() == 'true'", <<< 这起作用,但不是$chat_id,我需要这个Child成为通配符
},
"info": {
//".indexOn": "email",
".write": "auth != null && $user_id === auth.uid",
},
}
},
"chat": {
//".write": "auth != null && !data.child('chat').child('$chat_id').exists()",
"$chat_id": {
"info": {
".read": "data.child('users').child(auth.uid).exists()",
".write": "auth != null && !data.exists()&&newData.child('admin').val()==auth.uid || data.child('users').child(auth.uid).exists()",
},
"messages": {
"$message_id": {
".write": "root.child('chat').child($chat_id).child('info').child('users').child(auth.uid).exists()",
".read": "root.child('chat').child($chat_id).child('info').child('users').child(auth.uid).exists()",
}
}
}
},
}
}
任何帮助都将不胜感激。
谢谢
英文:
I've searched the forums for an answer to this but can't find one.
I'm writing a Key:value pair to firebase database and want to set rule.
It's a chat application, and I'm adding a field to my user for each chat he's a member of, eg user/$uid/chat/$chat_id
The chatId i'm creating using
FirebaseDatabase.getInstance().getReference().child("chat").push().getKey();
I'm trying to add some kind of validation so that the data has to contains $chat_id : true.
But I'm strugging to get the wildcard to work.
The test data I'm writing -
"-M5STgMVWyafV-LhPIT2": "true", but I need the Key to be a wildcard as it's random each time.
The data structure looks like this.
chat
-M5STgMVWyafV-LhPIT2 // <<< $chat_id
info
admin: "zQvmGG2vPfTrdjZBfWP2ZotajYE2"
groupName: "Some Group"
id: "-MJD6cAtpxY_jmFyk-sb"
no_of_users: 2
timer: 60000
users
y1EKihfmIJTDCD9OJt0N89WDC642: true
zQvmGG2vPfTrdjZBfWP2ZotajYE2: true
messages
-MJ0G0gdecak8ACR7fb2
creator: "wjl8VNmcaPNHJAUQnCDRHWkJ33p2"
date: "Oct 06, 2020"
text: "Some Text"
time: "23:13"
userName: "Bob"
user
zQvmGG2vPfTrdjZBfWP2ZotajYE2
chat
-M5STgMVWyafV-LhPIT2: true // <<< $chat_id
-MGNk6fl8jneDhw7Jv02: true
-MGNkFGD4OYb7ZSzX8P2: true
-MGNl47kbPuNZFc3RLD2: true
-MGNyFIuhjrwyQT4VME2: true
-MGNydtnFHW60i5TbzW2: true
-MJ-ETJ-LcOxvYkZ8b72: true
info
email: "someemailaddress@me.com"
name: "Bob"
notificationKey: "be5df5fa-3a20-4704-986f-8a35d"
profilePic: "https://"
status: "Some status"
zQvmGG2vPfTrdjZBfWP2ZotajYE3
zQvmGG2vPfTrdjZBfWP2ZotajYE4
And the rules I've tried, which work if I specify the $chat_id as a string.
{
"rules": {
"user": {
".read": "auth != null",
".indexOn": "info/email",
"$user_id": {
"chat": {
".write": "auth != null",
".validate": "newData.child($chat_id).val() == 'true'", // <<< this doesn't work Unknown variable '$chat_id'.
//".validate": "newData.child("-M5STgMVWyafV-LhPIT2").val() == 'true'", <<< this works, but not $chat_id, I need this Child to be a wildcard
},
"info": {
//".indexOn": "email",
".write": "auth != null && $user_id === auth.uid",
},
}
},
"chat": {
//".write": "auth != null && !data.child('chat').child('$chat_id').exists()",
"$chat_id": {
"info": {
".read": "data.child('users').child(auth.uid).exists()",
".write": "auth != null && !data.exists()&&newData.child('admin').val()==auth.uid || data.child('users').child(auth.uid).exists()",
},
"messages": {
"$message_id": {
".write": "root.child('chat').child($chat_id).child('info').child('users').child(auth.uid).exists()",
".read": "root.child('chat').child($chat_id).child('info').child('users').child(auth.uid).exists()",
}
}
}
},
}
}
Any help would be apreciated.
Thanks
答案1
得分: 0
这个:
newData.hasChild('$chat_id')
意味着你正在检查新数据是否有一个字面上称为 `$chat_id` 的子节点。除了 `$` 在键中不被允许(因此这是不可能为真的,因为 `$` 不被允许用于键名),这也很可能不是你想要的。
如果你想要检查新数据在你的数据库中是否有一个与 `$chat_id` 相同值的子节点,就直接使用没有引号的 `$chat_id`。所以:
newData.hasChild($chat_id)
英文:
This:
newData.hasChild('$chat_id')
Means that you're checking if the new data has a child node that is literally called $chat_id
. Aside from the fact that this can't be true (as $
is not allowed in keys), it is also likely not what you want.
If you instead want to check if the new data has a child with the same value as $chat_id
in your database, use $chat_id
without quotes. So:
newData.hasChild($chat_id)
答案2
得分: 0
我找到了正确的语法。我需要在写入函数后面添加变量,然后在变量后面放置验证。
以下是这个例子,以防对其他人有帮助。
"$user_id": {
"chat": {
".write": "auth != null",
"$chat_id": {
".validate": "$chat_id.length === 20 && newData.val() === 'member'",
},
},
英文:
I figured out the correct syntax for this. I needed to add the variable after the write function, and then put the validate after the variable.
Here it is in case it helps anyone later.
"$user_id": {
"chat": {
".write": "auth != null",
"$chat_id": {
".validate": "$chat_id.length === 20 && newData.val() === 'member'",
},
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论