0
我想創建一個firebase規則,允許用戶讀取和寫入子項,如果該子項的屬性具有特定的值。比方說,我的數據庫看起來是這樣的:Firebase安全規則允許有條件讀取
"tasks": {
"SDkh7s62jnd23d9": {
"uid": "someuserid",
"other": "datagoes here"
}
}
這裏是我當前的安全規則:
"tasks": {
".indexOn": "_state",
"$registerQueueKey": {
".read": "data.child('uid').val() == auth.uid",
".write": "newData.child('uid').val() == auth.uid"
}
}
此規則限制用戶寫入權限,但它永遠不會讓用戶讀取,即使孩子他們正在嘗試讀取的uid
屬性等於auth.id
。
然後我測試了以下規則集:
"tasks": {
".indexOn": "_state",
"$registerQueueKey": {
".read": true,
".write": "newData.child('uid').val() == auth.uid"
}
}
儘管.read
權限被設置爲永久true
值,用戶仍然無法讀取$registerQueueKey
孩子。
然後我測試了以下規則集:
"tasks": {
".indexOn": "_state",
".read": true,
"$registerQueueKey": {
".write": "newData.child('uid').val() == auth.uid"
}
}
現在我可以閱讀的孩子很好,所以我嘗試這最後的安全規則:
"tasks": {
".indexOn": "_state",
".read": "data.child($registerQueueKey").child('uid').val() == auth.uid",
"$registerQueueKey": {
".write": "newData.child('uid').val() == auth.uid"
}
}
但是這個規則拋出,因爲一個錯誤變量$registerQueueKey
未在其使用範圍內定義。
如何完成這樣的規則?
火力地堡檢查讀取授權你在哪裏附加監聽器的位置。因此,無論您是否可以從該位置讀取(並從而讀取其下的所有內容),或者無法從該位置讀取數據。這意味着您無法使用安全規則來過濾您在此處嘗試的內容。這被稱爲[規則不是過濾器](https://firebase.google.com/docs/database/security/securing-data#rules_are_not_filters)在文檔中,在[這個答案](http://stackoverflow.com/a/14298525/209103)和[這些其他問題](http://stackoverflow.com/search?q=%5Bfirebase%5D+rules+are+not+filters)。 –
@FrankvanPuffelen所以我需要重構我的數據庫,以便我試圖檢查的值是該孩子的關鍵? –
這將是一個確實可行的模式。 –