【问题标题】:How to configure Firebase security Rules with variable keys如何使用可变键配置 Firebase 安全规则
【发布时间】:2018-03-15 11:14:09
【问题描述】:

我有以下格式的数据

我喜欢这样配置规则

  1. 允许在消息块中创建新项目(使用唯一键)
  2. 不允许删除现有项目(“foo bar”和“hello world”)
  3. 允许更新现有消息的子项(“赞”和“视图”)

我尝试了以下规则

{
  "rules": {
    ".read": true,

    "message": {  
        //allow writing only if the new item does not exist
        ".write": "!data.exists() && newData.exists()"    

        //what write rule do I need for
        //insert data with unique keys 
        //allow update of its child (likes and views)
        //    of existing data
    }
  }
}

但如果消息已经包含项目,它就不起作用。

【问题讨论】:

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


    【解决方案1】:

    要允许创建和更新,您需要:

    ".write": "newData.exists()"
    

    换句话说就是:如果操作后该位置有数据,则允许写入。

    【讨论】:

    • 如何设置规则以防止删除键(“foo bar”和“hello world”),但允许更新子项(查看和喜欢)。
    • 据我所知,我给出的规则允许这样做。如果没有,请使用以下代码更新您的问题:1) 您想要允许的写入,2) 您想要禁止的写入。
    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2020-12-19
    • 2016-06-27
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    相关资源
    最近更新 更多