【问题标题】:Firebase security rules traversing through the database for validationFirebase 安全规则遍历数据库进行验证
【发布时间】:2017-04-28 21:14:47
【问题描述】:

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

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

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

【问题讨论】:

  • 你为什么删除your previous post并添加一个新的?如果您有详细信息要添加,请下次将它们添加到您的原始问题中。
  • 你可以用root.child('users/'+auth.uid+'/lastPostAt')代替data.parent().parent().parent().parent().parent().child('users/'+auth.uid+'/lastPostAt')

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


【解决方案1】:

如果您想在两分钟内阻止其他帖子,newData.val() 至少需要比lastPostAt 大 120,000 毫秒

所以你的规则应该是:

"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,你应该考虑使用它。)

【讨论】:

    猜你喜欢
    • 2016-09-16
    • 1970-01-01
    • 2020-06-23
    • 2019-01-20
    • 1970-01-01
    • 2020-08-08
    • 2018-12-15
    • 1970-01-01
    相关资源
    最近更新 更多