需要在Firebase DB中添加规则

时间:2016-10-11 20:09:05

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

我想添加一条规则,将可以保存在no_of_orders字段中的max int限制为2OrderSchedule文档数据是通过我正在开发的应用程序预先填充的。

以下是jsonExport数据。

    {
  "OrderSchedule" : {
    "13-Oct-2016" : {
      "13:00 - 14:00" : {
        "no_of_orders" : 4
      }
    }
  },
  "Orders" : {
    "-KTpIn4HeuDBDliCnTSA" : {
      "car_Model" : "Van",
      "date_Created" : 1476212105,
      "date_Scheduled" : "13-Oct-2016",
      "service_Type" : "Shine",
      "status" : "Scheduled",
      "time_Scheduled" : "13:00 - 14:00",
      "uid" : "sms|57f7b267e618d33da23e65ce"
    },
    "-KTpJ6BREc695pv4Ue6s" : {
      "car_Model" : "SUV",
      "date_Created" : 1476212188,
      "date_Scheduled" : "13-Oct-2016",
      "service_Type" : "Detailed",
      "status" : "Scheduled",
      "time_Scheduled" : "13:00 - 14:00",
      "uid" : "sms|57f7b267e618d33da23e65ce"
  },
  "users" : {
    "sms|57f7b267e618d33da23e65ce" : {
      "created_at" : 1475868493,
      "email" : "balouza@hotmail.com",
      "phone" : "+0000000000",
      "username" : "balouza"
    },
    "sms|57fa9701e618d33da240efa5" : {
      "created_at" : 1476040501,
      "email" : "look@hotmail.com",
      "phone" : "+00000000000",
      "username" : "bolzo"
    }
  }
}

安全规则:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
      "max": {
        ".write": "auth != null"
      },
        ".OrderSchedule": {
          "$date": {
            "$timeSlot": {
              "no_of_orders": {
                ".validate": "newData.val() <= root.child('max').val()"
              }
            }
          }
        }
   }
}

Screenshot of firebase db

1 个答案:

答案 0 :(得分:1)

在数据库中保存父节点{ max : 2, "OrderSchedule" : { "13-Oct-2016" : { "13:00 - 14:00" : { "no_of_orders" : 4 } } }, "Orders" : { "-KTpIn4HeuDBDliCnTSA" : { "car_Model" : "Van", "date_Created" : 1476212105, "date_Scheduled" : "13-Oct-2016", "service_Type" : "Shine", "status" : "Scheduled", "time_Scheduled" : "13:00 - 14:00", "uid" : "sms|57f7b267e618d33da23e65ce" }, ... } 对。

  {
  "rules": {
   "max" : {
     ".write" : "false"
     },
    "OrderSchedule": {
      "$date":{
       "$timeInterval":{
          "no_of_orders":{
            ".validate": "newData.val() <= root.child('max').val()"
          } 
       }
     }
    }
  }
}

在您的安全规则中,只需添加: -

{
  "rules": {
    ".read": "auth != null", 
    ".write": "auth != null",
    "max" : {
     ".write" : "false"
     }, 
    "OrderSchedule": {
      "$date":{
       "$timeInterval":{
          "no_of_orders":{
            ".validate": "newData.val() <= root.child('max').val()"
          } 
       }
     }
    }
  }
}

如果您只想在数据库中读取和写入经过身份验证的用户,请使用以下命令: -

var isDebuggerAttached = System.Diagnostics.Debugger.IsAttached;