firebase_rpg
utility library to write firebase security rules easier
Last updated 6 years ago by emmn .
GPL2 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install firebase_rpg 
SYNC missed versions from official npm registry.

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()))"
      }
    }
  }
}

Current Tags

  • 1.0.3                                ...           latest (6 years ago)

4 Versions

  • 1.0.3                                ...           6 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (3)
Dev Dependencies (3)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |