Skip to content

Instantly share code, notes, and snippets.

@keremtiryaki
Last active August 17, 2019 11:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keremtiryaki/38655514b68334aa696a to your computer and use it in GitHub Desktop.
Save keremtiryaki/38655514b68334aa696a to your computer and use it in GitHub Desktop.
Security Rules & queries
var baseUrl = "https://213das234sdf35dfg.firebaseio.com";
angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope, Auth, $firebaseArray, $firebaseObject) {
$scope.object = {};
$scope.projects = [];
Auth.$onAuth(function(authData) {
if (authData) {
$scope.authData = authData;
// This returns the correct data
var objectRef = new Firebase(baseUrl+"/projects");
$scope.object = $firebaseObject(objectRef.child("-KBAYwlKln2hf1AmjwwP"));
console.log("$scope.object:");
console.log($scope.object);
//RETURNS ERROR:
//permission_denied: Client doesn't have permission to access the desired data.
var projectsRef = new Firebase(baseUrl).child("projects").orderByChild("team_id").equalTo($scope.authData.auth.slack.team_id);
$scope.projects = $firebaseArray(projectsRef);
console.log("$scope.projects");
console.log($scope.projects);
}
});
})
.factory("Auth", function($firebaseAuth) {
var usersRef = new Firebase(baseUrl);
return $firebaseAuth(usersRef);
});
{
"auth": {
"uid": "userId1",
"slack": {
"url": "https://team1.slack.com/",
"team_id": "teamId1",
"user_id": "userId1",
"team": "team1",
"user": "name1"
}
},
"expires": 1518207304,
"token": "BlaBlaFooBArFoObArfOo_sample_token",
"uid": "userId1",
"provider": "custom"
}
var a = {
"members" : {
"userId1" : {
"team" : "team1",
"team_id" : "teamId1",
"url" : "https://team1.slack.com/",
"user" : "name1",
"user_id" : "userId1"
},
"userId2" : {
"team" : "team2",
"team_id" : "teamId2",
"url" : "https://team2.slack.com/",
"user" : "name2",
"user_id" : "userId2"
}
},
"projects" : {
"-KBAYwlKln2hf1AmjwwP" : {
"name" : "p1",
"team_id" : "teamId1"
},
"-KBAYyLvBvZ0f47Krf6S" : {
"name" : "p2",
"team_id" : "teamId1"
},
"-KBA_ohE4tQg3h9gxJPM" : {
"name" : "asdasd",
"team_id" : "teamId2"
},
"-KBA_p_An2mh7wTdk4iY" : {
"name" : "asdsa sad",
"team_id" : "teamId2"
}
},
"teams" : {
"teamId1" : {
"team_id" : "teamId1",
"team_name" : "team1",
"url" : "https://team1.slack.com/",
},
"teamId2" : {
"team_id" : "teamId2",
"team_name" : "team2",
"url" : "https://team2.slack.com/",
}
}
};
var objectRef = new Firebase(baseUrl).child("projects/-KBAYwlKln2hf1AmjwwP");
objectRef.on('value', function(dataSnapshot) {
console.log(dataSnapshot.val());
});
//This throws permission_denied errors: Client doesn't have permission to access the desired data.
var projectsRef = new Firebase(baseUrl).child("projects").orderByChild("team_id").equalTo("teamId1");
projectsRef.on('value', function(dataSnapshot) {
console.log(dataSnapshot.val());
});
{
"rules": {
".read": false,
".write": false,
"members": {
".read": false,
"$id": {
".read": "auth != null && auth.uid == $id",
".write": "auth != null && auth.uid == $id"
}
},
"teams": {
".read": false,
"$id": {
".read": "auth != null && auth.slack.team_id == $id",
".write": "auth != null && auth.slack.team_id == $id"
}
},
"projects": {
".read": false,
".indexOn": ["team_id"],
"$id": {
".read": "auth != null && data.child('team_id').val() == auth.slack.team_id"
}
}
}
}
@MaxBushido
Copy link

Finally! Many thanks for helping me understand how to nest rules.
On the official documentation there is no example or text on how to set multiple rules for multiple locations and even here it's just full of copy & paste from those sources!! Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment