【问题标题】:How to allow specific user access to firebase nodes?如何允许特定用户访问 Firebase 节点?
【发布时间】:2015-03-14 14:53:07
【问题描述】:

我想允许所有登录用户访问以创建新内容,但只有拥有者才能更新数据。我不知道该怎么做,这是我尝试过的:

{
  "rules": {
    ".read": false,
    ".write": false,
    "videos": {
      ".read": true,
      ".indexOn": ["id", "title_lower_case"],
      "$video": {
        ".write": "(auth !== null && auth.provider === 'password' && (!newData.exists() || newData.hasChildren())) || (auth.uid === root.child('videos').child($video).child('uid').val())",
        ".validate": "newData.hasChildren(['id', 'title', 'title_lower_case', 'uid'])",
        "id": {
          ".validate": "newData.isString() && newData.val().length >= 5 && newData.val().length <= 1000"
        },
        "title": {
          ".validate": "newData.isString() && newData.val().length >= 2 && newData.val().length <= 1000"
        },
        "title_lower_case": {
          ".validate": "newData.isString() && newData.val().length >= 2 && newData.val().length <= 1000"
        },
        "uid": {
          ".validate": "newData.val() === auth.uid"
        },
        "$other": {
          ".validate": false
        }
      }
    },
    "users": {
      ".read": true,
      "$user": {
        ".read": "auth !== null && auth.provider === 'password'",
        ".write": "auth.uid === $user && (!newData.exists() || newData.hasChildren())",
        ".indexOn": "name_lower_case",
        ".validate": "newData.hasChildren(['email', 'name', 'name_lower_case'])",
        "email": {
          ".validate": "newData.isString() && newData.val().length <= 2000"
        },
        "name": {
          ".validate": "newData.isString() && newData.val().length >= 2 && newData.val().length <= 2000"
        },
        "name_lower_case": {
          ".validate": "newData.isString() && newData.val().length >= 2 && newData.val().length <= 2000"
        },
        "$other": {
          ".validate": false
        }
      }
    }
  }
}

我认为这部分允许任何登录用户创建一个新节点:

auth !== null && auth.provider === 'password' && (!newData.exists() || newData.hasChildren())

而且这部分只允许所有者编辑节点:

auth.uid === root.child('videos').child($video).child('uid').val()

顺便说一句,如果有什么要说的,我会使用 firebase 中的简单登录功能。

【问题讨论】:

    标签: firebase firebase-security


    【解决方案1】:

    执行以下操作似乎可以解决问题。如果 auth 变量存在,则允许写入能力。由于我没有完全理解这些规则,我不得不怀疑这是否针对当前用户运行,或者它是否正在检查是否有任何用户登录。

    {
        "rules": {
          "product":{
            ".read": true,
            ".write":"auth !== null"
          }
        }
    }
    

    【讨论】:

    • 但这只会检查您是否登录,而不是检查您是否是“视频”的所有者,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多