我想保证用户只能添加与其 auth.uid 具有相同 id 的文档。
例如,ID 为 1X0T6xC6hhRN5H02zLCN6SQtwby2 的用户只能创建/更新文档 /salesmen/1X0T6xC6hhRN5H02zLCN6SQtwby2 。
在应用程序级别,它是通过以下命令完成的(用于创建和更新)。
this.db
.collection("salesmen")
.doc(user.uid)
.set(data)
.then(function() { //...
在 Firestore 级别,我正在尝试以下规则,但它不起作用(它会导致写入文档时出错:错误:权限缺失或不足.).
match /salesmen/{salesman} {
allow read: if true;
allow write: if request.auth != null && resource.id == request.auth.uid;
}
这条规则如何执行?
resource.id
只能用于引用 Firestore 中的现有文档。如果正在编写的文档尚不存在,则该方法不起作用。
相反,您可以使用salesman
在匹配表达式中使用通配符来确定用户是否可以写入特定文档,即使该文档尚不存在:
match /salesmen/{salesman} {
allow read: if true;
allow write: if request.auth != null && salesman == request.auth.uid;
}
看起来你也可以使用request.resource.id
引用可能尚未写入的文档的 id:
match /salesmen/{salesman} {
allow read: if true;
allow write: if request.auth != null && request.resource.id == request.auth.uid;
}
虽然我找不到明确讨论的内容参考文档 https://firebase.google.com/docs/firestore/reference/security/从这一刻起。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)