我正在使用@react-native-firebase/firestore
我必须以错误的方式考虑集合或文档,但是如果直接通过onSnapshot
获取文档时遭到拒绝,我希望以下安全规则会阻止我通过doc
获取文档,但这不是案例
// I get permission denied when doing this
firestore().doc('users/KRImYj0Lrmv00P9KRAuJ').get()
// I do not get permission denied when doing this and can access the document I can't get above
firestore().collection('users').onSnapshot()
安全规则
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{user} {
allow read: if resource.data.is_deleted == false
allow write: if false;
}
}
}
// FIRST
try {
await firestore()
.collection('users')
.where('is_deleted', '==', true)
.onSnapshot((snapshot) => {
if (snapshot) {
snapshot.forEach((s) => {
console.log('User', s.data())
})
}
})
}
catch (error) {
console.log(error)
}
// SECOND
try {
const docs = await firestore()
.collection('users')
.where('is_deleted', '==', true)
.get()
console.log(docs)
}
catch (error) {
console.log(error)
}
第一个返回快照,我可以记录文档
FIRST LOGS
User {"fname": "Steve", "is_deleted": true, "lname": "Kaspar", "name": "Steven Kaspar", "name_change_count": 2}
User {"fname": "Brian", "is_deleted": true, "lname": "Fitz"}
SECOND LOGS
[Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.]
[我怀疑在onSnapshot
情况下,结果来自本地缓存,该缓存在您禁止读取操作之前就已填充。