【问题标题】:Touble writing firebase write rule for pushing data无法编写用于推送数据的 Firebase 写入规则
【发布时间】:2018-12-29 16:34:24
【问题描述】:

我正在尝试将数据推送到我的数据库,并且我想检查推送数据的所有者 ID 是否与推送它的人的 uid 相同。我得到许可被拒绝。我不知道如何编写推送数据的安全规则,也找不到任何相关信息。数据结构如下所示:

Shops{
    "Shop1PushId" : {
        "ShopCredentials" : {
        "Owner" : "ownerUID"
        }
    }
    "Shop2PushId" : {
    ...
}

这是我正在推动的对象。

{
    "ShopCredentials" : {
        "Owner" : "owner_id",
        "Another" : "another thing"
    }
}

这是我的 Firebase 规则:

"Shops" : {
  ".read" : true,
  ".write" : "newData.child('ShopCredentials').child('Owner').val() === auth.uid"
}

android studio 中的代码:

DatabaseReference shopsRef = database.getReference("Shops");
shopsRef.push().child("ShopCredentials").child("Owner").setValue(shopData.getShopOwner());

【问题讨论】:

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


    【解决方案1】:

    现在您正在对/Shops 本身执行规则。但这是商店的列表。您希望在特定商店而不是列表上执行规则。为此,您需要在规则中添加一个所谓的$ 变量,这表明它下面的规则适用于每个子节点:

    "Shops" : {
      ".read" : true,
      "$shopid": {
        ".write" : "newData.child('ShopCredentials').child('Owner').val() === auth.uid"
      }
    }
    

    现在任何人都可以读取所有商店(即使他们不知道具体的演出 ID),但只有知道商店的具体 ID 并指定自己为新商店的所有者时,才能写入商店。

    我不确定您究竟想在这里保护什么。如果您希望引导该过程,以便人们只能创建以自己为所有者的新商店,请意识到通过这些规则,任何人仍然可以声称拥有他们知道 ID 的任何商店的所有权。如果您想阻止这种情况,并且只想在创建商店时允许声明所有权,请使用以下内容:

    "Shops" : {
      ".read" : true,
      "$shopid": {
        "ShopCredentials": {
          "Owner": {
            ".write" : "!data.exists() && newData.val() === auth.uid"
          }
        }
      }
    }
    

    【讨论】:

    • 是的,醒了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多