【发布时间】:2020-12-05 23:20:51
【问题描述】:
一个用户只有在他有正确的孩子和正确的初始值时才应该被创建。
以下代码有效,但它阻止我在创建用户后更改健康值。
如何仅检查初始子值?
{
"rules": {
".read": true,
".write": false,
"users": {
// Only allow writes in users
".write": true,
"$user": {
// Check if new user has the correct children
".validate": "newData.hasChildren(['userID', 'deviceModel', 'health', 'age', 'playername', 'score', 'clickedby', 'timestamp'])
// Check if new user has a health of 10
&& newData.child('health').val() == '10'
// Check if new user has an age of 0
&& newData.child('age').val() == '0'
// Check if new user has a score of 0
&& newData.child('score').val() == '0'
// Check if new user has a timestamp which is not a future value
&& newData.child('timestamp').val() <= now"
}
}
}
}
更新:Frank van Puffelen 的规则完美运行
这是我更新后的规则:
{
"rules": {
".read": true,
".write": false,
"users": {
// Only allow writes in users
".write": true,
"$user": {
// Check if new user has the correct children
".validate": "data.exists() || (
newData.hasChildren(['userID', 'deviceModel', 'health', 'age', 'playername', 'score', 'clickedby', 'timestamp'])
// Check if new user has a health of 10
&& newData.child('health').val() == 10
// Check if new user has an age of 0
&& newData.child('age').val() == 0
// Check if new user has a score of 0
&& newData.child('score').val() == 0
// Check if new user has a timestamp which is not a future value
&& newData.child('timestamp').val() <= now) "
}
}
}
}
【问题讨论】:
标签: firebase-realtime-database firebase-security