我想限制我的非高级用户每天只能发表一篇文章。高级用户没有限制。我不清楚如何设置数据库规则。目前我只有经过验证的用户才能发布到数据库。注意,我要写入AddPost节点
我的数据库Json如下所示:
User :
uid:
verified : true
premium : false
我的规则是:
{
"rules": {
"AddPost": {
".read": "auth != null",
".write":
"root.child('Users').child(auth.uid).child('verified').val() == true"
}
,"Users": {
".read": "auth != null",
".write": "auth != null"
}
,"FavoritedBusinesses": {
".read": "auth != null",
".write": "auth != null"
}
,"Geolocs": {
".read": "auth != null",
".write": "auth != null"
}
,"ReviewPost": {
".read": "auth != null",
".write": "auth != null"
}
,"Views": {
".read": "auth != null",
".write": "auth != null"
}
}
}
我的解决方案
所以在弗兰克斯的指导下,我可以把一些规则串在一起。作为一个基本规则,每个用户都必须经过验证,所以我将其作为一个公共分母放在“validate”条件中。。然后在write中,我们首先检查用户是否是高级用户,如果返回false,则检查上次存储在最后一篇文章中的时间戳。但是,如果第一个条件失败,那么这意味着用户是实际的保费,应该允许张贴。。
新数据库规则
".write": "root.child('Users').child(auth.uid).child('premium').val()
=== false && (newData.parent().child('Users').child(auth.uid).child('lastPostStamp').val() === now) && (newData.parent().child('Users').child(auth.uid).child('lastPostStamp').val() > root.child('Users').child(auth.uid).child('lastPostStamp').val() + 24*60*60*1000) || root.child('Users').child(auth.uid).child('premium').val() === true ",
".validate": "root.child('Users').child(auth.uid).child('verified').val() === true"
最佳答案
正如道格所说,这是可能的:
要求每次用户写一篇文章时,他们也将时间戳写到一个已知的位置(比如/Users/$uid/last_post_timestamp
)。
正在验证它们是否只能将ServerValue.TIMESTAMP
写入该位置。
验证他们是否只能在/Users/$uid/last_post_timestamp
中的当前值至少一天前写入新日志。
所以你需要在新职位的规则中遵循以下原则:
(newData.parent().child('Users').child(auth.uid).child('last_post_timestamp').val()
=== now) &&
(newData.parent().child('Users').child(auth.uid).child('last_post_timestamp').val()
> root.child('Users').child(auth.uid).child('last_post_timestamp').val() + 24*60*60*1000)