我已经创建了一个Firebase实时数据库和一个使用Android Studio (Java)进行读写的应用程序。在Firebase中,我有:一个包含信息的用户文件夹,一个包含任何用户都可以创建的数据的文章文件夹,这里是"Products“文件夹的结构:
Products
-MNywaQZrRqIgjQINbZK
description: "A"
members
94cMm9pJ59a517aNgLijZV9HjGt1: "94cMm9pJ59a517aNgLijZV9HjGt1"
admin: "pU4gGrq93fgcDZGX9QT95k3wOCB2"
product_Location: "Loc A"
product_Note: "q"
quantity: "2"
-MNzHkmEBnYDWQjYQJZq
description: "B"
members
admin: "94cMm9pJ59a517aNgLijZV9HjGt1"
product_Location: "Loc B"
product_Note: "alpha"
quantity: "2"
以下是安全规则。它们允许任何用户访问他创建的内容( (data.child('members').child(auth.uid).exists()) = auth.uid)或任何其他用户与他共享的admin
{
"rules": {
"Products": {
"$Product_id": {
".read": "data.child('members').child('admin').val() === auth.uid || data.child('members').child(auth.uid).exists()",
".write": "auth.uid != null"
}
}
}
}
}
有了这些,当我被标识为用户时,我可以访问节点"Products“中的任何子节点,允许我这样做。
我面临的问题是,当我使用Android Studio提取数据时,我正在读取文件夹"Products“本身,因为规则是为"Products/$Product_id”设置的,它不起作用,我没有被授权读取数据。以下是Android Studio中的代码:
mDatabase = FirebaseDatabase.getInstance();
mRef = mDatabase.getReference().child("Products");
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot1) {
mProducts.clear();
for(DataSnapshot snapshot2:snapshot1.getChildren()) {
Article info_from_Firebase = snapshot2.getValue(Article.class);
mProducts.add(info_from_Firebase);
}
arrayAdapter.notifyDataSetChanged();
scr2_listcontent = (ListView) findViewById(R.id.scr2_lv);
arrayAdapter = new ArticleAdapter(getApplicationContext(), mProducts);
scr2_listcontent.setAdapter(arrayAdapter);
}
当然,当我将Firebase中的规则更改为以下内容时,我可以读取数据,但我想要设置的访问限制不再存在:
{
"rules": {
"Products": {
".write": "auth.uid != null",
".read": "auth.uid != null",
}
}
}
在Firebase或Android Studio中有什么我应该做的不同的事情吗?
非常感谢您的回答,并祝您2021新年快乐。
转载请注明出处:http://www.0730huitian.com/article/20230526/1963240.html