此文档页:编写云修复安全规则的条件,如下所示:
另一个常见的模式是确保用户只能读写自己的数据。
并提供此示例:
service cloud.firestore {
match /databases/{database}/documents {
// Make sure the uid of the requesting user matches name of the user
// document. The wildcard expression {userId} makes the userId variable
// available in rules.
match /users/{userId} {
allow read, update, delete: if request.auth.uid == userId;
allow create: if request.auth.uid != null;
}
}
}我不明白为什么create规则不是用与其他规则if request.auth.uid == userId相同的条件定义的,而是用if request.auth.uid != null定义的。据我所知,有了这个规则,任何用户都可以在users中创建任何文档,但除非它与他的uid匹配,否则不能使用它做任何事情。那么,为什么要允许它呢?
发布于 2018-08-18 04:21:47
让我们讨论可以实现的非常基本的安全规则(通过用户身份验证):
allow read, update, delete: if request.auth.uid != null;
allow create: if request.auth.uid != null;任何用户都可以删除其他人创建的文档。因此,为了限制/控制它,我们实现了所提供的代码片段。
service cloud.firestore {
match /databases/{database}/documents {
// Make sure the uid of the requesting user matches name of the user
// document. The wildcard expression {userId} makes the userId variable
// available in rules.
match /users/{userId} {
allow read, update, delete: if request.auth.uid == userId;
allow create: if request.auth.uid != null;
}
}
}代码片段只是一个示例用例,它使用不同的条件作为参考,因为这是教程/指南,因此Firebase团队尝试将尽可能多的条件匹配到代码片段中。
当然,您可以执行allow create: if request.auth.uid == userId;来严格限制特定用户。
我希望它能给你一些想法!
https://stackoverflow.com/questions/51902762
复制相似问题