Cloud Firestore - 请求变量未按预期工作

时间:2017-11-11 09:09:32

标签: android firebase firebase-security google-cloud-firestore

我有一个简单的火库数据库,几乎没有国家,每个国家都有一个城市。考虑每个国家的文件,即法国。使用面向PERMISSION_DENIED异常的请求变量读取文档时,遵循以下两个方案。

分享以下内容供您参考 1)FireStore配置 2)不能正常工作的具体配置 3)每个故障的相关日志 3)创建和读取firestore数据库的类文件 4)调用以下场景的方法

场景1 - 以下2个权限中的至少一个应该有效,但它们都不起作用

allow read : if request.resource.__name__ != null ; 
allow read : if request.resource.__name__ == null ; 

错误记录

I / System.out:firestoreTest ACTION_READ onFailureListener e = com.google.firebase.firestore.FirebaseFirestoreException:PERMISSION_DENIED:权限丢失或不足。 I / System.out:firestoreTest getOnCompleteListener不成功

场景2 - 虽然文档名称'法国'存在于数据库中,它不起作用。

allow read : if request.resource.__name__ == /databases/$(database)/documents/Countries/france ; 

错误记录

I / System.out:FireStoreTest.firestoreTest action = [2],country = [france],hashmapCountry = [null] I / System.out:firestoreTest ACTION_READ onFailureListener e = com.google.firebase.firestore.FirebaseFirestoreException:PERMISSION_DENIED:权限丢失或不足。 I / System.out:firestoreTest getOnCompleteListener不成功

场景3 - 没问题,运作良好。仅出现,因为类似的情景2不起作用

 allow read : if resource.__name__ == /databases/$(database)/documents/Countries/france ; 

错误记录

I / System.out:FireStoreTest.firestoreTest action = [2],country = [france],hashmapCountry = [null] I / System.out:firestoreTest getOnCompleteListener成功{city = paris}

测试方案的基本配置

service cloud.firestore {
     match /databases/{database}/documents
    {
        match /Countries/{country}
          {

        allow write : if true ;     
        allow read : // replace as per below scenario 

          }    
    }    
}

}

创建集合的方法,需要在测试前调用一次

HashMap<String, String > hashMap = new HashMap<>() ;
hashMap.put("city","paris") ;
fireStoreTest.firestoreTest(fireStoreTest.ACTION_ADD,"france",hashMap);

方法可用于测试以下3种情况

 fireStoreTest.firestoreTest(fireStoreTest.ACTION_READ,"france",null);

以下课程用于测试以上3种情况

package com.firestoreTest ;

import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.HashMap;
import java.util.Map;


public class FireStoreTest {

public static final int ACTION_ADD = 1 ;
public static final int ACTION_READ = 2 ;
private FirebaseFirestore firebaseFirestore ;

public FireStoreTest() {
    firebaseFirestore = FirebaseFirestore.getInstance() ;
}

public void firestoreTest(int action, String country, HashMap hashmapCountry)

{
    System.out.println("FireStoreTest.firestoreTest action = [" + action + "], country = [" + country + "], hashmapCountry = [" + hashmapCountry + "]");

    DocumentReference documentReference =  firebaseFirestore.collection("Countries").document(country) ;

    switch (action)
    {
        case ACTION_ADD:



            documentReference.set(hashmapCountry)
                    .addOnFailureListener(e-> System.out.println("firestoreTest ACTION_ADD onFailureListener e = "+e)  )
                    .addOnCompleteListener(task -> {

                        if (task.isSuccessful())
                        {
                            System.out.println("firestoreTest setOnCompleteListener  Successful  " );
                        } else
                        {
                            System.out.println("firestoreTest setOnCompleteListener  Unsuccessful  " +task.getException() );
                        }

                    }) ;





            break ;

        case ACTION_READ:

            documentReference.get()
                    .addOnFailureListener(e-> System.out.println("firestoreTest ACTION_READ onFailureListener e = "+e))
                    .addOnCompleteListener(taskDocumentSnapshot->{

                        if (taskDocumentSnapshot.isSuccessful())
                        {
                            DocumentSnapshot documentSnapshot = taskDocumentSnapshot.getResult() ;

                            if (documentSnapshot.exists())
                            {
                                Map mapDocumentSnapshot = documentSnapshot.getData() ;

                                System.out.println("firestoreTest getOnCompleteListener  Successful  "+mapDocumentSnapshot );

                            } else
                            {
                                System.out.println("firestoreTest getOnCompleteListener  no data found  " );
                            }

                        } else
                        {

                            System.out.println("firestoreTest getOnCompleteListener  Unsuccessful  " );
                        }

                    }) ;
            break ;
    }

}

}

1 个答案:

答案 0 :(得分:0)

收到来自firebase支持的以下回复

问题在于使用request.resource。如our documentation中所述,request.resource(及其所有变量,如request.resource。 name 和request.resource.data.city)用于写入操作,但不用于读取操作