Solution 1 :
Security rules don’t filter data. So that means that in your current data model you indeed have to perform two read operations.
The common solution is to separate the public and “slightly less” public settings into two top-level nodes:
"Settings":{
".read": "auth != null",
".write": "root.child('Users').child(auth.uid).child('rank').val() == 3",
},
"PublicSettings":{
".read": true,
".write": "root.child('Users').child(auth.uid).child('rank').val() == 3",
},
Now all users will have to perform two reads, one for the auth-requiring settings and one for the public settings. But it will now require two reads, no matter how many public properties you define.
Problem :
there is a table called Settings in my database and in my security rules, only two of the children can be accessed by non auth user.
"Settings":{
".read": "auth != null",
".write": "root.child('Users').child(auth.uid).child('rank').val() == 3",
"$id":{
".read": "$id == 'maintenance' || $id == 'welcomeList'"
}
},
But this way I can not get settings using one query like below:
settingsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
settings = dataSnapshot.getValue(Settings.class));
}
});
I have to query twice to get what I want. Is there any other way to do this? Because I am going to need more than two children in the future and I do not want to end up having 5 different queries.
settingsRef.child("maintenance").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
settings.setMaintenance(dataSnapshot.getValue(Integer.class));
}
});
settingsRef.child("welcomeList").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
settings.setWelcomeList(dataSnapshot.getValue());
}
});
Comments
Comment posted by jackieboyx
Thank you for your answer. In this case the solution you’ve suggested seems fine. But for example, I have questions in my database and I can not let the users see the answer. But when i set it to false, they won’t have a permission to check if it is wrong or right. So i set it to “auth != null” and now they have access to the answer. What I am afraid of is if they can find a way to access the answer outside the application, or hack into it or something. I don’t know if i managed to explain my issue but again thanks for the solution.
Comment posted by jackieboyx
I don’t have the reputation to vote but thank you for your help.