由于您的管理进程将使用秘密令牌登录,因此安全规则将不适用。因此,您可以使用以下方法简单地保护客户端访问:
// not applied to privileged server logging in with token
".write": false,
或者,如果您希望客户增加金额,您可以使用以下技巧,它只允许他们增加计数器,并且只允许他们在计数器已更新时添加注释。(参见工作演示http://jsfiddle.net/katowulf/5ESSp/)
{
"rules": {
".read": true,
".write": false,
"incid": {
"counter": {
// this counter is set using a transaction and can only be incremented by 1
".write": "newData.isNumber() && ((!data.exists() && newData.val() === 1) || newData.val() === data.val()+1)"
},
"records": {
"$id": {
// this rule allows adds but no deletes or updates
// the id must inherently be in the format rec# where # is the current value of incid/counter
// thus, to add a record, you first create a transaction to update the counter, and then use that counter here
// the value must be a string less than 1000 characters
".write": "$id >= 'rec'+root.child('incid/counter').val() && !data.exists() && newData.isString() && newData.val().length <= 1000"
}
}
}
}
}