2016-02-12 108 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,我希望這已經夠了,而且很清楚。如果它不起作用,我建議分別測試每個規則以查看哪個引起了一個問題,所以我(或其他人)可以幫助解決它。