在flutter中查询firestore:权限被拒绝,该怎么办



我正在尝试从专用设备查询一些数据。我的规则设置如下:

match /private_devices/{device} {
function userHasKey() {
return request.auth != null && exists(/databases/$(database)/documents/users/$(request.auth.uid)/keys/$(device));
}

allow read: if (userHasKey())
}

我的颤振代码:

Stream<QuerySnapshot> getPrivateDevices(List<String> keyList) {
return private.where('UDID', whereIn: keyList).snapshots();
}

我使用列表从导入密钥

Stream<QuerySnapshot> getUserKeys(User user) {
return users.document(user.uid).collection('keys').snapshots();
}

我的用户规则:

match /users/{userId} {
allow read, write: if (request.auth != null && request.auth.uid == userId)
}

match /users/{userId}/keys/{key} {
allow read, write: if (request.auth != null && request.auth.uid == userId)
}

编辑:我的收藏参考:

final CollectionReference devices = Firestore.instance.collection("devices");
final CollectionReference users = Firestore.instance.collection("users");
final CollectionReference private = Firestore.instance.collection("private_devices");

我在控制台中收到以下权限错误:

Listen for Query(private_devices where UDID in [F8MXi2JzwYvIAoVRYG5f] order by __name__) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}
I/System.out(27625): com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.

我还在规则操场上测试了我的规则,一切都很好。我知道这与查询有关,但不知道如何前进。

我还注意到,当我尝试使用private.document(keyList[0]).get()获取单个文档时,实际上我会返回数据。

欢迎任何帮助,提前谢谢。

您的规则不允许查询,因为安全规则不是过滤器。请务必仔细阅读该文档以及本博客。

安全规则不会为查询结果中的每个文档交叉引用另一个文档。这并没有达到Firestore所要求的规模。可以为单个文档获取执行此操作,但不能为查询执行此操作。

相反,您必须将筛选器所需的所有数据放入单个集合中的文档中,并确保客户端应用的筛选器仅与规则允许他们读取的文档相匹配。

相关内容

最新更新