2016-09-25 89 views
0

我使用的是Firebase。這裏是我的一個節點看起來像:Firebase在數據庫中確保節點的安全

--Users 
----RandomUID1 
----RandomUID2 
----RandomUID3 

對於規則,這就是我:

--"users" : { 

----".read": "auth != null && !root.child('blockedUsers').hasChild(auth.uid)", 

----".write": "auth != null && !root.child('blockedUsers').hasChild(auth.uid)" 

--} 

我想是從users節點讀取將保持不變,但我想寫作只允許users節點的孩子。這樣做的原因是,如果我不小心編寫了一個晚上刪除整個users節點的代碼,那將是災難性的。是否有可能制定規則,以便不允許在節點上寫入,但允許寫入子節點?

有沒有辦法讓自己免受可能因人爲錯誤而發生的事故?

回答

2

可以使用變量,Firebase variable rules

.AT用戶節點我沒加一個寫入規則,即不允許修改數據。

"users":{ 
     ".read": "auth != null && !root.child('blockedUsers').hasChild(auth.uid)", 
     "$uid": { //where $uid is child 
     ".read": "auth!==null && auth.uid == $uid", 
     ".write": ""auth!==null && auth.uid === $uid" 
     } 
    } 
+0

謝謝!如果我在用戶節點上寫入「寫入」:「false」會怎麼樣? –

+0

這裏沒有什麼區別,因爲默認值是'false'。但爲了清晰起見,您可能需要添加它,然後在較低級別上指定更寬鬆的規則。需要記住的一件重要事情是逆向是不可能的。權限是繼承的:因此,一旦您在一個級別上指定了「true」,您就不能在較低級別上獲得該權限。 –

+0

很好的答案btw @Fowotade –