【问题标题】:Firebase Rules for a social app社交应用的 Firebase 规则
【发布时间】:2018-04-29 10:56:51
【问题描述】:

我正在尝试扩展social android aap,目前它的规则是公开的,它的数据结构是这样的,

{
  "posts" : {
    "postId1" : {
      "authorId" : "abcd",
    },
    "postId2" : {
      "authorId" : "abcd",

    },
    "postId3" : {
      "authorId2" : "wxyz",

    },
    "postId4" : {
      "authorId2" : "wxyz",
    }
  }
}

我想允许经过身份验证的用户在“帖子”节点中创建和删除他自己的帖子 我试过了,

{
  "rules": {
        ".read":"auth.uid != null",
        ".write":false,
      "questions": {
      "$uid": {
        ".write": "$uid === auth.uid"
      }
    }
}}

但这不允许用户创建帖子,尽管用户可以在“帖子”节点中编辑或删除他预先存在的帖子,但似乎“帖子”节点内没有写入权限。 但是,如果我允许“帖子”的写权限,那么由于级联规则,每个经过身份验证的用户都可以访问其他人的数据。如何实现我想要的功能?

【问题讨论】:

    标签: android firebase-realtime-database firebase-security


    【解决方案1】:

    实时数据库的编写规则请见firebase-bolt:https://github.com/firebase/bolt

    这个工具使编写规则变得容易。

    对于您的规则,这里有一个示例,仅允许作者更新帖子:

    {
      "rules": {
        ".read": "auth.uid != null",
        "posts": {
          "$postId": {
            ".write": "data.val() == null && newData.child('uid').val() == auth.uid || data.val() != null && newData.val() != null && data.child('uid').val() == auth.uid || data.val() != null && newData.val() == null && newData.child('uid').val() == auth.uid"
          },
          ".read": "auth.uid != null"
        }
      }
    }
    

    Firebase 螺栓等效如下:

    path / {
        read() {auth.uid != null}
    }
    
    path /posts {
        read() {auth.uid != null}
        /{postId}{
        create() { this.uid == auth.uid}
        delete() { this.uid == auth.uid}
        update() { prior(this.uid) == auth.uid}
        }
    }
    

    【讨论】:

    • 兄弟我认为它仍然存在,我想要“帖子”节点的规则,这些规则不允许在“帖子”中创建内容。
    • @ShahzadAkram 据我所知,这授予path /posts/$postId 的创建和更新权限。如果这些规则对您来说失败,请显示执行写入数据库的代码。
    • databaseReference.updateChildren("/posts/" + post.getId(), postValues)
    • @FrankvanPuffelen,兄弟,我是 firebase 新手,但根据我的理解,我只需要写 path /posts/,$postId 不是我想写的路径,它是之后的帖子身份它的创作,如果我错了,请指导我。
    • $postId 表示变量值。这意味着在 post/ 之后将有自动生成或用户生成的唯一 ID,在该 ID 下将显示一个帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 2016-08-28
    • 2011-06-02
    • 2016-11-23
    • 1970-01-01
    • 2011-08-07
    相关资源
    最近更新 更多