Garren Fitzenreiter
Garren Fitzenreiter

Reputation: 839

Seperate firebase database rules for parent and child

My database is structured like so...

Database structure

I want to have different read/write rules for the "users/uid" and "users/uid/locations/$address/latlng" node.

    "users": {
    "$uid": {

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

      "locations": {
        "$address": {
          "latlng": {

            ".read": true,
            ".write": false,

          }
        }
      }
    }
  }

Due to the rules cascade, it seems like this is not possible. The read and write are both passing where I want it to fail. Basically, I want the user to be able to create an address and latlng node under the locations node, but only let certain users create additional nodes under the address node.

Is this possible or will I need to structure my database differently?

Edit:

Here is my code for storing the info to the database

   mRootRef.child("users").child(user.getUid()).child("locations").child(currentAddress).child("latlng").setValue(currentLatLng.latitude + "," + currentLatLng.longitude);

Here is my code for retrieving the info from the database

mRootRef.child("users").child(user.getUid()).child("locations").child(selectedItem).child("verified").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                        //Check if address is verified

                        if(dataSnapshot.getValue() == null){

                            ChangeVerifiedMessage(false);
                            return;

                        }

                        if (dataSnapshot.exists()){

                            ChangeVerifiedMessage(true);

                            }else{

                            ChangeVerifiedMessage(false);

                            }

                        }

Upvotes: 1

Views: 789

Answers (1)

Thomas
Thomas

Reputation: 2560

You can try to use validation to achieve what you want. You can validate, that the children (in your case latlng) do not exist in specific cases. Validations are not cascading, so you can just replace your write rules with it. It may be a little workaround, but the only idea that comes to my mind without restructuring your data.

Upvotes: 1

Related Questions