【问题标题】:Firebase write permission deniedFirebase 写入权限被拒绝
【发布时间】:2016-11-15 05:29:29
【问题描述】:

我不明白为什么 firebase 不让我写这篇具体的文章。我觉得我在模拟器中做同样的事情,它在那里工作。以下是我对本节的安全规则。

"joinRequests" : {
  "$clanid": {
    "$requesterid" : {
      ".read" : "$requesterid === auth.uid || root.child('clans/' + $clanid + '/members/' + auth.uid + '/admin').val() === true",
      ".validate": "newData.hasChildren(['request'])",
      "request" : {
        ".write" : "true",
        ".validate": "newData.hasChildren(['name', 'message'])",
        "name" : {
        ".validate": "newData.isString()"
        },
        "message" : {
          ".validate": "newData.isString()"
        },
        "$other": {
        ".validate": false
        }
      },
      "approved" : {
        ".write" : "root.child('clans/' + $clanid + '/members/' + auth.uid + '/admin').val() === true || ($requesterid === auth.uid  && !newData.exists())",
        ".validate": "newData.isBoolean()"
      },
      "$other": {
      ".validate": false
      }
    }
  }
},

在我的 android 应用程序的代码中,我运行以下两行:

dataSnapshot.child("request").getRef().removeValue();
dataSnapshot.child("approved").getRef().removeValue();

我觉得奇怪的是,它允许我删除“已批准”值,但不能删除“请求”值。 dataSnapshot 是一个 $requesterid。如果我在模拟器中运行这一行并且它允许写入:

/joinRequests/QV28VJYG/c1cef959-2dd3-4cab-8649-2b81892cffa6/request

我在 android studio 中遇到的错误是这样的:

W/RepoOperation: setValue at /joinRequests/QV28VJYG/qRlJt4UIAcRVIe9VXoYVBa68tO43/request failed: DatabaseError: Permission denied

我在做的事情一定很愚蠢,但我无法想象它会是什么。任何帮助都会很棒,谢谢。

【问题讨论】:

  • 你能在请求中没有这个验证规则的情况下试试吗:“.validate”:“newData.hasChildren(['name', 'message'])”,?
  • 所以问题出在这一行 ".validate": "newData.hasChildren(['request'])" 因为我的要求我不能把允许写入 $ requesterid 所以我试图只删除孩子,而父母中的这条规则不允许它。奇怪的是模拟器不会捕捉到它。
  • 呜呜呜。是的,看起来模拟器 UI 期待已排序的行消息,并且当 API 返回未排序的行消息数组时无法突出显示某些行。我会修复它。感谢您的报告!

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


【解决方案1】:

newData 是一个快照,表示操作发生后数据的样子。

newData.hasChildren(['request']) 告诉 firebase 在执行操作后确保 request 节点存在。因此,禁止删除该节点。

【讨论】:

    猜你喜欢
    • 2016-08-27
    • 2020-02-15
    • 2017-12-26
    • 1970-01-01
    • 2020-07-31
    相关资源
    最近更新 更多