1.0.3 • Published 10 years ago
firebase_rpg v1.0.3
Firebase RPG (rules, paths, generators)
Just a simpler way to write security rules
npm install firebase_rpg
coffee examples/simple.coffee
- Rule
Init:
- using string
- using object which responds to val() method (rule or path)
Methods:
- and(string or rule, isolate=false) => returns rule
- or(string or rule, isolate=false) => returns rule
- exists() => returns rule
- val() => returns string
isolate - specify if we want to wrap the condition in a section (see the tests)
- Path
Init:
- using string
- using object which responds to val() method (rule or path)
Methods:
- child(string or path) => returns path
- val() => returns string
- Generator
Init:
- rules hash containing rules
- rules hash item should be hash with write / read optional attributes
E.g.
rules =
'test/all':
read: true
write: false
will result in:
{
"rules": {
"test": {
"all": {
".read": true,
".write": false
}
}
}
}
- Usage
- require module
- use the R, P, G and in the end export the JSON
E.g.
RPG = require "firebase_rpg"
R = RPG.R
P = RPG.P
isLoggedIn = ->
R('auth.username !== null')
isCurrentUser = (userId) ->
R("#{userId} === auth.username")
isAdmin = (userId) ->
R(P("root").child("'admin'").child("#{userId}")).exists()
rules =
'test/$user_id':
read : isLoggedIn()
write: isLoggedIn()
.and(
isCurrentUser("$user_id").or(isAdmin("$user_id"))
, true)
generatedRules = RPG.G(rules).generate()
console.info(JSON.stringify(generatedRules, null, ' '));
Output:
{
"rules": {
"test": {
"$user_id": {
".read": "(auth.username !== null)",
".write": "(auth.username !== null&&($user_id === auth.username||root.child('admin').child($user_id).exists()))"
}
}
}
}