0

I have no Idea, why Setvalue() works with map object(static data) and throws permission denied as exception when using plain java object with dynamic data(POJO).

Works: Saving data using Map object

Map newUserData = new HashMap();
            newUserData.put("Name", getUserName());
            newUserData.put("Surname",getUserName()+"_testSurname");
            newUserData.put("Gender","Gender_data");
            newUserData.put("PhotoURL","");

adding metadata failed com.google.firebase.database.DatabaseException: Firebase Database error: Permission denied:

Saving data using POJO

public class MetaInfo {

private String Name, Surname,Gender,PhotoURL;

public MetaInfo() {}

public MetaInfo(String name, String surname, String gender, String photoURL) {
    Name = name;
    Surname = surname;
    Gender = gender;
    PhotoURL= photoURL;
}

public String getPhotoURL() {
    return PhotoURL;
}
...
...
..

Adding data using MetaInfo object:

MetaInfo mData= new MetaInfo(name.getText().toString(),
                surname.getText().toString(),
                gender.getText().toString(),
                "");

Calling setvalue() with map object works however, permission deined with when MetaInfo object with valid data is used.

DatabaseReference.child(getUid()).child("metadata").setValue(mData).addOn CompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    Log.i(TAG,"metadata added ");
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.i(TAG,"adding metadata failed "+e.toString());
                }
            });
                @Override

FB-rules

 rules": {
  "test":{
    "$UID":{
      // METADATA
      "metadata":{
        ".read":true,
        ".write":"auth.uid === $UID",
        ".validate":"newData.hasChildren(['Name','Surname'])",
        "Name":{
        ".validate":"newData.val()!== null"
        },
        "Surname":{

        ".validate":"newData.val()!== null"
        },
        "$other":{
        ".validate": true
        }
      }
user755
  • 2,521
  • 3
  • 18
  • 29

2 Answers2

0

In my opinion the problem is with your google-services.json file.

Make sure your application name and client id are same as in Firebase console.

If you are not sure, please re-download google-services.json file from your project's console and add it your project.

An also don't forget to set the rules to:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Please keep this rules only for testing purposes. Don't keep this rules always like this.

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

You skipped a few crucial pieces of your MetaInfo class. But I'll assume you have these getters:

public String getName() { return Name; }
public String getSurname() { return Surname; }

These getters map to (follow age-old JavaBean conventions) properties named name and surname. But your rules validate that any new data has Name and Surname. It might be hard to spot at first, but the casing is different.

The solution is to annotate the properties with how you want them stored:

@PropertyName("Name")
public String getName() { return Name; }
@PropertyName("Surname")
public String getSurname() { return Surname; }

If you have corresponding setters for the properties, you'll also need to annotate those.

The easiest way to troubleshoot these problems by the way is to initially write to a location in your database where everything is allowed. That way you can see what the Firebase SDK writes your POJO as, which makes it a lot easier to spot what validation rules it violates.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I do have getter and setter, after applying your suggetion to annotate properties, it get runtime erro `Found setter with invalid case-sensitive name: setName` while reading data, havent test writing yet. – user755 Aug 10 '17 at 16:05
  • Please update your question with the minimum code that reproduces the problem. – Frank van Puffelen Aug 10 '17 at 18:53