Firebase安全规则遍历数据库以进行验证

时间:2016-12-13 17:43:10

标签: firebase firebase-realtime-database firebase-security angularfire2

尝试以下验证规则时遇到问题:

            ".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分钟内未提交其他帖子。

我在StackOverflow上看过这个post,但我试图看看是否可以通过我的实现来做到这一点。

$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"
            }

目前,当我提交帖子时,两个时间戳都是相同的,但我可以不间断地发布越来越多的帖子。

1 个答案:

答案 0 :(得分:2)

如果您想在两分钟内阻止其他帖子,则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

在规则中使用此功能可以防止使用任意的未来时间戳。 (如果你没有使用ServerValue.TIMESTAMP,你应该考虑使用它。)

相关问题