【问题标题】:Firebase database rules how to check if node property value for comparison to uidFirebase 数据库规则如何检查节点属性值是否与 uid 进行比较
【发布时间】:2021-03-02 11:32:18
【问题描述】:

我正在使用 firbase 数据库,我正在尝试设置规则来限制用户的写入权限,这些用户的 uid 与他们尝试写入的对象的 userId 节点匹配,并且仍然允许写入以创建新对象,例如新帖子如果它不存在。因此允许新帖子并允许编辑您自己的帖子,但如果帖子存在但不属于用户,则拒绝写入。

类似:

"posts": {
  ".write": "auth !== null && posts.post.userId.val() === auth.uid
}

或者类似的东西

//where posts.post represents objectRef.objectUpdating
"rules":{
  ".write": " auth !== null && posts.post.userId.val() === auth.uid
}

posts.postid 对象更新如下:

firebase.database().ref('posts').child(id).update(updates)

这是帖子外观的示例。主题和论坛遵循相同的结构。

  "posts": {
    "-KsjWehQ--apjDBwSBCZ": {
      "edited": {
        "at": 1504037546,
        "by": "ALXhxjwgY9PinwNGHpfai6OWyDu2"
      },
      "publishedAt": 1504035908,
      "text": "some post text",
      "threadId": "-KsjWehQ--apjDBwSBCY",
      "userId": "ALXhxjwgY9PinwNGHpfai6OWyDu2"
    },
    ...{post2},
    ...{etc}
  }

希望这是有道理的,任何帮助表示赞赏。

编辑: 我明白了,所以当调用 update 或 create 方法时,它会检查 data.userId 是否与 auth.uid 相同。现在可以更新现有数据了。

虽然 create 方法不起作用,但如果 userId 与 auth.uid 不匹配,我不确定如何允许 update() 像以前一样创建新帖子或线程而不更新。

以下规则:

{
  "rules": {
    ".read": true,
    "users": {
      ".indexOn": ["usernameLower", "email"],
      "$user": {
        ".write": "auth !== null && $user === auth.uid"
      }
    },
    "forums": {
      ".write": false
    },
    "categories": {
      ".write":false
    },
    "$resource": {
      "$child": {
        ".write": "(auth !== null && auth.uid === data.child('userId').val()) || data.val() === null"
      }
    }
  }
}

【问题讨论】:

    标签: javascript firebase vue.js firebase-realtime-database firebase-authentication


    【解决方案1】:

    我设法让它工作。 以帖子为例:

    "posts":{
      "$post":{
          ".write": "(auth !== null && auth.uid === data.child('userId').val()) || (!data.exists() && auth !== null)"
       }
    }
    

    如果有更好的方法可以做到这一点,或者如果我是多余的或忽略了某些事情,请随时发表评论并告诉我,我还是 firebase 的新手,任何意见都会受到赞赏。

    作为参考,我在下面包含了我的规则。 我不清楚的一件事是我是否必须在我的其他写入规则下方将“.write”设置为 false 以防止写入任何其他数据,或者是否默认处理。

    {
      "rules": {
        ".read": true,
        "users": {
          ".indexOn": ["usernameLower", "email"],
          "$user": {
            ".write": "auth !== null && $user === auth.uid"
          }
        },
        "categories": {
          ".write": false
        },
        "forums": {
          "$forum":{
            "lastPostId": {
              ".write": "auth !== null"
            },
            "threads": {
              ".write": "auth !== null"
            }
          }
        },
        "posts": {
          "$post": {
            ".write": "(auth !== null && auth.uid === data.child('userId').val()) || (!data.exists() && auth !== null)"
          }
        },
        "threads": {
          "$thread": {
            "$child":{
              ".write": "auth !== null"
            },
            ".write": "(auth !== null && auth.uid === data.child('userId').val()) || (!data.exists() && auth !== null)"
          }
        },
        "$resource": {
          "$child": {
            "$child2": {
              ".write": "auth !== null"
            },
            "userId": {
              ".write": "!data.exists()"
            }
          }
        }
      }
    }
    

    作为参考,这些是传递给 update() 以在数据库中创建新帖子的更新:

    const updates = {}
            updates[`posts/${postId}`] = post
            updates[`threads/${post.threadId}/posts/${postId}`] = postId
            updates[`threads/${post.threadId}/contributors/${post.userId}`] = post.userId
            updates[`users/${post.userId}/posts/${postId}`] = postId
    
    firebase.database().ref().update(updates)
    
    

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 2019-11-14
      • 2021-05-12
      • 1970-01-01
      • 2017-09-16
      • 2021-08-16
      • 2019-12-17
      • 2021-10-22
      • 2016-10-19
      相关资源
      最近更新 更多