我对 Firebase 非常陌生,目前使用 FlutterFlow 构建基本 UI。我遇到了理解问题,我不知道自己做错了什么。
在命名组织的集合中,我有以下内容:
总体目标是成员中的所有 auth-userID 都可以读取、写入和删除该组织。
如果我将 Firebase 中的规则设置为
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{document} {
allow create: if request.auth.uid == document;
allow read: if request.auth.uid == document;
allow write: if request.auth.uid == document;
allow delete: if false;
}
match /organizations/{document} {
allow create: if request.auth != null;
allow read: if request.auth != null;
allow write: if request.auth != null;
allow delete: if request.auth != null;
}
}
}
我的 UI 呈现了集合中的所有项目,一切都很好。如果我现在将读写删除更改为标记用户(flutter UI 使用的术语)
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{document} {
allow create: if request.auth.uid == document;
allow read: if request.auth.uid == document;
allow write: if request.auth.uid == document;
allow delete: if false;
}
match /organizations/{document} {
allow create: if request.auth != null;
allow read: if request.auth.uid in resource.data.members;
allow write: if request.auth.uid in resource.data.members;
allow delete: if request.auth.uid in resource.data.members;
}
}
}
我不再找回任何组织。我在组织>用户中添加了身份验证用户ID手册,格式为/users/XXXXXX或仅从users/{documentsIDs}获取的XXXXXXX,但没有区别。
我做错了什么?
在第二个屏幕截图中,您使用
in
运算符,它检查左侧字段的奇异值(在您的情况下为 members
)是否包含在您在右侧指定的值数组中边。由于 members
是一个数组,因此它会检查右侧数组内的数组,这不是您想要的。
array-contains
运算符,它检查您在右侧指定的奇异值是否存在于您在左侧指定的数组字段中。
不相关:
create
的organization
规则不正确。 resource.data
包含写入操作之前的文档值,因此create
始终为空。您需要使用
request.resource.data
,其中包含写入操作后将存在的值(如果允许)。
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{document} {
allow create: if request.auth.uid == document;
allow read: if request.auth.uid == document;
allow write: if request.auth.uid == document;
allow delete: if false;
}
match /organizations/{document} {
allow create: if request.auth != null;
allow read: if resource.data.created_by == /databases/$(database)/documents/users/$(request.auth.uid);
allow write: if resource.data.created_by == /databases/$(database)/documents/users/$(request.auth.uid);
allow delete: if resource.data.created_by == /databases/$(database)/documents/users/$(request.auth.uid);
}
}
}
但是鉴于以下声明:
https://firebase.google.com/docs/firestore/security/rules-query#secure_and_query_documents_based_on_authuid
即使当前用户实际上是每个故事文档的作者,查询也会失败。出现此行为的原因是,当 Cloud Firestore 应用您的安全规则时,它会根据其潜在结果集而不是数据库中文档的实际属性来评估查询。如果查询可能包含违反安全规则的文档,则查询将失败。如果加载文档列表,必须进行过滤,以免违反上述规定。