【问题标题】:Why am I getting permission denied to my Firestore data?为什么我的 Firestore 数据的权限被拒绝?
【发布时间】:2019-10-01 05:05:45
【问题描述】:

我已经设置了一个简单的 Firestore 数据库来存储数据并处理规则/权限。我可以访问一个名为“workplace”的集合中的数据,但我无法读取名为“employee”的新集合中的数据

我已尝试将规则更改为:

allow read: if true;

我可以读取数据

我在应用中的查询:

db.collection("employee")
                .whereEqualTo("workplaceId", w.getId())
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
                            Employee e = doc.toObject(Employee.class);
                            e.setId(doc.getId());
                            Log.d(TAG, "staff: " + e.getFirstName());
                        }
                    }
                });

我在 Firestore 中的规则:

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

    match /workplace/{workplaceID} {
      allow read: if resource.data.ownerId == request.auth.uid;
      allow write: if request.auth.uid != null;
    }

    match /employee/{employeeID} {
        allow read: if resource.data.workplaceId == request.resource.data.workplaceId;
      allow write: if request.auth.uid != null;
    }
  }
}

我希望能够获取“员工”集合中的所有文档,其中它们的“workplaceId”等于我传入的数据。

请参阅下面我的数据库结构图像的链接:

https://imgur.com/GArRGkg

【问题讨论】:

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


    【解决方案1】:

    原来我的问题是我试图使用规则来“过滤”数据,这是不正确的并且会失败。

    查询已经在这里执行过滤,所以我只需要将规则更改为:

    match /employee/{employeeID} {
            allow read, write: if request.auth.uid != null;
        }
    

    这让我可以访问数据,并且查询只选择了我想要检索的数据。

    【讨论】:

    • 您仍应检查用户是否有权访问特定资源。否则,可以修改查询以包含属于其他用户的数据。
    猜你喜欢
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2019-03-20
    • 2020-10-25
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    相关资源
    最近更新 更多