代码之家  ›  专栏  ›  技术社区  ›  Chris

无法将数据库时间戳与firebase“now”安全规则进行比较

  •  1
  • Chris  · 技术社区  · 8 年前

    我在比较firebase db中保存的时间间隔与“现在”firebase安全规则时遇到问题。我写的规则是为了防止用户在某个时间之后阅读帖子。 我就是这样 在我的数据库中保存时间戳 :

    "time":  NSDate().timeIntervalSince1970
    

    这是保安 我写的规则应该阻止阅读 如果在一段时间后:

    "follower-feed": {
      // ".read": "auth !== null",
      ".write": "auth !== null",
        "$userID": {
        "$postID": {
          ".read": "auth !== null && data.child('time').val() >= ((now / 1000) - 86400)",}}},
    

    这就是 数据库架构 我正在使用存储帖子:

    -从动件进给:{

    user\u uid\u 1:{

    post_uid_1: {
    
         "time": 1515435031.16646
    
    post_uid_2: {
    
         "time": 1515435091.22323
    

    我想指出的是,我已经考虑到了‘now’是以毫秒为单位的这一事实,将其除以1000应该会将我的两个数字设置为相同的秒时间值。我已经扼杀了所有关于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()
            }
    
        })
    
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Bob Snyder    8 年前

    看来 CURRENT_USER_FEED_REF 是包含给定用户帖子的位置,即。 follower-feed/$userID ,并且您希望帖子年龄的安全规则将充当过滤器,允许查询返回用户最近的帖子,并排除旧帖子。但安全规则不是过滤器。对于任何位置,您要么能够读取所有数据(包括其子数据),要么不能读取任何数据。您没有允许读取的规则 关注者订阅源/$用户ID ,因此该位置的查询将失败。

    看见 this answer 从Firebase团队成员处获取有关如何实现所需内容的想法,或搜索“Firebase规则不是过滤器”以查看其他相关问题和答案。