1.0.3 • Published 10 years ago

firebase_rpg v1.0.3

Weekly downloads
2
License
GPL2
Repository
github
Last release
10 years ago

Firebase RPG (rules, paths, generators)

Just a simpler way to write security rules

npm install firebase_rpg
coffee examples/simple.coffee
  1. 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)

  1. Path

Init:

  • using string
  • using object which responds to val() method (rule or path)

Methods:

  • child(string or path) => returns path
  • val() => returns string
  1. 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
      }
    }
  }
}
  1. 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()))"
      }
    }
  }
}
1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago