在我的项目中,我想通过检查用户的 uid 是否是该文档的子集合(包含该文档的所有成员)的一部分来控制对文档的访问。 当我想用 exists() 方法检查它时,它没有在应该的时候授予权限。
match /events/{season}/events/{code} {
function isVV (season, code) {
return exists(/databases/$(database)/documents/events/$(season)/events/$(code)/vv/$(request.auth.uid));
}
allow read: if isVV(season, code);
当我将 $(code) 替换为我当前正在测试的值时,规则通过并且一切都按预期工作。当我使用变量时它不会。
有没有 Cloud Firestore Rules 专家可以帮助我? 也许有更好的方法来做到这一点?
最佳答案
出于某种原因,不喜欢评估 Firestore 规则中数据库以外的变量。查看文档,我发现 exists 函数实际上接受了一个 Path
,在此处进行了描述:https://firebase.google.com/docs/reference/rules/rules.Path
使用这些信息,我能够通过首先使用连接将路径构造为字符串然后将其传递给 exists
函数来实现与您上面想要的类似的东西。
对于上面的示例,这看起来像:
match /events/{season}/events/{code} {
function isVV (database, season, code) {
return exists(path("/databases/" + database + "/documents/events/" + season + "/events/" + code + "/vv/" + request.auth.uid));
}
allow read: if isVV(database, season, code);
}
注意:您必须将数据库作为函数参数传递,因为我发现以这种方式使用字符串连接时,它不会像使用 exists(/databases/$(database ))
关于firebase - Exists() 不适用于第二个变量 Cloud Firestore 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397091/