【问题标题】:Firebase database rules: how to make field readonly?Firebase 数据库规则:如何使字段只读?
【发布时间】:2018-08-21 03:23:53
【问题描述】:

我想利用用户在我的数据库中创建一些 JSON 数据的能力,但限制将来编辑它的能力。我应该如何编写我的规则?

"users": {
    // ".write": "auth != null",
    "$userUid": {
        ".read": "auth != null && $userUid === auth.uid",
    }
}

【问题讨论】:

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


    【解决方案1】:

    要将某些内容设为只读,只需不授予其写入权限即可。

    要使某些东西只创建,只有在没有现有数据的情况下才允许编写它:

    ".write": "!data.exists()"
    

    然后还要让用户只能在自己的节点下写:

    "$uid": {
      ".write": "!data.exists() && auth.uid === $uid"
    }
    

    要进行创建或删除,允许在没有数据时写入值,在有数据时也不写入值:

    ".write": "!data.exists() || !newData.exists()"
    

    【讨论】:

      猜你喜欢
      • 2020-02-28
      • 1970-01-01
      • 2018-06-22
      • 2017-07-15
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      相关资源
      最近更新 更多