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

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

问题描述

我需要有关创建Firebase数据库规则的帮助.用例是这样的:用户(游戏者)有3个可以存放任何宝藏的宝藏盒.用户可以购买额外的宝盒.所以我想出的firebase数据库中的树结构是:

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'上的规则很简单:用户只读且不允许写(只能由服务器端管理员修改).

用户/框"下的规则应为:新数据的密钥应为数字,其值应为> 0,且< =用户/设置/最大t-box"值. /p>

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

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

解决方案

使用序号作为键,您将遇到很多问题. (可以)将它们视为数组,并在Firebase中数组是邪恶的,通常应避免使用.

无法修改,搜索数组,如果要修改它们,则必须完全重写它们.

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

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 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

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).

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.

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.

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.

解决方案

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.

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"

and then the rules are a snap

{
  "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()" 
      }
    }
  }
}

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

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

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