Firebase security rules issue - android

I'm trying to restrict access to a particular groups if the user is not in the members list.
So this is my rule,
{
"rules": {
".read":"auth != null",
"groups": {
"$groupid":{
".read" : true,
".write" : "root.child('groupmembers').child($groupid).child('admins').child(root.child('users').child(auth.uid).child('username').val()).val()==true"
}
},
"groupchat":{
"$chatid":{
".read" : "root.child('groupmembers').child($chatid).child('members').child(root.child('users').child(auth.uid).child('username').val()).val()==true",
".write" : "root.child('groupmembers').child($chatid).child('members').child(root.child('users').child(auth.uid).child('username').val()).val()==true"
}
}
}
}
The first rule for "groups">"$groupid" works perfectly. But I'm getting problem with second rule for "groupchat"
DataStructure ex:
Group Chat structure
"groupchat" : {
"testinggroup" : {
"chat" : {
"-KiYN6RKZOyWI-qufWn3" : {
"message" : "Hey",
"timestamp" : 1493094660399,
"type" : "text",
"username" : "user2"
}
},
"lasttimestamp" : 1493094660674
}
}
Users Structure
"users":{
"ujciTL9oFKNylcgiGcdYqUxjcgA3" : {
"email" : "example#gmail.com",
"username" : "user1"
}
}
groupmembers structure
"groupmembers":{
"groupid":{
{
"admins" : {
"user1" : true,
"user3" : true
},
"members" : {
"user1" : true,
"user2" : true,
"user3" : true,
"user4" : true
}
}
}
}
groups structure
"groupid"{
{
"category" : "group category",
"creator" : "creator uid",
"desc" : "group desc",
"id" : "groupid",
"image" : "url",
"name" : "groupname"
}
}
Operation ex:
FirebaseDatabase.getInstance()
.getReference()
.child("groupchat")
.child(j)
.child("lasttimestamp")
.setValue(ServerValue.TIMESTAMP);
So to debug the errors
I added "myusername" in place of root.child('users').child(auth.uid).child('username').val()
and it's working.
But in the previous rule("For groups child") I added the same thing and it was working.
I don't know why I'm getting error.

Related

mongodb Filtering of content state associated with objectid in array

I want to import the data through the filtering function.
I think we should probably use 'aggregate' instead of 'find'.
The conditions for the data I want are:
There is A Collection and B Collection.
The 'contents' of A Collection points to the "_id" of B Collection.
The corresponding value is only fetched when the "Visibility" value of the B Collection is "true".
"A" Collection:
{
"_id" : ObjectId("aaaaa"),
"title" : "study list",
"contents" : [ ObjectId("11111"), ObjectId("22222") ]
}
{
"_id" : ObjectId("bbbbb"),
"title" : "study list",
"contents" : [ ObjectId("33333"), ObjectId("44444") ]
}
{
"_id" : ObjectId("ccccc"),
"title" : "study list",
"contents" : [ ObjectId("55555") ]
}
"B" Collection:
{
"_id" : ObjectId("11111"),
"visibility" : true
}
{
"_id" : ObjectId("22222"),
"visibility" : true
}
{
"_id" : ObjectId("33333"),
"visibility" : true
}
{
"_id" : ObjectId("44444"),
"visibility" : false
}
{
"_id" : ObjectId("55555"),
"visibility" : false
}
== Data Result
{
"_id" : ObjectId("aaaaa"),
"title" : "study list",
"contents" : [ ObjectId("11111"), ObjectId("22222") ]
}
How can I get the data I want?
You can use below aggregation
db.collectionA.aggregate([
{ "$lookup": {
"from": "collectionB",
"localField": "contents",
"foreignField": "_id",
"as": "data"
}},
{ "$match": {
"data": {
"$not": {
"$elemMatch": { "visibility": false }
}
}
}}
])

android: adding a new node

