2016-11-21 61 views
1

請考慮以下用戶的Firebase數據庫規則。我想知道是否有辦法編寫一個可以一個一個地刪除孩子(朋友)的規則,但不是一次全部刪除?Firebase規則允許兒童逐一刪除

在這裏,一個簡單的刪除操作將不起作用,因爲朋友需要是空的或有孩子。但是,它可以更新爲擁有任何其他孩子,這意味着所有孩子都可以立即被覆蓋。

"user": 
{ 
    "$uid": 
    { 
     /* user can read all user data */ 
     ".read": "$uid == auth.uid", 
     /* allow to add to friends but not delete */ 
     ".write": "$uid == auth.uid && (!data.child('friends').exists() || newData.child('friends').hasChildren())", 
     /* other user data also needs to be writable */ 
     "name": {}, 
     /* only explicit user data is allowed */ 
     "$other": { ".validate": false }, 
     "friends": 
     { 
      "$friend_uid": { ".validate": "root.child('user').child($friend_uid).exists()" }, 
     }, 
    } 
} 

回答

0

根據firebase策略,.write策略是由子項繼承的,所以解決方法是給每個子項寫規則。

"user": 
{ 
    "$uid": 
    { 
     /* user can read all user data */ 
     ".read": "$uid == auth.uid", 
     /* no writing at this level */ 
     /* ".write": false, */ 
     /* user data is still writable */ 
     "name": { ".write":"$uid == auth.uid" }, 
     /* no longer necessary */ 
     /*"$other": { ".validate": false },*/ 
     "friends": 
     { 
      "$friend_uid": { ".write":"$uid == auth.uid" }, 
     }, 
    } 
}