Kev*_*inB 4 firebase firebase-security google-cloud-firestore
我正在尝试为我的应用程序设置安全规则。
我有一个看起来像这样的集合:
Run Code Online (Sandbox Code Playgroud)Scores | | | | |___ AutoID______________history | |___value: 22 |__20190506 | |___userID: "abc" |___dateString:20190506 |___ AutoID |___value:22 |___value: 45 |___userID: "def"
我认为这样做就可以了:
match /scores/{docID=**} {
allow read: if userIsLoggedIn();
allow create, update: if request.resource.data.userID == request.auth.uid;
allow delete: if resource.data.userID == request.auth.uid;
}
Run Code Online (Sandbox Code Playgroud)
用户可以达到分数,但不能达到Collection“历史”。我认为问题来自于resource.data.userID。
“历史Collection”无法访问resource.data.userID。我想访问resource.data.userID我想从父文档
可以这样做吗?
如果您要使用的文档字段不在正在检查与规则匹配的读/写访问权限的文档中,则您必须使用get()其他文档(即使它位于父路径中)的完整路径,以便您可以在规则中使用其字段值。请阅读文档中有关访问其他文档的更多信息。
如果您在规则中不使用全局通配符,而是按名称调用子集合,那么可能会更容易,这样您就可以使用单独的通配符值更轻松地构建父文档的路径。像这样的东西:
match /scores/{scoreId}/history/{id} {
allow create, update:
if get(/databases/$(database)/documents/scores/$(scoreId)).data.userID == request.auth.uid;
}
Run Code Online (Sandbox Code Playgroud)
这不是一个确切的规则,但我希望您可以看到我在这里所做的事情,以便制定您自己的规则。
| 归档时间: |
|
| 查看次数: |
1685 次 |
| 最近记录: |