2016-11-17 56 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