我正在尝试使用“规则”选项卡在firestore中设置授权以读取记录。
问题
allow read: if request.auth.uid != null && resource.data.firebaseID == request.auth.uid;
我必须过滤并读取用户创建的记录。另外,我还必须避免其他用户创建的数据。当我在“规则”部分中使用上述规则时,没有从中获得任何记录。你能帮我解决这个问题吗?
数据
完整规则
service cloud.firestore {
match /databases/{database}/documents {
match /sampleData/{anything=**}{
allow read, write: if true;
}
match /{document=**} {
allow read: if request.auth.uid != null && resource.data.firebaseID == request.auth.uid;
allow write: if request.auth.uid != null ;
}
}
}
代码
private void proceedExportToFireStore() {
// Access a Cloud Firestore instance from your Activity
FirebaseFirestore db = FirebaseFirestore.getInstance();
WriteBatch batch = db.batch();
DocumentReference newCoinRef;
CollectionReference cryptos = db.collection("cryptos");
List<Tran> tranList = getAllTranForFireStore();
String firebaseUID = FirebaseAuth.getInstance().getCurrentUser().getUid();
for (Tran t : tranList) {
Map<String, Object> tranData = new HashMap<>();
tranData.put("firebaseID", firebaseUID);
tranData.put("createdBy", userEmailID);
tranData.put("coinid", t.getCoinID());
tranData.put("coinname", t.getCoinName());
tranData.put("coinsymbol", t.getCoinSymbol());
tranData.put("date", String.valueOf(t.getDate()));
tranData.put("qty", String.valueOf(t.getQty()));
tranData.put("price", String.valueOf(t.getPrice()));
tranData.put("priceunit", String.valueOf(t.getPriceUnit()));
newCoinRef= cryptos.document(t.getCoinID());
batch.set(newCoinRef, tranData);
}
batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
System.out.println("Testing 1 Batch add done");
}
});
}