2016-09-06 75 views
2

我有这样的结构:火力地堡:允许管理员和特定记录用户阅读收集

{ "users": { 
    { 
    "uid1" : { 
     "admin" : true, 
     "email" : "[email protected]", 
     "name" : "Admin User" 
    }, 
    "uid2" : { 
     "email" : "[email protected]", 
     "name" : "User 1" 
    }, 
    "uid3" : { 
     "email" : "[email protected]", 
     "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", 
    } 
    } 
} 

什么我需要做的,除了什么规则已经允许,有管理员的用户能够列出集合中的所有用户?

回答

4

有了Firebase,一旦您授予了权限,您就无法在树的下方撤销它。因此,".read": trueusers下授予所有用户的读取权限。

您需要更改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" 
    } 
    } 
} 
+2

由于管理员的读取权限已经级联,因此您不需要重复'$ uid'下的管理员权限。该规则可以是“.read”:“auth.uid === $ uid”' –

+0

好!看起来我误解了文档。工作正常,谢谢! –