【发布时间】: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