我的 firebase
中存储了以下数据:
firebaseRoot
admins
simplelogin:1:
users
simplelogin:1
email: abc@xyz.com
picture: csd
provider: password
uid: simplelogin:1
simplelogin:2
email: abc1@xyz.com
picture: zsd
provider: password
uid: simplelogin:1
以及以下安全规则:
{
"rules": {
"admins": {
".read": "root.child('admins').child(auth.uid).val() === true",
".write": "root.child('admins').child(auth.uid).val() === true"
},
"users": {
"$user":{
".read": "$user === auth.id || root.child('admins').child(auth.uid).val() === true",
".write": "$user === auth.id"
}
}
}
}
我的授权要求如下。
- 管理员只能由现有管理员读取和添加。这有效。
- 管理员可以读取所有用户,但不能写入用户数据。
- 用户可以读取和更新自己的用户数据。
目前,根据上述规则,我无法读取管理员和登录用户的用户数据。我收到以下错误消息。请提供您的帮助。谢谢。
var rootRef = new Firebase('https://xxxxx.firebaseio.com/');
var users = rootRef.child('users');
users.on('value', function(snap) {
console.log(snap.key(), snap.val());
}, function(error) {
console.log(error);
});
错误:
Error: permission_denied: Client doesn't have permission to access the desired data.
最佳答案
Firebase 安全规则有两个陷阱:
-
这意味着一旦您向某人授予 JSON 结构中某个级别的(读或写)访问权限,您就无法再在较低级别上立即获取该访问权限
-
这意味着只有当您对节点中的所有数据具有读取访问权限时,您才能读取该节点。如果您只有部分数据的读取权限,则完整数据的读取操作将会失败。
在您的安全规则中,您仅授予读取 users
下(部分)子项的权限。因此尝试读取整个 users.on('value'
将会失败。
您可以通过向管理员授予 .read
访问 users
节点的权限来解决此问题。
"users": {
".read": "root.child('admins').child(auth.uid).val() === true",
"$user":{
".read": "$user === auth.id",
".write": "$user === auth.id"
}
}
关于firebase 安全权限不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30732705/