在键名上创建 firebase 数据库规则 [英] create firebase database rule on key name

查看:24
本文介绍了在键名上创建 firebase 数据库规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关创建 Firebase 数据库规则的帮助.用例是这样的:用户(玩家)有3个可以装任何宝物的宝箱.用户可以购买额外的宝箱.所以我想出来的firebase数据库中的树结构是:

I need help on creating a firebase database rule. The use case is like this: the user (gamer) have 3 treasure boxes which can hold any treasure. User can purchase extra treasure box. So the tree structure in firebase database I came up is:

user
  |- settings
       |- max-t-box : 3
  |- boxes
       |- 1 : {} // a complicated data for the treasure or null
       |- 2 : {} // a complicated data for the treasure or null
       |- 3 : {} // a complicated data for the treasure or null

'user/settings/max-t-box'的规则很简单:用户只读,不允许写(只能由服务器端管理员修改).

The rule on 'user/settings/max-t-box' is trivial: user read only and not allowed to write (it can only be modified by server side admin).

'user/boxes' 下的规则应该是:新数据的键应该是一个数字,它的值应该是 > 0 和 <= 'user/setting/max-t-box' 值.

The rule under 'user/boxes' is supposed to be: the key of new data should be a number and it's value should be > 0 and <= 'user/setting/max-t-box' value.

根据 firebase 文档,我可以使用 $variable 来捕获路径段,但是它没有为我提供足够的 api 来检查路径节点名称值.

According to firebase document, I can use $variable to capture path segment, however it does not provide enough api for me to check the path node name value.

到目前为止,我提出的解决方案是为路径user/boxes/1"、user/boxes/2"和user/boxes/3"编写规则.然而这在用户购买了很多盒子之后看起来真的很愚蠢.

So far the solution I come up is to write rules for path 'user/boxes/1', 'user/boxes/2' and 'user/boxes/3'. However this really looks stupid after user buys many boxes.

推荐答案

您将遇到使用序列号作为关键字的很多问题.它们(可以)被视为一个数组,并且在 Firebase 数组是邪恶的,一般应该避免.

You are going to run into a lot of issues using sequential numbers as keys. They are (can be) treated like an array and in Firebase Arrays Are Evil and should generally be avoided.

数组不能被修改、搜索,如果你想修改它们,它们必须完全重写.

Arrays cannot be modified, searched and if you want to modify them, they have to be totally re-written.

使用 push() 或 childByAuto() 构建数据来创建密钥是一种可行的方法:

Structuring your data using push() or childByAuto() to create your keys is the way to go:

root
     settings
       max-t-box 3

    boxes
       -yuy8jj09j9090f
         box_num: 1
         box_name: "Big box"
         box_location: "Sewer"
       -y8jokokoais9g
         box_num: 2
         box_name: "Small box"
         box_location: "Tower"

然后规则很简单

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
    "boxes": {
      "$box_num": {
        ".validate": "newData.child('box_num').val() > 0 && 
                      newData.child('box_num').val() <= 
                                        root.child('settings').child('max-t-box').val()" 
      }
    }
  }
}

我直接在根节点中执行此操作,但您可以将 root.child('users') 替换为您的 firebase 结构.

I did this directly within the root node but you can substitute the root.child('users') for your firebase structure.

这篇关于在键名上创建 firebase 数据库规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