2017-04-07 66 views
1

我试图创建一个允许一些用户编写但不是全部的规则。无法使用规则写入Firebase数据库

我需要所有用户都可以阅读'菜单'项目,但只有在商店数据中列出的用户才能写入。

我的数据结构:

{ 
    "category" : [ null, "Burger", "Drinks" ], 
    "menu" : [ null, { 
     "available" : true, 
     "category" : "1", 
     "description" : "item1 description", 
     "image" : "chicken_maharaja", 
     "name" : "New Chicken Maharaja", 
     "price" : 1300, 
     "store" : 1 
    }, { 
        "available" : true, 
        "category" : "1", 
        "description" : "item2 description", 
        "image" : "big_spicy_chicken_wrap", 
        "name" : "Big Spicy Chicken Wrap", 
        "price" : 120, 
        "store" : 1 
       }, { 
        "available" : true, 
        "category" : "2", 
        "description" : "item3 description", 
        "image" : "thumsup", 
        "name" : "Thumsup 100ml", 
        "price" : 40, 
        "store" : 1 
       }, { 
        "available" : true, 
        "category" : "2", 
        "description" : "item4 description", 
        "image" : "mccafe_ice_coffee", 
        "name" : "Ice Coffee", 
        "price" : 140, 
        "store" : 1 
       }, { 
        "available" : true, 
        "category" : "1", 
        "description" : "item5 description", 
        "image" : "mc_chicken", 
        "name" : "MC Chicken", 
        "price" : 190, 
        "store" : 1 
       }, { 
        "available" : true, 
        "category" : "2", 
        "description" : "item6 description", 
        "image" : "Smoothie", 
        "name" : "Smoothie", 
        "price" : 70, 
        "store" : 2 
       }, { 
        "available" : true, 
        "category" : "1", 
        "description" : "item8 description", 
        "image" : "salad_wrap", 
        "name" : "Salad Wrap", 
        "price" : 150, 
        "store" : 2 
       } ], 
    "stores" : [ null, { 
     "location" : "Campinas - Taquaral", 
     "name" : "Store 1", 
     "user" : { 
      "pyixsRTw9qdiuESt62YnmEYXQt13" : true 
     } 
    }, { 
        "location" : "São Paulo - Perdises", 
        "name" : "Store 2", 
        "user" : { 
         "LBNZ8Dwp2rdJtlSh0NC1ApdtbAl2" : true, 
         "TLomOgrd3gbjDdpDAqGiwl0lBhn2" : true 
        } 
       } ], 
    "userProfile" : { 
     "LBNZ8Dwp2rdJtlSh0NC1ApdtbAl2" : { 
      "birthDate" : "1974-02-10", 
      "email" : "[email protected]", 
      "firstName" : "João", 
      "lastName" : "Silva" 
     }, 
     "pyixsRTw9qdiuESt62YnmEYXQt13" : { 
      "birthDate" : "1974-02-10", 
      "email" : "[email protected]", 
      "firstName" : "Leandro", 
      "lastName" : "Garcia" 
     } 
    } 
} 

我的规则:

{ 
"rules": { 
    "menu": { 
     "$items": { 
      ".read": "true", 
      ".write": "root.child('stores').child('1').child(data.child('user').val()).hasChild(auth.uid)" 
     } 
    }, 
    "stores": { 
     "$store": { 
      ".read": "true", 
      ".write": "root.child('stores').child('$store').child(data.child('user').val()).hasChild(auth.uid)" 
     } 
    } 
} 
} 

读就可以了。 :-)但我不能写。

+0

请编辑您的问题以包含无法编写的代码。硬编码路径和值赞赏(见[创建一个MCVE](http://stackoverflow.com/help/mcve))。 –

+0

嗨,弗兰克。我在Firebase控制台上使用验证工具。我没有写任何代码。 –

+0

很高兴知道。在这种情况下,请添加模拟器的屏幕截图,其中显示了现有的JSON,您写入的路径,您正在编写的数据以及您正在使用的身份验证数据。 –

回答

0

您的newData没有小孩user,因此检查总是失败。你大概的意思:

"43268522": { 
    "menu": { 
    "$items": { 
     ".read": "true", 
     ".write": "root.child('stores').child('1').child('user').hasChild(auth.uid)" 
    } 
    } 

你可能在寻找这样的规则:

".write": " 
    root.child('stores')  
     .child(newData.child('store').val()) 
     .child('user') 
     .hasChild(auth.uid)" 

所以这使用来自新数据store属性来查找当前用户是否是他们”商店重新尝试修改。

不幸的是这个规则不会与您当前的数据结构的工作,因为store的价值是多少,而商店的关键是一个字符串:"1" !== 1

最简单的办法是到店里存储为一个字符串,例如:

"store": "1" 

你可能要考虑的是,无论如何,因为你现在越来越火力地堡的阵列胁迫,这是没有帮助的。欲了解更多信息,请参阅我们的博客文章Best Practices: Arrays in Firebase。我建议使用push IDs来存储商店,或者只是将它们加上前缀,例如

"stores": { 
    "store1": { 
    ... 
    } 
} 
+0

谢谢弗兰克。 –

相关问题