2016-02-12 96 views
0

我在Firebase中有一个名为“pending_members”的字段,其中包含等待“所有者”授予待批准权限的成员列表,因此,“pending_members”需要以下内容规则:向Firebase添加安全措施以防止插入其他数据

  1. 当前用户只能从列表
  2. 的“所有者”添加本身(UID)
  3. 当前用户可仅去除本身(UID)可以从列表中删除任何构件
  4. 只有“所有者”才能阅读清单

我已经尝试了各种安全规则,但似乎错过了很多角落案例,例如,用户被给予写访问权限,因为数据包含他们的用户身份,但他们可以随后提交其他用户的uid。

任何人都可以针对这种情况提出适当的规则吗?非常感谢

"pending_members" : { 
    ".write" : "auth !== null && 
     // The user is authenticated AND 
     (newData.child(auth.uid).exists() || 
     // The new data contains either the current user's id OR 
     (!newData.exists() && 
     // There's no new data (a delete operation) AND 
     data === auth.uid))", 
     // The old data is the current user's id 

"$member" : { 
    ".validate" : "newData.isString()", 
     "$other": { ".write": false, ".read": false } 
    } 
} 

编辑: 结构示例:

users  -> 
       personal_data -> 
            email     (user email address) 
            first_name    (user first name) 
            last_name    (user last name) 
       networks_index -> 
networks -> 
       members     (list of uids of users linked to the network) 
       owner     (uid of the owner/primary user) 
       pending_members   (list of uids of users wishing to link to the network) 

Data Example (image)

+1

你可以添加一个firebase结构的例子吗? –

+0

@AndréKool按要求添加。 –

回答

0

复杂的结构,你有,但我会试试看: 记住标准值进行读,写是错误的。

{ 
    "rules": { 
    "networks": { 
     "$networkid": { 
//Give read and write access to the owner of the network 
     ".read": "auth != null && "root.child('networks').child($networkid).child('owner').val() == auth.uid", 
     ".write": "auth != null && "root.child('networks').child($networkid).child('owner').val() == auth.uid", 
     "pending_members": { 
      "$uid": { 
       //Give members write access to their own node inside pending_members 
       ".write": "auth != null && auth.uid == $uid", 
       //Use validate to check if the value is a bool or emty(removal) 
       ".validate": newData.isBoolean() || !newData.exists() 
     } 
    } 
    } 
} 

我只专注于这里的pending_members,我希望这已经够了,而且很清楚。如果它不起作用,我建议分别测试每个规则以查看哪个引起了一个问题,所以我(或其他人)可以帮助解决它。