【问题标题】:Firebase database rule - Check for initial child valuesFirebase 数据库规则 - 检查初始子值
【发布时间】:2020-12-05 23:20:51
【问题描述】:

一个用户只有在他有正确的孩子和正确的初始值时才应该被创建。

以下代码有效,但它阻止我在创建用户后更改健康值。

如何仅检查初始子值?

{
    "rules": {
        ".read": true,
            ".write": false,
                "users": {
            // Only allow writes in users
            ".write": true,
                "$user": {
                // Check if new user has the correct children
                ".validate": "newData.hasChildren(['userID', 'deviceModel', 'health', 'age', 'playername', 'score', 'clickedby', 'timestamp'])
                    // Check if new user has a health of 10
                    && newData.child('health').val() == '10'
                    // Check if new user has an age of 0
                    && newData.child('age').val() == '0'
                    // Check if new user has a score of 0
                    && newData.child('score').val() == '0'
                    // Check if new user has a timestamp which is not a future value
                    && newData.child('timestamp').val() <= now"
            }
        }
    }
}

更新:Frank van Puffelen 的规则完美运行

这是我更新后的规则:

{
    "rules": {
        ".read": true,
            ".write": false,
                "users": {
            // Only allow writes in users
            ".write": true,
                "$user": {
                // Check if new user has the correct children
                ".validate": "data.exists() || (
                newData.hasChildren(['userID', 'deviceModel', 'health', 'age', 'playername', 'score', 'clickedby', 'timestamp'])
                    // Check if new user has a health of 10
                    && newData.child('health').val() == 10
                    // Check if new user has an age of 0
                    && newData.child('age').val() == 0
                    // Check if new user has a score of 0
                    && newData.child('score').val() == 0
                    // Check if new user has a timestamp which is not a future value
                    && newData.child('timestamp').val() <= now) "
            }
        }
    }
}

【问题讨论】:

    标签: firebase-realtime-database firebase-security


    【解决方案1】:

    如果您只想检查新数据,可以使用data.exist()。例如:

    ".validate": "data.exists() || (
      newData.hasChildren(['userID', 'deviceModel', 'health', 'age', 'playername', 'score', 'clickedby', 'timestamp'])
        // Check if new user has a health of 10
        && newData.child('health').val() == '10'
        // Check if new user has an age of 0
        && newData.child('age').val() == '0'
        // Check if new user has a score of 0
        && newData.child('score').val() == '0'
        // Check if new user has a timestamp which is not a future value
        && newData.child('timestamp').val() <= now
     )"
    

    不相关:我会考虑将数值存储在数字中,而不是像现在这样存储在字符串值中。那就是newData.child('health').val() == 10 等等。

    顺便说一句:在开发早期编写如此漂亮的安全规则值得称赞。 ?

    【讨论】:

    • 非常感谢弗兰克!您的规则完美运行。我唯一要做的就是将最后一个 ) 移到最后一个“像这样:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2017-05-12
    • 2020-10-18
    • 2017-07-15
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    相关资源
    最近更新 更多