如何在Firebase数据库中创建节点后阻止访问更改?

时间:2017-05-23 04:52:49

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

我有以下情况,如果节点已经存在,我怎么能拒绝更新(覆盖)节点?例如,添加到朋友的请求,我希望这个功能执行一次,因为它在数据库中设置规则,但它们不起作用。如何解决这个问题?

规则

// friends 
      "friends": {
        "$ownerID": {
          "friendIncomingRequests": {
            "$secondUserID": {
              ".write": "!data.exists()" // Only allow new nominations to be created
            }
          },
          "friendOutgoingRequests": {
             "$secondUserID": {
              ".write": "!data.exists()" // Only allow new nominations to be created
            }
          }
        }
      }

数据

  "friends" : {
    "8OdvaGQfMVdJrlCxdc5pOaj09hy2" : {
      "friendOutgoingRequests" : {
        "mp9pfsfVQKavwYddjYYPC5Ja9N93" : {
          "timeStamp" : 1.495514876872129E9,
          "userID" : "mp9pfsfVQKavwYddjYYPC5Ja9N93",
          "userName" : "Tim C."
        }
      }
    },
    "mp9pfsfVQKavwYddjYYPC5Ja9N93" : {
      "friendIncomingRequests" : {
        "8OdvaGQfMVdJrlCxdc5pOaj09hy2" : {
          "senderID" : "8OdvaGQfMVdJrlCxdc5pOaj09hy2",
          "senderName" : "Alexsander K.",
          "timeStamp" : 1.495514876872129E9
        }
      }
    }
  },

更新 我认为问题在于此代码,因为我在规则中也有这个代码。但我该如何解决呢?

"rules": {
    ".read": "auth != null",
    ".write": "auth != null",
 }

更新1 :以下是所有规则。我只需要在朋友中制定一个特定的写规则(更新)。我看到了他们规则的每个单独分支的示例,但如果我需要为一个分支执行一些特定的规则,对于数据库的其余部分,您需要标准规则我应该如何做得更好?

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",



      // card location
    "cardLocation": {
      // Allow anyone to read the GeoFire index
      //".read": true,
      // Index each location's geohash for faster querying
         ".indexOn": "g",

     },

      "cards": {
        ".indexOn": "ownerID"
      },

      "userListEvents": {
          "$uid": {
            ".indexOn": "isConfirmed"
          }
      },

      "userImages": {
        "$uid": {
          "userProfileImages": {
                            ".indexOn": "isoDate"
          }
        }
      },

      // tags 
      "userTags": {
        "$uid": {
          ".indexOn": "isSelected"
        }
      },


        // people search
        // 
        "userLocations": {
          ".indexOn": "g"
        },


      // friends 
      "friends": {
        "$ownerID": {
          "friendIncomingRequests": {
            "$secondUserID": {
              ".write": "!data.exists()" 
            }
          },
          "friendOutgoingRequests": {
             "$secondUserID": {
              ".write": "!data.exists()" 
            }
          }
        }
      }

  }
}

1 个答案:

答案 0 :(得分:1)

  

我认为问题在于此代码,因为我在规则中也有这个代码。但我该如何解决呢?

"rules": {
    ".read": "auth != null",
    ".write": "auth != null",
 }

是的,你的想法是正确的。 Firebase .read.write规则级联。因此,您应该将每个.read.write放在数据结构的每个子节点上。这样的事情:

{
  "rules": {
  //skip this
    "someNode": {
        ".read": "auth != null",
        ".write": "auth != null"
    },
    // friends 
    "friends": {
      "$ownerID": {
        "friendIncomingRequests": {
          "$secondUserID": {
           ".write": "!data.exists()" 
        }
      },
        "friendOutgoingRequests": {
          "$secondUserID": {
            ".write": "!data.exists()" 
          }
        }
      }
    }
    //...
  }
}