【发布时间】:2017-01-28 20:35:09
【问题描述】:
我正在使用 Firebase 规则来设置权限,但在设置规则以允许写入权限以包括用户和任何管理员时遇到问题。
下面是我尝试设置权限的数据库:
'rules': {
'rankings': {
'user': {
'userShortenedAuthId': { //user's rankings },
'anotherShortenedAuthId': { //another user's rankings }
}
},
'admin': {
'adminAuthId': true
},
'users': {
'userAuthId': {
'rank': 'userShortenedAuthId'
},
'anotherAuthId': {
'rank': 'anotherShortenedAuthId'
}
}
}
这些是这些数据的规则:
"rankings":{
"user":{
"$rankId": {
".read": true,
".write": "auth != null && ((root.child('users/' + auth.uid + '/rank').val() == $rankId) || root.child('admin/' + auth.uid).exists())"
}
}
}
我在以管理员身份登录时尝试写入“rankings/user/userShortenedId”,但我从 Firebase 收到权限被拒绝错误。 (以用户身份登录就可以了)。错误出现在“rankings/user/$rankId/.write”规则中。让我感到困惑的是,我一直在其他地方使用 'root.child('admin/' + auth.uid).exists())' 规则来获得管理员权限,而且这似乎也可以正常工作。
这是产生权限错误的代码。
firebase.database().ref('rankings/' + userShortenedAuthId).update({newPlayerKey:totalPlayers}))
.then(function(){
console.log('successfully updated user ranking');
})
.catch(function(err){
console.log('error updated user ranking', err);
});
【问题讨论】:
-
您缺少顶级
"rules"节点。那是否存在于您的真实规则中(并且只是从您发布的 sn-p 中丢失)?另外:你能显示重现写入失败的最小代码吗? -
@FrankvanPuffelen 顶级“规则”已经到位,只是不包括在此处。我将在上面添加导致失败的代码。
-
@FrankvanPuffelen NVM!我意识到我正在尝试访问错误的 firebase ref URL。它应该是 'rankings/user/' + userShortenedAuthId。
标签: firebase firebase-realtime-database firebase-security