【发布时间】:2016-06-11 16:02:36
【问题描述】:
这一定很容易做到,而且我正在做一些愚蠢的事情,但是 4 个小时我无法理解我做错了什么。 我有这样的数据库实体:
posts:
-KK-3ajDuSUglnMMdXgy
authorId: "DYI4TPbwQocFsVROStUqcGNFHhW2"
authorName: "john"
privacy: "PrivacyTypeWorld"
text: "some post text"
帖子可以有 3 个级别的隐私:所有人 (PrivacyTypeWorld)、仅作者 (PrivacyTypePrivate) 和朋友 (friendId、friendId, ...)。
我的安全规则如下所示:
{
"rules": {
"posts": {
".read": "auth != null && (data.child('privacy').val() === 'PrivacyTypeWorld')",
".write": "auth != null"
}
}
}
但是有了这个规则,我什么都看不到。如果我设置".read": "auth != null" - 一切正常。
我做错了什么?
编辑:
要检索我使用的数据:
_refHandle = [[_ref child:@"posts"] observeEventType:FIRDataEventTypeChildAdded
withBlock:^(FIRDataSnapshot *snapshot) {
[_arrPosts addObject:snapshot];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_arrPosts.count-1 inSection:0]] withRowAnimation: UITableViewRowAnimationAutomatic];
}];
【问题讨论】:
-
您的规则缺少一个级别。要访问每个节点,您需要一个变量...例如在本例中为 $post_id,然后您可以引用(一般来说)$post_id/privacy = 'whatever'。请参阅 Structuring Your Rules 部分中大约 1/2 处的 Secure Your Data。否则,您正在寻找帖子/隐私,其中应该是帖子/post_id/privacy。
-
您能否将无效的代码添加到您的问题中?
-
@FrankvanPuffelen,什么代码?我可以在 Firebase 控制台中看到实际添加的数据 (dropbox.com/s/shbfrob1af8o405/…) 和规则 loojs,如 adolfosrs 回答
-
@daleijn 他的意思是你的快速代码。您如何尝试检索数据...
-
@adolfosrs,更新问题
标签: ios firebase firebase-realtime-database firebase-security