【发布时间】:2019-02-26 03:40:18
【问题描述】:
我正在使用 Firebase 商店/Firebase 规则构建类似论坛的结构。我的结构是这样的:
Collection --- Document ------ Collection --- Document
Topic1 CreationDate UsersJoined UserUID1
Topic2 Title UserUID2
Topic3 UpdatedDate UserUID3
... ... ...
基本上,每个主题都有一个用户集合。我的目标是能够编写一个安全规则,其中只有“UsersJoined”中的用户可以读/写相应的主题。这就是我现在的规则:
service cloud.firestore {
match /databases/{database}/documents {
match /Topics/{topicUID} {
allow read, create, update, delete: if exists(/databases/$(database)/documents/Topics/$(topicUID)/UsersJoined/$(request.auth.uid));
match /UsersJoined/{userUID=**} {
allow read, create, update, delete;
}
}
}
}
所以当我使用内置模拟器时,读取工作正常;但是,当我请求通过我的 IOS 代码阅读它时,它告诉我我没有足够的权限。
我试过只做allow read: if request.auth.uid != null;,我可以阅读。我确信 UserUID 确实存在于 UsersJoined 集合中。
我还尝试创建一个“姐妹”集合来存储我的用户 ID,所以我的结构如下所示:
Collection ----------- Document
MyTestUserCollection UserUID1
Topic1 UserUID2
Topic2 ...
...
然后我使用了这条规则:if exists(/databases/$(database)/documents/MyTestUserCollection/$(request.auth.uid));,并且在模拟器和 IOS 代码上读取也一样。
当用户列表嵌套在主题中时,我的问题是无法阅读。所以我的问题是......通过编写一个检查(“读取”)嵌套集合中数据的规则,我是否违反了“允许读取”规则(因为从技术上讲它还没有确定我是否可以读取)?还是我把事情复杂化了一点,有更好的方法来构建我的集合/文档?还是我只是没有正确编写规则?
我不相信我在 IOS 上的代码是问题所在,但以防万一这是我请求从我的数据库中读取的操作:(用户通过 Firebase Auth 登录)
[[myFirestore collectionWithPath:@"Topics"]
getDocumentsWithCompletion:^(FIRQuerySnapshot *snapshot, NSError *error) {
if (error != nil) {
NSLog(@"Error getting documents: %@", error);
} else {
NSLog(@"Read it");
}
}];
非常感谢任何帮助!
【问题讨论】:
标签: ios firebase google-cloud-firestore firebase-security