Firebase: set security rules depending on user roles

MarcoS

I would like to implement "write" security rules in Firebase depending on users roles.
My data structure is like this:

+ myapp
  + users
    + john
      + email: "[email protected]"
      + roles
        + administrator: true
    + mary
      + email: "[email protected]"
      + roles
        + moderator: true
    + ...
  + documents
    + -JVmo6wZM35ZQr0K9tJu
      + ...
    + -JVr56hVTZxlAI5AgUaS
      + ...
    + ...

I want - for example - that only administrator users can write documents.
These are the rules I've come to:

{
  "rules": {
    ".read": true,
    "$documents": {
      ".write": "root.child('users').child(auth.uid).child('roles').child('administrator').val() === true"
    }
  }
}

But it doesn't work: not even administrator users can write documents...
Is my understanding of Firebase security rules totally flawed?

UPDATE: Just before Jenny's answer (believe it or not :-), I did implement the exact same solution he provides (of course based on Kato's comment).
Though, making some tests, I could not let the rules structure

{
  "rules": {
    "documents" {
      "$document" {
        ".read": "root.child('users').child(auth.uid).child('roles').child('documents').child('read').val() === true",
        ".write": "root.child('users').child(auth.uid).child('roles').child('documents').child('write').val() === true"
      }
    }
  }
}

work... I always got a warning like this:

"FIREBASE WARNING: on() or once() for /documents failed: Error: permission_denied: Client doesn't have permission to access the desired data. "

So I came up with this structure, instead:

{
  "rules": {
    "documents" {
      ".read": "root.child('users').child(auth.uid).child('roles').child('documents').child('read').val() === true",
      ".write": "root.child('users').child(auth.uid).child('roles').child('documents').child('write').val() === true"
    }
  }
}

Which indeed works, for me: if I set a roles/customers/read node to true on a user he can read all documents, otherwise he can't (and the same for write).

My doubts now are:

  • why I could not let the first rule (as suggested by Kato) work?
  • do you see any possible security hole in a rule like the one I did came up with?
  • are rules using "$" variables necessary/useful even if you don't have to allow/deny the readability/writeability of each single document based on it's key, but you just want to allow/deny the readability/writeability of a node as a whole?
mimming

Based on the names of your user records, they don't match auth.uid, which is probably a Simple Login id, such as twitter:2544215.

Start by adjusting your users to be stored by their Simple Login uid:

+ myapp
  + users
    + twitter:2544215
      + email: "[email protected]"
      + roles
        + administrator: true
    + twitter:2544216
      + email: "[email protected]"
      + roles
        + moderator: true
    + ...
  + documents
    + -JVmo6wZM35ZQr0K9tJu
      + ...
    + -JVr56hVTZxlAI5AgUaS
      + ...
    + ...

Next, add a security rule so that administrators can access documents. You have a couple options here, depending on your specific use case.

  1. To give administrators write access to contents of each document:

    {
      "rules": {
        "documents": {
          "$documents": {
            ".write": "root.child('users').child(auth.uid).child('roles').child('administrator').val() === true"
          }
        }
      }
    }
    
  2. Or, alternatively, give them access to the whole collection:

    {
      "rules": {
        "documents": {
          ".write": "root.child('users').child(auth.uid).child('roles').child('administrator').val() === true"
        }
      }
    }
    

The difference between these two being the $documents variable that moves the security rule one step further into the hierarchy.

(This was mostly just an aggregation of comments by @Kato into answer form)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create public/private user profile with Firebase security rules?

From Dev

Security rules in Firebase

From Dev

Firebase Security Rules

From Dev

Basic security rules with Firebase

From Dev

Firebase Security Rules

From Dev

Firebase security and rules

From Dev

firebase - security rules REST

From Dev

Firebase security rules not working

From Dev

Conditional SELECT depending on a set of rules

From Dev

How to separate user roles in firebase?

From Dev

Firebase indexOn security & rules not working

From Dev

Firebase Security & Rules -Double-

From Dev

Temporary Variables in Firebase Security rules

From Dev

Firebase rate limiting in security rules?

From Dev

Firebase Security Rules for .remove () method

From Dev

Firebase permission denied with security rules

From Dev

Are Firebase Security Rules validations atomic?

From Dev

Use TIMESTAMP in Firebase Security Rules

From Dev

Firebase permission denied with security rules

From Dev

Firebase Database Delete Security Rules

From Dev

Firebase storage security rules not working

From Dev

Error deploying firebase security rules

From Dev

Why are my Firebase Firestore Security Rules allowing a .set() operation on an existing document, but the .set() has missing fields

From Dev

Why are my Firebase Firestore Security Rules allowing a .set() operation on an existing document, but the .set() has missing fields

From Dev

Set CSS rules depending the value of a table cell

From Dev

How to set the rules depending on the entity field?

From Dev

Firebase Security Rules Without Firebase Authentication

From Dev

Firebase - How to set security rules so that only logged in users can read and write?

From Dev

Reusing Storyboard UI for a UIViewController subclass depending on user roles

Related Related

  1. 1

    How to create public/private user profile with Firebase security rules?

  2. 2

    Security rules in Firebase

  3. 3

    Firebase Security Rules

  4. 4

    Basic security rules with Firebase

  5. 5

    Firebase Security Rules

  6. 6

    Firebase security and rules

  7. 7

    firebase - security rules REST

  8. 8

    Firebase security rules not working

  9. 9

    Conditional SELECT depending on a set of rules

  10. 10

    How to separate user roles in firebase?

  11. 11

    Firebase indexOn security & rules not working

  12. 12

    Firebase Security & Rules -Double-

  13. 13

    Temporary Variables in Firebase Security rules

  14. 14

    Firebase rate limiting in security rules?

  15. 15

    Firebase Security Rules for .remove () method

  16. 16

    Firebase permission denied with security rules

  17. 17

    Are Firebase Security Rules validations atomic?

  18. 18

    Use TIMESTAMP in Firebase Security Rules

  19. 19

    Firebase permission denied with security rules

  20. 20

    Firebase Database Delete Security Rules

  21. 21

    Firebase storage security rules not working

  22. 22

    Error deploying firebase security rules

  23. 23

    Why are my Firebase Firestore Security Rules allowing a .set() operation on an existing document, but the .set() has missing fields

  24. 24

    Why are my Firebase Firestore Security Rules allowing a .set() operation on an existing document, but the .set() has missing fields

  25. 25

    Set CSS rules depending the value of a table cell

  26. 26

    How to set the rules depending on the entity field?

  27. 27

    Firebase Security Rules Without Firebase Authentication

  28. 28

    Firebase - How to set security rules so that only logged in users can read and write?

  29. 29

    Reusing Storyboard UI for a UIViewController subclass depending on user roles

HotTag

Archive