Search code examples
androidfirebase-realtime-databasefirebase-authenticationfirebase-security

how can I read and write as an admin even if the rules set to false in Firebase Realtime Database


As an admin I am trying to get the users data so that I could 'read' and 'write' even through the rules of the firebase 'read','write' set to be false.

users
  |
  "uid"
    |-firstname: "xyz"
    |-lastname: "xyz"
    |-email: "xyz@gmail.com"
    

Here, is my rules

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

when I am retrieving users data from firebase using FirebaseRecyclerOptions, users data display only when I set rules read and write to true but I want even if the rules set to be false through admin I could read and write. Is this possible

  FirebaseRecyclerOptions<Model> options =
            new FirebaseRecyclerOptions.Builder<Model>()
                    .setQuery(FirebaseDatabase.getInstance().getReference("Users"), Model.class)
                    .build();

Solution

  • Add a boolean to your user object and call it admin, set it false for all the users except you.

    Now copy that to your firebasee rules:

    "users": {      
       ".read": 
          "root.child('users').child($uid).child('Admin').val()",            
       ".write":"  
          "root.child('users').child($uid).child('Admin').val()"
    
     ,"$uid": {
        ".read": "auth != null && $uid === auth.uid",
            ".write": "auth != null && $uid === auth.uid"
            }
       }
    
    }                
    }
    

    1

    Just like the isActivated boolean I have in my Users you can create a boolean called isAdmin.Add it to your admin class , give it set and get methods in your class.When you upload it to firebase you can read and write this data. Then let your firebase rules to do the job.