我在尝试以下验证规则时遇到问题:
".validate": "newData.val() >= now - 2000 && newData.val() >= data.parent().parent().parent().parent().parent().child('users/'+auth.uid+'/lastPostAt').val() - 2000"
以下是我的 Firebase 数据库架构的相关区域:
areas
-> area1
-> posts
-> uid
-> post object ( has timestamp property (date) - firebase timestamp)
user
-> uid
-> user object ( has timestamp property (lastPostAt) - Firebase timestamp)
提交帖子后,它会更新提交帖子的特定用户的用户对象中的 lastPostAt
时间戳属性。
我正在寻找一些 Firebase 规则,以确保在提交新帖子时不会存储该帖子,除非用户在过去 2 分钟内未提交其他帖子。
我看过这个post在 StackOverflow 上,但我正在尝试看看是否可以通过我的实现来做到这一点。
在我的帖子节点的 $uid
下,我正在尝试 .validate
规则:
"posts" : {
"$message_id": {
".indexOn": "username",
".write": "newData.hasChildren(['date'])",
"date" : {
".validate": "newData.val() >= now - 2000 && newData.val() >= data.parent().parent().parent().parent().parent().child('users/'+auth.uid+'/lastPostAt').val() - 2000"
}
目前,当我提交帖子时,两个时间戳是相同的,但我可以毫不延迟地继续发布越来越多的帖子。
最佳答案
如果您想在两分钟内阻止其他帖子,newData.val()
需要至少 120,000 毫秒大于lastPostAt
.
所以你的规则应该是:
"date" : {
".validate": "newData.val() >= now - 2000 && newData.val() >= data.parent().parent().parent().parent().parent().child('users/'+auth.uid+'/lastPostAt').val() + 120000"
}
此外,如果您使用 ServerValue.TIMESTAMP
,您可以使用:
newData.val() === now
在规则中使用它可以防止使用任意的 future 时间戳。 (如果您没有使用ServerValue.TIMESTAMP
,您应该考虑使用它。)
关于Firebase 安全规则遍历数据库进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41127388/