2016-11-17 57 views
0

我只希望那些用戶讀取uid等於或from字段message或郵件的「至」部分設置爲all的郵件。
這是我的數據庫:我應該使用哪些Firebase安全規則來閱讀郵件?

{ 
"intents" : { 
    "intentFields" : "", 
    "intentName" : "", 
    "to" : "" 
}, 
"messages" : { 
    "-KViVgc7ZG051eMXP0-5" : { 
    "from" : "Ed", 
    "name" : "Ed", 
    "photoUrl" : "https://cdn1.iconfinder.com/data/icons/user-pictures/100/male3-512.png", 
    "text" : "Hello, I'm Ed", 
    "timeStamp" : "1476880306", 
    "to" : "all", 
    "type" : "text" 
    }, 
    "-KWmsuvm0uJIf01eHfyN" : { 
    "from" : "Capyv3mxQsUUn2W1nPOgcJ0Ex9T2", 
    "name" : "Aakash Bansal", 
    "photoUrl" : "https://lh5.googleusercontent.com/-oQyA4HXVycc/AAAAAAAAAAI/AAAAAAAAHKo/Ov0A0p0LjiY/s96-c/photo.jpg", 
    "text" : "ho", 
    "timeStamp" : "1479396273", 
    "to" : "Ed", 
    "type" : "text" 
    } 
} 
} 

我會嘗試一些規則,太閱讀火力地堡文檔。但是沒有任何規則幫助我實現我的結果。

的,我試過的規則的一個例子是:

{ 
"rules": { 
    "intents" : { 
    ".read" : "auth.uid === data.child('to').val()", 
    ".write" : true 
    }, 
    "messages" : { 
    "$message": { 
      ".read": "auth.uid !== null", 
      ".write": true 
    } 
    } 
} 
} 

下面一組規則工作得很好,雖然他們沒有達到我想要的結果,但他們顯示應用程序的所有消息。

{ 
"rules": { 
    "intents" : { 
    ".read" : "auth.uid === data.child('to').val()", 
    ".write" : true 
    }, 
    "messages" : { 
    ".read": "auth.uid !== null", 
    "$message": { 
      ".write": true 
    } 
    } 
} 
} 


只是爲了告知,我使用firebaseui在Android應用中讀取數據。請告訴我,如果我對「Firebase安全規則」的理解有任何問題。

編輯:即使下面的一組規則不工作:

{ 
"rules": { 
    "intents" : { 
    ".read" : "auth.uid === data.child('to').val()", 
    ".write" : true 
    }, 
    "messages": { 
    "$message": { 
     ".read" : true, 
     ".write": true 
    } 
    } 
} 
} 


,我用它來讀取數據了Android的代碼是:

mFirebaseAdapter = new MessageAdapter(FriendlyMessage.class, 
     R.layout.message_my, 
     RecyclerView.ViewHolder.class, 
     mFirebaseDatabaseReference.child("/messages")); 
+0

怎麼樣你*閱讀*的消息?因爲如果你想讀取'/ messages',它確實會因這些規則而失敗。這是一個常見的混淆來源,我們在文檔和[這裏的答案中描述爲[規則不是過濾器](https://firebase.google.com/docs/database/security/securing-data#rules_are_not_filters) ](http://stackoverflow.com/a/14298525/209103)。 –

+0

你的代碼在哪裏?你究竟在讀什麼? (提示:上面的puf的評論幾乎肯定是答案,但不能說沒有代碼) – Kato

+0

是puf,我已經把firebaserecycleradapter放在/ messages,我應該在哪裏得到這些消息? –

回答

0
{ 
    "rules": { 
    "messages": { 
     "$message": { 
     ".read": "auth.uid === data.child('to').val() || auth.uid === data.child('from').val() || data.child('to').val() === 'all' ", 
     ".write": true 
     } 
    } 
    } 
} 
+0

對不起,如果編輯後的版本有效,請告訴我。 – ksav

+0

不,先生,它不工作。雖然我已經嘗試了這個解決方案和一些更類似的解決方案,但我沒有得到應用程序中的任何數據 –

+0

什麼是不工作?你想要做什麼?你得到什麼錯誤? – ksav