【发布时间】:2018-01-08 18:33:14
【问题描述】:
我在将我保存在我的 firebase 数据库中的时间间隔与“现在”的 firebase 安全规则进行比较时遇到问题。我正在写的规则是为了防止用户在某个时间之后阅读。 这就是我在我的数据库中保存时间戳的方式:
"time": NSDate().timeIntervalSince1970
这是我正在编写的安全性规则,如果在一定时间后应该防止读取:
"follower-feed": {
// ".read": "auth !== null",
".write": "auth !== null",
"$userID": {
"$postID": {
".read": "auth !== null && data.child('time').val() >= ((now / 1000) - 86400)",}}},
这是我用来存储帖子的数据库架构:
-follower-feed: {
user_uid_1:{
post_uid_1: {
"time": 1515435031.16646
post_uid_2: {
"time": 1515435091.22323
我想指出,我已经考虑了“现在”以毫秒为单位的事实,并且将其除以 1,000 应该将我的两个数字设置为相同的秒时间值。我已经扼杀了关于 firebase 文档的所有内容,但没有什么能帮助我解决这个问题。当我运行模拟器测试以确定请求的读取是否会通过时,它说它将通过。但是,在我的应用程序中没有读取任何数据。
这是尝试从 firebase 读取数据的代码:
var followingPosts = [Post]()
func loadUserFeed(_ update: @escaping () -> Void) {
userFeedHandle = CURRENT_USER_FEED_REF.observe(DataEventType.value, with: {(snapshot) in
self.followingPosts.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let post = Post(postID: child.key, postData: child.value as! Dictionary<String, Any>)
self.followingPosts.append(post)
self.followingPosts.sort(by: { Double(truncating: $0.time) > Double(truncating: $1.time)})
update()
}
if snapshot.childrenCount == 0 {
update()
}
})
}
【问题讨论】:
-
如果取消注释规则
".read": "auth !== null"是数据读取成功吗? -
是的,如果我将规则放在节点的顶部,则读取成功
-
您能否发布尝试读取数据库但失败的代码?当我请求读取
/follower-feed/some-userID/some-postID时,该规则是正确的并且适用于我使用Android 客户端。请注意,规则不是过滤器。您没有针对位置follower-feed或follower-feed/$userID的规则,因此如果您在这些位置请求查询,它将失败。 -
确定,我更新了我的帖子
标签: swift firebase firebase-security