简体   繁体   English

规则实时数据库

[英]Rules realtime database

I want to create a rule, which when one login account reads and writes to the database, can you help me, and is there something wrong with my code?我想创建一个规则,当一个登录帐户读取和写入数据库时,你能帮我吗,我的代码有问题吗?

this my rules database这是我的规则数据库

{
  "rules": {
    "User": {
      "$uid": {
     ".read": "auth !== null && auth.uid === $uid",
        ".write": "auth !== null && auth.uid === $uid"
        }
    },
      "Layanan Nasabah": {
       ".read": "root.child('User/' + auth.uid).exists()",
        ".write": "root.child('User/' + auth.uid).exists()"
      }
    }
}

this my codes这是我的代码

database.child("Layanan Nasabah").child(mAuth.getUid()).setValue(new pengaduan
            (getNo,getNo_Tiket,getNama_Penelpon,getPenelpon1,getPenelpon2,getEmail,getPertanyaan,getTertanggung,getNo_Polis,getObyek,getCatatan)).addOnSuccessListener(unused -> {
        Toast.makeText(TambahActivity.this, "Data Berhasil Disimpan", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(TambahActivity.this, OnprogressActivity.class));
        finish();

    }).addOnFailureListener(e ->
        Toast.makeText(TambahActivity.this, "Data Gagal Disimpan", Toast.LENGTH_SHORT).show());
}

From what I understand you want only logged in users (authenticated users) to read/write to nodes in your database.据我了解,您只希望登录用户(经过身份验证的用户)读取/写入数据库中的节点。

This rule:这条规则:

    ".write": "auth !== null && auth.uid === $uid"

Means that any authenticated user can only write to his own uid.意味着任何经过身份验证的用户只能写入他自己的uid。

In users node, you already did what you are asking for:users节点中,您已经完成了您要求的操作:

//because this means any authenticated user can read and write their 
//own nodes (UIDs)

"User": {
  "$uid": {
    ".read": "auth !== null && auth.uid === $uid",
    ".write": "auth !== null && auth.uid === $uid"
    }
}

UPDATE更新

If you also want that authenticated users only can read and write to Layanan Nasabah node, you can do this:如果您还希望经过身份验证的用户只能读取和写入Layanan Nasabah节点,您可以这样做:

"Layanan Nasabah": {

 "$uid":{

   ".read": "root.child('User/' + auth.uid).exists() && auth !== null && auth.uid === $uid",

   ".write": "root.child('User/' + auth.uid).exists() && auth !== null && auth.uid === $uid"

 }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM