这些是我的火力计规则:
{
"rules": {
"intents" : {
".read" : "auth.uid === data.child('to').val()",
".write" : true
},
"messages" : {
"$user" : {
".read": "auth.uid === data.child('to').val() ||
auth.uid === data.child('from').val() ||
(auth.uid === 'iAmEd' && data.child('to').val() === 'Ed') ||
data.child('to').val() === 'all'",
".write": true
}
}
}
}
messages
上的读取操作说明是:
如果向用户A发送消息,则允许A读取消息(auth.uid === data.child('to').val()
)
如果用户A发送了一条消息,则允许A读取该消息(auth.uid === data.child('from').val()
)
uid = "iAmEd"
),消息也必须在to
部分中包含“Ed”。 ((auth.uid === 'iAmEd' && data.child('to').val() === 'Ed')
)to
=“全部”发送,则每个人都可以阅读该邮件。虽然写操作工作正常。但是我的客户端无法阅读任何消息,也不会将消息发送给to
设置为all
的消息。
以下是两个示例消息供参考:
{
"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"
},
"-KWK50chFl_QroPwoYVu" : {
"from" : "Capyv3mxQsUUn2W1nPOgcJ0Ex9T2",
"name" : "Aakash Bansal",
"photoUrl" : "https://lh5.googleusercontent.com/-oQyA4HXVycc/AAAAAAAAAAI/AAAAAAAAHKo/Ov0A0p0LjiY/s96-c/photo.jpg",
"text" : "hi",
"timeStamp" : "1478896392",
"to" : "Ed",
"type" : "text"
}
}
}
我也查看了几个YouTube视频,但它们没有参考如何比较firebase安全规则中的字符串。
编辑:当我使用这些简单的规则时,它们也不起作用。虽然用户能够写,但它没有阅读任何消息。
{
"rules": {
"intents" : {
".read" : "auth.uid === data.child('to').val()",
".write" : true
},
"messages" : {
"$message" : {
".read" : "data.child('from').val() === auth.uid"
},
".write": true
}
}
}
另外,如果您认为我的Android代码或RecyclerAdapter实现可能存在问题,我可能会告诉我,当我使用".read" : true
时,android能够正确读取代码。
因此,他们肯定只是我的规则的一些问题。
编辑2 还有一个问题,当我使用以下规则时,应用仍然没有读取数据。
{
"rules": {
"intents" : {
".read" : "auth.uid === data.child('to').val()",
".write" : true
},
"messages" : {
"$message" : {
".read": true,
},
".write": true
}
}
}
但是当我使用以下代码时,应用程序会准确地读取数据:
{
"rules": {
"intents" : {
".read" : "auth.uid === data.child('to').val()",
".write" : true
},
"messages" : {
".read": true,
".write": true
}
}
}
以下是读取数据的android代码:
MessageAdapter.java
public class MessageAdapter extends FirebaseRecyclerAdapter<FriendlyMessage, MessageViewHolder> {
private static final String TAG = MessageAdapter.class.getSimpleName();
private ProgressBar mProgressBar;
private Context context;
public MessageAdapter(Class<FriendlyMessage> modelClass, int modelLayout, Class<MessageViewHolder> viewHolderClass, DatabaseReference ref, Context context, ProgressBar progressBar) {
super(modelClass, modelLayout, viewHolderClass, ref);
this.context = context;
this.mProgressBar =progressBar;
}
@Override
protected void populateViewHolder(MessageViewHolder viewHolder, FriendlyMessage friendlyMessage, int position) {
Log.d(TAG, "populateViewHolder reached");
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
viewHolder.getMessageTextView().setText(friendlyMessage.getText());
viewHolder.getMessengerTextView().setText(friendlyMessage.getName());
if (friendlyMessage.getPhotoUrl() == null) {
viewHolder.getMessengerImageView()
.setImageDrawable(ContextCompat
.getDrawable(context,
tod));
} else {
Glide.with(getApplicationContext())
.load(friendlyMessage.getPhotoUrl())
.into(viewHolder.getMessengerImageView());
}
boolean temp;
try {
temp = friendlyMessage.getName().equals("Ed");
} catch (NullPointerException e) {
temp = false;
}
if (temp) {
viewHolder.getDiffForRecSend().setBackgroundResource(R.drawable.bubble);
viewHolder.getMessageTextView().setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
viewHolder.getMessengerTextView().setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
} else {
viewHolder.getDiffForRecSend().setBackgroundResource(R.drawable.blankbluebububble);
viewHolder.getMessengerTextView().setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.blue1));
viewHolder.getMessageTextView().setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.blue));
}
Log.d(TAG, "populateViewHolder ended");
}
}
MainActivity.java
/***/
mFirebaseAdapter = new MessageAdapter(FriendlyMessage.class,
R.layout.item_message,
MessageViewHolder.class,
mFirebaseDatabaseReference.child("/messages"),
context,
mProgressBar);
/***/
我只使用了FirebaseUI和一个扩展它的自定义Recycler Adapter。
任何帮助将不胜感激!