简体   繁体   English

权限被拒绝,在Firebase中检索数据

[英]Permission denied, retrieving data in firebase

I'm trying to read all my devices information with a specific owner, and put a rule that the users can only get the device information if they are the owner. 我试图与特定所有者一起读取我的所有设备信息,并制定了一条规则,即用户只有拥有者才能获得设备信息。

So I setted up my rules like this in Firebase 所以我在Firebase中设置了这样的规则

{
  "rules": {   
     "devices": { 
        "$devices": {
            ".read": "data.child('owner').val() === auth.uid",          
            ".write": "auth != null"
        }
    }
}

For example my DB in firebase is like this : 例如我在firebase中的数据库是这样的:

{
  "devices" : {
    "SN1" : {

      "owner" : "Pl2YaoakaoPLeOowwm8RSFE5",
       [...]
    },
    "SN2" : {

      "owner" : "Pl2YaoakaoPLeOowwm8RSFE5",
      [...]
    },
    "SN3" : {

      "owner" : "KL2YaoaploPLeOoabm8KDFE4",
      [...]
    }
  }
}

I'm trying getting these information by doing this : 我正在尝试通过执行以下操作获取这些信息:

FirebaseUser user = firebaseAuth.getCurrentUser();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://NAME.firebaseio.com/");
        rootRef.child("devices").orderByChild("owner").equalTo(user.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                System.out.println(snapshot.getValue());
                Log.v("TAG", "value Device : " + snapshot.getValue());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.v("TAG","The read failed: " + databaseError.getMessage());
            }
        });

I should expect SN1 and SN2 devices information, but instead I get a permission denied : Listen at /device failed: DatabaseError: Permission denied. 我应该期待SN1和SN2设备信息,但是我却获得了拒绝权限:在/ device上侦听失败:DatabaseError:权限被拒绝。
Seems that he is trying getting all devices (not allowed by rules) and then locally perform a filter on it. 似乎他正在尝试获取所有设备(规则不允许),然后在本地对其执行过滤器。
If I directly retrieving SN1 data like this : rootRef.child("devices").child("SN1") , it's working. 如果我像这样直接检索SN1数据: rootRef.child("devices").child("SN1") ,它就可以正常工作。

what am I doing wrong ? 我究竟做错了什么 ?

Its Easy Simply Try these Rules.! 简单,只需尝试以下规则。

  {
  "rules": {
    "users": {
      "$user_id": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($user_id)
        ".write": "$user_id === auth.uid"
      }
    }
  }
}


{
  "rules": {
    "users": {
      "$uid": {
        ".read": "auth != null && auth.uid == $uid"
      }
    }
  }
}

And In Fire base Thread 并在Fire base线程中

 private FirebaseAuth mAuth;
 public String id;

            mAuth = FirebaseAuth.getInstance();
            DatabaseReference rootRef = 
            FirebaseDatabase.getInstance().getReference();

             id = mAuth.getCurrentUser().getUid();


     rootRef.child("devices").addValueEventListener(new ValueEventListener() 
     {
     @Override
     public void onDataChange(DataSnapshot snapshot) {


     for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

     String key=dataSnapshot.getKey();






   rootRef.child("devices").child(key).orderByChild("owner")
   .equalTo(id).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {

     System.out.println(snapshot.getValue());
     Log.v("TAG", "value Device : " + snapshot.getValue());
                    }//end of for each loop
    }//end of data change

     @Override
     public void onCancelled(DatabaseError databaseError) {
                        Log.v("TAG","The read failed: " + 
     databaseError.getMessage());
                    }
                });
{
  "rules": {   
     "devices": { 
        "$devices": {
            ".read": true,          
            ".write": true
        }
    }
}

Try these . 试试这些。

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

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