2017-08-06 91 views
0

我有一個電影應用程序。人們可以評論一部電影。firebase刪除數據屬於用戶

我檢查:

Firebase Security & Rules, How can I let users delete their own data?

https://www.firebase.com/docs/security/guide/securing-data.html#section-data-variables

https://www.firebase.com/docs/security/guide/user-security.html

但我couldent找到我的答案。

comments { 
    550: { 
    UKs5lg...: { 
      comment: "this is comment", 
      userId: "Jkulsç122doasdjk" 
    }, 
    WfyG5Hsl...: { 
      comment: "this is comment2", 
      userId: "Jkulsç122doasdjk" 
    } 
}, 
1694: { 
    Ki5Uydmk...: { 
      comment: "movie 2 comment 1", 
      userId: "Jkulsç122doasdjk" 
    }, 
    Kovx9ox...: { 
      comment: "movie 2 comment2", 
      userId: "Jkulsç122doasdjk" 
    } 
} 

} 

在數據庫中,像550,1694等數字是電影的ID號碼。如你所見,每部電影都有獨特的評論。每條評論都有評論和userId(發送此評論的用戶的uid)屬性。我刪除評論,但我想,用戶可以刪除只是自己的評論。所以我想設置像這樣的規則:

{ 
    "rules": { 
    "comments": { 
     "$movieId": { 
       ".read": true, 
       ".write": ??????, 
     } 
    } 
} 
} 

我應該寫什麼安全規則而不是問號?我想寫就像;

".write" : "!data.exists() || (!newData.exists() && data.child(blabla+ 'userId').val() === auth.uid)" 

但我不能寫,因爲我不知道應該如何得到獨特的身份證作爲一個孩子。

回答

2

您需要將規則下推到您要強制執行操作的級別。既然你說每個用戶應該只能刪除他們自己的評論,那麼強制執行這條規則應該在評論上:

{ 
    "rules": { 
    "comments": { 
     ".read": true, 
     "$movieId": { 
     "$commentId": { 
      ".write": "!data.exists() || (!newData.exists() && data.child('userId').val() === auth.uid)", 
     } 
     } 
    } 
    } 
} 
+0

謝謝sooo多。我實際上嘗試過這樣的事情,它以前沒有工作。但它現在起作用。我想我在某個地方犯了一個錯誤。再次坦率地謝謝你。 – escalepion