Authentication Firebase用户读取所有数据,写入新数据,但仅修改自己的数据
我有Firebase的下一个场景: 有两件事,用户和事件 我希望用户能够创建新的事件,查看所有现有事件,但只能修改他们创建的事件 这意味着UserOne创建了EventOne,可以查看EventOne和EventTwo,但只能修改EventOne 我的结构如下:Authentication Firebase用户读取所有数据,写入新数据,但仅修改自己的数据,authentication,firebase,rules,Authentication,Firebase,Rules,我有Firebase的下一个场景: 有两件事,用户和事件 我希望用户能够创建新的事件,查看所有现有事件,但只能修改他们创建的事件 这意味着UserOne创建了EventOne,可以查看EventOne和EventTwo,但只能修改EventOne 我的结构如下: -events -eventOne endTime: id: name: providerId: startTime: userId: -eventTwo endT
-events
-eventOne
endTime:
id:
name:
providerId:
startTime:
userId:
-eventTwo
endTime:
id:
name:
providerId:
startTime:
userId:
-users
-userOne
-userTwo
我目前的规则基本上是默认的:
{
"rules": {
"users": {
"$uid": {
// grants write access to the owner of this user account whose uid must exactly match the key ($uid)
".write": "auth !== null && auth.uid === $uid",
// grants read access to any user who is logged in with an email and password
".read": "auth != null && auth.uid == $uid"
}
},
"events": {
".read": "auth != null",
".write": "auth != null"
}
}
}
提前感谢。为了确保所有用户都能看到所有事件,您可以将事件的读取规则设置为
true
或auth!=null
,如果您希望要求读卡器至少经过身份验证,则使用后者
要确保事件只能由其创建者修改,请将原始作者记录在事件旁边,并使用auth.uid
对其进行验证
{
"rules": {
"events": {
"$eventid": {
".read": "auth != null",
".write": "auth != null && (!data.exists() || data.child('author').val() === auth.uid) && newData.hasChild('author')",
"author": {
".write": "newData.val() === auth.uid"
}
}
}
}
}
在上面,我们使用
!data.exists()
检查此位置是否存在现有事件。如果不存在事件,任何人都可以在此位置添加事件。接下来,data.child('author').val()==auth.uid
确保如果事件存在,只有作者可以修改它。感谢您的快速回答!,我只需要在“规则”下面添加“.read:“auth!=null”
,以确保它们可以被读取。如果我在中设置newData.hasChild('author')
。.validate
?@vzhen,只要您也在验证“author”的值是否与您想要的值相匹配(即auth令牌中的某个内容),这一切都是一样的@vzhen,这真的不一样。