Skip to content

Instantly share code, notes, and snippets.

@adil1214
Forked from codediodeio/database.rules.json
Created February 20, 2019 02:03
Show Gist options
  • Select an option

  • Save adil1214/4f22fc921c384b2694fe063a050e114d to your computer and use it in GitHub Desktop.

Select an option

Save adil1214/4f22fc921c384b2694fe063a050e114d to your computer and use it in GitHub Desktop.

Revisions

  1. @codediodeio codediodeio revised this gist Jun 20, 2017. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions database.rules.json
    Original file line number Diff line number Diff line change
    @@ -78,6 +78,21 @@
    }
    }

    // Validates timestamp is not a future value

    {
    "rules": {
    "posts": {
    "$uid": {
    "timestamp": {
    ".validate": "newData.val() <= now"
    }
    }
    }
    }
    }


    // Prevents Delete or Update
    {
    "rules": {
  2. @codediodeio codediodeio created this gist Jun 20, 2017.
    125 changes: 125 additions & 0 deletions database.rules.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,125 @@

    // No Security

    {
    "rules": {
    ".read": true,
    ".write": true
    }
    }

    // Full security

    {
    "rules": {
    ".read": false,
    ".write": false
    }
    }

    // Only authenticated users can access/write data

    {
    "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
    }
    }

    // Checks auth uid equals database node uid
    // In other words, the User can only access their own data

    {
    "rules": {
    "posts": {
    "$uid": {
    ".read": "$uid === auth.uid",
    ".write": "$uid === auth.uid"
    }
    }
    }
    }

    // Validates user is moderator from different database location

    {
    "rules": {
    "posts": {
    "$uid": {
    ".write": "root.child('users').child('moderator').val() === true"
    }
    }
    }
    }

    // Validates string datatype and length range

    {
    "rules": {
    "posts": {
    "$uid": {
    ".validate": "newData.isString()
    && newData.val().length > 0
    && newData.val().length <= 140"
    }
    }
    }
    }

    // Checks presense of child attributes

    {
    "rules": {
    "posts": {
    "$uid": {
    ".validate": "newData.hasChildren(['username', 'timestamp'])"
    }
    }
    }
    }

    // Prevents Delete or Update
    {
    "rules": {
    "posts": {
    "$uid": {
    ".write": "!data.exists()"
    }
    }
    }
    }

    // Prevents only Delete
    {
    "rules": {
    "posts": {
    "$uid": {
    ".write": "newData.exists()"
    }
    }
    }
    }

    // Prevents only Update
    {
    "rules": {
    "posts": {
    "$uid": {
    ".write": "!data.exists() || !newData.exists()"
    }
    }
    }
    }

    // Prevents Create and Delete
    {
    "rules": {
    "posts": {
    "$uid": {
    ".write": "data.exists() && newData.exists()"
    }
    }
    }
    }