【发布时间】:2021-05-21 21:30:31
【问题描述】:
在阅读基于云防火墙角色的访问示例https://firebase.google.com/docs/firestore/solutions/role-based-access#rules,第4步时,我发现用户是否可以创建评论并不清楚。
根据上面的链接,评论归用户所有,这里是数据模型:
/stories/{storyid}/comments/{commentid}
{
user: "alice",
content: "I think this is a great story!"
}
还有规则:
match /comments/{comment} {
allow read: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
['owner', 'writer', 'commenter', 'reader']);
// Owners, writers, and commenters can create comments. The
// user id in the comment document must match the requesting
// user's id.
//
// Note: we have to use get() here to retrieve the story
// document so that we can check the user's role.
allow create: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
['owner', 'writer', 'commenter'])
&& request.resource.data.user == request.auth.uid;
}
注意规则的最后一行,要创建评论,经过身份验证的用户 (request.auth.uid) 必须是评论所有者的用户。但是,在创建此评论之前,此用户属性如何存在?也许,在创建评论时,不需要规则“&& request.resource.data.user == request.auth.uid”的最后一段。但是在更新评论的时候,可以添加这条规则。
我错过了什么吗?顺便说一句,在将示例用于在线参考之前,他们是否真的测试了示例?也很遗憾,这些在线文档创建时没有时间戳。现在有人告诉我,两年前的东西可能已经过时了。
【问题讨论】:
标签: database google-cloud-firestore firebase-security