Skip to content

Instantly share code, notes, and snippets.

@hardiksondagar
Created December 31, 2015 06:52
Show Gist options
  • Save hardiksondagar/75c4ca608aa69d7c6031 to your computer and use it in GitHub Desktop.
Save hardiksondagar/75c4ca608aa69d7c6031 to your computer and use it in GitHub Desktop.
Firebase multi-user database structure and security rules
/* Data structure */
{
"users": {
"05ef4bd9-3957-4466-b65d-4a625acc89e0":{
name: "Alan Turing",
email: "alan@turingmachine.com",
roles:{
student:false,
hiring_manager:false,
admin:true
}
},
"1fe5de67-4774-4e1d-9e22-2476263afcc7":{
name: "Nikola Tesla",
email: "nikola@tesla.com",
roles:{
student:false,
hiring_manager:true,
admin:false
}
},
"22d009b6-3eca-40be-9648-d1a6dcb12f61":{
name: "Thomas Edision",
email: "edision@business.com",
roles:{
student:true,
hiring_manager:false,
admin:false
}
}
},
"jobs":{
"-K6nJ0JX0LUghI7YdY_l":{
title:"Software engineer for Auto Pilot",
location:"Navada",
type:"hiring",
status:"active",
createdBy:"1fe5de67-4774-4e1d-9e22-2476263afcc7"
},
"-K6nJCVM5CAqPQX1WUby":{
title:"Seeking internship in electric car company",
location:"Mountain View",
type:"seeking",
status:"active",
createdBy:"22d009b6-3eca-40be-9648-d1a6dcb12f61"
}
},
"interviews":{
"1fe5de67-4774-4e1d-9e22-2476263afcc7":{
date:1451328903455,
location:"Navada",
status:"scheduled",
managerId:"1fe5de67-4774-4e1d-9e22-2476263afcc7",
studentId:"22d009b6-3eca-40be-9648-d1a6dcb12f61"
}
}
}
/* Security rules */
{
"rules": {
"users": {
"$user": {
".read": "auth.uid === $user",
".write": "auth.uid === $user && (!newData.exists() || newData.hasChildren())",
"name": {
".validate": "newData.isString() && newData.val().length <= 2000"
},
"email": {
".validate": "newData.isString() && newData.val().length <= 2000"
},
"$other": {
".validate": false
}
}
},
"jobs": {
"$job": {
".read": "auth != null && auth.uid == $userId",
".write": "root.child('users').child(auth.uid).child('roles').child('student').val() === true || root.child('users').child(auth.uid).child('roles').child('hiring_manager').val() === true",
".indexOn": ["created"]
}
},
"interviews": {
"$interview": {
".read": "auth != null && ( root.child('interviews').child($interview).child('studnetId').val() === auth.uid || root.child('interviews').child($interview).child('managerId').val() === auth.uid || root.child('users').child(auth.uid).child('roles').child('admin').val() === true) ",
".write": "root.child('users').child(auth.uid).child('roles').child('student').val() === true || root.child('users').child(auth.uid).child('roles').child('hiring_manager').val() === true",
".indexOn": ["studentId","managerId"]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment