2016-11-21 6 views
1

次のfirebaseデータベースルールを考慮してください。子供(友達)を一つずつ削除できるルールを書く方法があるのだろうか?子供を1人ずつ削除できるようにする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" }, 
     }, 
    } 
} 
関連する問題