Firebase规则:允许推送但不允许更新

Dan*_*nov 2 validation data-security firebase firebase-security firebase-realtime-database

我很难理解如何允许用户在列表中创建新记录,但只允许创建者更新自己的帖子.

例如以下结构:

post {
    post1: {
        author: "user1"
        text: "Some text"
    }
    post2: {
        author: "user2"
        text: "Some text 2"
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我希望两个用户都能够创建新帖子.但也可以保护post2不被user1编辑.因此,只有user1可以编辑post1,只有user2可以编辑post2.

Mic*_*igh 8

你想做这样的事情:

{"rules": {
  "post": {
    "$id": {
      ".write": "auth !== null && (!data.exists() || data.child('author').val() === auth.uid)"
    }
  }
}}
Run Code Online (Sandbox Code Playgroud)

如果用户已登录,则只允许写入; a)尝试写入的节点为空或b)尝试写入的节点由当前用户创作.