我有以下几点:
收藏:
schedules
- uuid
- schoolUuid
- userUuid
- requestUuid
安全规则 match /schedules/{scheduleUuid}
function isAuthor(){
return resource.data.userUuid == request.auth.uid
}
function isAdmin(){
let schoolUuid = resource.data.schoolUuid
return get(/databases/$(database)/documents/admins/$(request.auth.uid)).data[schoolUuid] == true
}
read: if isAuth() && (isAuthor() || isAdmin())
create: if isAuth()
update: if isAuth() && (isAuthor() || isAdmin())
查询 作为管理员,此测试通过:
const db = firebaseDB({ uid: schoolAdmin.uuid, email: clinicAdmin.email });
const scheduleRef = db
.collection('schedules')
.where('schoolUuid', '==', 'some-school-uuid');
await firebase.assertSucceeds(scheduleRef.get());
作为作者,这个测试也通过了: const db = firebaseDB({ uid: schoolEmployee.uuid, email: schoolEmployee.email });
const scheduleRef = db
.collection('schedules')
.where('userUuid', '==', 'some-employeeUuid');
await firebase.assertSucceeds(scheduleRef.get());
现在,作为管理员或作者,以下查询失败: const db = firebaseDB({ uid: schoolEmployee.uuid, email: schoolEmployee.email }); // either schoolEmployee or schoolAdmin
const scheduleRef = db
.collection('schedules')
.where('requestUuid', '==', 'some-requestUuid');
await firebase.assertSucceeds(scheduleRef.get());
它给出以下错误:“userUuid 在对象上未定义”期待
我希望 resource.data 包含 userUuid、schoolUuid、requestUuid。我注意到在“where”查询中,resource.data 上唯一可用的属性是我正在查询的属性。例如,如果我这样做:
db.collection('schedules').where('schoolUuid', '==', 'some-uuid')
我的安全规则中只有 schoolUuid 可用,如果我尝试访问 userUuid 或 requestUuid,它会给出未定义。当我专门查询文档时,情况并非如此:
db.collections('schedules').doc('schedule-uuid')
在这种情况下,所有属性都在安全规则中可用。问题似乎是当我进行 where 查询时。通过文档:“查询约束匹配安全规则约束”。我想知道它失败的原因是不是因为我没有
'requestUuid', '==', 'some-requestUuid'
的安全规则我将不胜感激任何澄清。看来我在这里误解了一些重要的东西。我一直在用this reference
最佳答案
当您使用 resource
安全规则中,具体引用文档在关闭匹配的集合内 正在读取或写入。它不引用任何父文档。如果您需要引用规则特别匹配的文档之外的任何其他文档,则需要使用 get()
,如 accessing other documents 的文档中所述.由于您的嵌套文档依赖于不同文档的内容(即使它是父文档),它需要一个 get()
.
关于firebase - 为什么在执行 where 查询时未定义资源属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63421106/