Need to add a new node "Completed" under the parent which will eventually contain all the information from the node "Requests" at end of the ride.
So basically, I need to create node "Completed" first but its not adding it.
database
{
"Requests" : {
"iowpxU6WKUWpzWJyfssSoOVCPFj2" : {
".priority" : "f8118c3k3v",
"destination" : "221 Prince William St",
"details" : {
"driver" : "nYIAHSYimJMHbMkXqDt9PQ0U3Nf2",
"location" : "27 Horsfield St",
"request status" : "accepted",
"ridePrice" : 3.75,
"rideStatus1" : "arrived at pickup",
"rideStatus2" : "rider in vehicle",
"rideStatus3" : "destination bound",
"rideStatus4" : "arrived at destination",
"rider" : "iowpxU6WKUWpzWJyfssSoOVCPFj2",
"riderPaid" : "true"
},
"Users" : {
"Drivers" : {
"nYIAHSYimJMHbMkXqDt9PQ0U3Nf2" : {
"driver" : "nYIAHSYimJMHbMkXqDt9PQ0U3Nf2",
"email" : "driver#me.com",
"name" : "driver",
"password" : "whatever",
"phone" : "5551212",
"rates" : "0"
}
},
"Riders" : {
"iowpxU6WKUWpzWJyfssSoOVCPFj2" : {
"avatarUrl" : "",
"email" : "rider#me.com",
"name" : "rider",
"password" : "whatever",
"phone" : "5551313",
"rates" : "0",
"riderId" : "iowpxU6WKUWpzWJyfssSoOVCPFj2"
}
}
}
}
addNewNode
private void addNewNode() { // TODO: ........ NOT ADDING NEW NODE :-| ..........
Toast.makeText(this, "addNewNode", Toast.LENGTH_LONG).show();
DatabaseReference newNode = FirebaseDatabase.getInstance().getReference("Completed");
newNode.child(riderId).push().addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(DriverTripDetail.this, "addNewNode: onDataChange", Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Any assistance on how I can fix this would be greatly appreciated.
The Firebase Database stores values at paths. If there is no value, the path does not exist.
Your code creates a reference to /Completed/$pushID. but doesn't set any value, so the path doesn't get created. Something like this will work, since it sets a value:
newNode.child(riderId).push().setValue(true)

android firebase saving data to realtime database

In android app I create a firebase user authenticated by phone number and it works fine then I update this user adding display name and this is fine too; in the end I want to save user's data into firebase database so I wrote this rule:
"users" : {
"$user_id" : { // firebase user uid
".read" : "$user_id === auth.uid",
".write" : "$user_id === auth.uid",
".validate": "newData.hasChildren(['enabled', 'group', 'name'])",
"enabled" : { ".validate" : "newData.isBoolean()" },
"group" : { ".validate" : "newData.isString()" },
"name" : { ".validate" : "newData.isString()" },
}
}
This doesn't work and I get returned "permission denied" altough the simulator shows everything is fine (in the simulation I use custom authentication and set firebase as provider with the user's uid).
the user's class is:
public class User {
public boolean enabled;
public String group;
public String name;
public User() { }
public User(boolean enabled, String group, String name) {
this.enabled = enabled;
this.group = group;
this.name = name;
}
}
and i try to save it by:
private void saveUserToDB() {
User newUser = new User(true, "operators", currentUser.getDisplayName());
mRootRef.child("users").child(currentUser.getUid()).setValue(newUser, new DatabaseReference.CompletionListener() {
#Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
Log.e("ERROR", "Data could not be saved " + databaseError.getMessage());
} else {
Log.e("SUCCESS", "Data saved successfully.");
}
}
});
}
Can anybody kindly help me to understand why this happens?
Thanks
UPDATE
below is a "schema" of how the database should be:
{
"messages" : {
"1763409620982" : { // timestamp
"position" : "position text",
"sender" : "sender name",
"text" : "message text"
},
"1763409734527" : { // timestamp
"position" : "position text",
"sender" : "sender name",
"text" : "message text"
}
...
},
"users" : {
"Yi79w0HgA4ZTNAMHadixzoO5PaR2" : { // firebase user uid
"enabled" : true,
"group" : "group 1",
"name" : "user display name"
},
"kX22c0GgL4ZTNAMHallfgoO4pBN1" : { // firebase user uid
"enabled" : true,
"group" : "group 2",
"name" : "user name"
}
...
}
}
and here's the full set of rules:
{
"rules": {
"messages" : {
".read" : "root.child('users').child(auth.uid).child('group').val() === 'group 1'",
".write" : "root.child('users').child(auth.uid).child('group').val() === 'group 2'",
"$tstamp" :{
".validate": "newData.hasChildren(['position', 'sender', 'text'])",
"position" : { ".validate" : "newData.isString()" },
"sender" : { ".validate" : "newData.isString()" },
"text" : { ".validate" : "newData.isString()" },
}
},
"users" : {
"$user_id" : {
".read" : "$user_id === auth.uid",
".write" : "$user_id === auth.uid",
".validate": "newData.hasChildren(['enabled', 'group', 'name'])",
"enabled" : { ".validate" : "newData.isBoolean()" },
"group" : { ".validate" : "newData.isString()" },
"name" : { ".validate" : "newData.isString()" },
}
}
}
}
as to the class user it's defined as above and only used into the method saveUserToDB
Try to add push() before setValue(): It will add new sub-node containing all the fields from object of User class:
mRootRef.child("users").child(currentUser.getUid()).push().setValue(and so on...
May be you need write permission?:

Firebase whitelist and recyclerView

{
"Events" : {
"events" : {
"-KT5UMQAhDHs1bB8bKLc" : {
"activity" : "biking",
"address" : "54°05'45.8\"N 28°19'02.9\"E, просп. Мира 9, Жодино, Беларусь",
"category" : "sport",
"creatorID" : "m11EvlP19OSbEz8XYl2MsNCwGXX2",
"date" : 1475431200689,
"forFriends" : true,
"info" : "вттчтвт",
"name" : "ьаьатвтвтт",
"uid" : "-KT5UMQAhDHs1bB8bKLc"
},
"-KUSsl7RaL-dVoDCoSYT" : {
"activity" : "picnic",
"address" : "Узденский район, Узденский район, Беларусь",
"category" : "entertainment",
"creatorID" : "1RXDSJzSEXakxvNdM41Ae3nbHN72",
"date" : 1477328400000,
"forFriends" : false,
"info" : "45",
"name" : "Test of date",
"uid" : "-KUSsl7RaL-dVoDCoSYT"
}
},
"FriendsOF" : {
"NuKweeGv8zTgt1Vi9RcU3i1u86U2" : {
"1RXDSJzSEXakxvNdM41Ae3nbHN72" : true
},
"m11EvlP19OSbEz8XYl2MsNCwGXX2" : {
"1RXDSJzSEXakxvNdM41Ae3nbHN72" : true
}
},
"Users" : {
"1RXDSJzSEXakxvNdM41Ae3nbHN72" : {
"email" : "*#gmail.com",
"name" : "Алексей Гвоздицкий",
"picture" : "*.jpg"
},
"NuKweeGv8zTgt1Vi9RcU3i1u86U2" : {
"email" : "*#gmail.com",
"name" : "Ольга Гвоздицкая",
"picture" : "*.jpg"
},
"m11EvlP19OSbEz8XYl2MsNCwGXX2" : {
"email" : "*#gmail.com",
"name" : "Егор Александров",
"picture" : "*.jpg"
}
}
}
I have a list of "events". Every "event" can be "forFiends" only (only if current user is in event creators "FriendsOF" list, then he can see this event. FriendsOF it's a whitelist). All availiable for current user events i want to represent in RecyclerView.
I am stucked. I can get all data snapshot and then filter on client side it. But it is not way of Jedi, because i load tonns of unnecessary data. Another solution is to deny acces to event if user should not see it. But i don't understand how to do it. My ref is mEventsRef = FirebaseDatabase.getInstance().getReference().child("Events").child("events"); And rule i can make for
"events":{
"$eventId":{".read":....}
}
So according to this guide i'll get empty recyclerView because of "error callback triggered with PERMISSION_DENIED"
Help me to solve my problem please!

How to display data that depends on other entity?

Having the following data:
"animals" : {
"a1" : {
"name" : "Dog"
},
"a2" : {
"name" : "Cat"
},
"a3" : {
"name" : "Cow"
}
},
"users" : {
"u1" : {
"favAnimals" : {
"a1" : true,
"a2" : true
},
"name" : "Danilo"
}
}
How can I display for the user, using the FirebaseListAdapter, the complete list of animals, highlighing user's favorite animals?
Is it possible to query and join them, to get a Database Reference that would watch for changes both in "/animals" and "/users/$uid/favAnimals" and return something like:
"animals" : {
"a1" : {
"name" : "Dog",
"fav" : true
},
"a2" : {
"name" : "Cat"
"fav" : true
},
"a3" : {
"name" : "Cow"
}
}

Categories

Resources