我有这样的结构:
{ "users": {
{
"uid1" : {
"admin" : true,
"email" : "admin@app.com",
"name" : "Admin User"
},
"uid2" : {
"email" : "user1@app.com",
"name" : "User 1"
},
"uid3" : {
"email" : "user2@app.com",
"name" : "User 2"
}
}
}
我有以下规则,允许管理员或用户自己阅读一个用户的数据并按预期工作:
{
"users": {
".read": true,
"$uid": {
// data is the user's or user is admin
".read": "root.child('users').child(auth.uid).child('admin').val() == true || auth.uid == $uid",
}
}
}
除了规则允许的内容之外,我还需要做什么,让管理员用户能够列出集合中的所有用户?
答案 0 :(得分:4)
使用Firebase,一旦您授予权限,就无法在树中向下撤消它。因此,".read": true
下的users
会授予所有用户读取权限。
您需要更改users
下的规则,以便仅为管理员true
。这样做还将允许管理员列出用户(并且还应该看到其他规则影响您期望的行为):
{
"users": {
".read": "root.child('users').child(auth.uid).child('admin').val() === true",
"$uid": {
".read": "root.child('users').child(auth.uid).child('admin').val() === true || auth.uid === $uid",
}
}
}
这可以简化 - 正如评论中所提到的那样 - 因为users
下的规则级联下来并且不需要重复:
{
"users": {
".read": "root.child('users').child(auth.uid).child('admin').val() === true",
"$uid": {
".read": "auth.uid === $uid"
}
}
}