【问题标题】:Firebase rule allowing children to be deleted only one by oneFirebase 规则只允许一个一个地删除子级
【发布时间】:2016-11-21 15:02:07
【问题描述】:

考虑以下适用于用户的 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()" },
        },
    }
}

【问题讨论】:

    标签: json firebase firebase-realtime-database firebase-security


    【解决方案1】:

    根据firebase策略,.write策略是由children继承的,所以解决方案是.write rules给每个children。

    "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" },
            },
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2018-04-24
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多