我有一个像这样的 firestore 数据库
我有这样的 Firestore 安全规则设置。
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read: if request.auth != null;
}
match /users/{userId}/{document=**} {
allow read, create, update, delete: if request.auth != null && request.auth.uid == userId ;
}
}
}
我应该如何设置规则-
您的规则中有一个 重叠匹配语句,因为您使用
match /{document=**}
映射到数据库中的 ALL 文档(请参阅 doc)。
所以,从那时起:
match /{document=**}
语句,您的规则是 allow read: if request.auth != null;
,然后 每个经过身份验证的用户都可以阅读任何
user
文档(您不会像对 uid
集合那样限制 users
。实际上您不能这样做,因为 match /{document=**}
并不专门针对 users
收藏)。
最好的办法是删除这个块并只保留下面的块。
match /users/{userId}/{document=**} {
allow read, create, update, delete: if request.auth != null && request.auth.uid == userId ;
}
如果您需要授予对其他集合的读取访问权限,请对每个集合使用规则,而不是使用通配符方法。
PS:您可以仔细检查是否确实需要执行
match /users/{userId}/{document=**}
,这将授予对 users
文档的所有子集合的访问权限。您可能只需要做match /users/{userId}
。