Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting .read and .write Security Rules To Work For Groups

I am​ having difficulty writing the security rules for​ building a team based collaboration platform.

  1. ​​When a user registers they should be able to create a team and invite users to that team.
  2. Projects should be owned by the team​.​
  3. ​Only users in that team should be able to view ​that project.​
  4. ​Users should only see the teams they are a member of​.

How do I write​ .read​ security rules so the ​users only see info from teams they're in?

I should only get two teams listed because I belong to them github:8272012​.​

​Current Security Rules: ​

{
    "rules": {
        ".read": true, 
        "users": {
          "$user": {
            //can add a message if authenticated
            ".write": "auth.uid === $user" 
          }
        }, 
        "teams": {
            "$team": {
                "users": {
                    // can write to the users list only if ADMINISTRATOR
                    "$user": {
                        ".write":"newData.parent().child(auth.uid).val() === 99"
                    }
                }
            }
        },
        "projects": {
          "$team": {
            "$project": { 
                //can add a message if they are a MEMBER
                ".write": "(!data.exists() && newData.exists() && root.child('teams/' + $team + '/users/' + auth.uid).val() >= 10)"
            }
          }
        }
    }
}

I should only get two teams listed because I belong to them github:8272012.

enter image description here

enter image description here

like image 415
Michael J. Calkins Avatar asked Aug 27 '15 20:08

Michael J. Calkins


People also ask

Why is it important to write security rules in Firebase?

Firebase Security Rules stand between your data and malicious users. You can write simple or complex rules that protect your app's data to the level of granularity that your specific app requires.

What file should be used for Realtime Database security rules?

To access your rules from the Firebase console, select your project, then in the left-hand navigation panel, click Realtime Database. Click Rules once you're in the correct database or storage bucket. To access your rules from the Firebase CLI, go to the rules file noted in your firebase. json file.

How do you fix insecure rules in Firebase?

Solution: Rules that restrict read and write access. Build rules that make sense for your data hierarchy. One of the common solutions to this insecurity is user-based security with Firebase Authentication. Learn more about authenticating users with rules.

How do I change my security rules on firestore?

Use the Firebase console To set up and deploy your first set of rules, open the Rules tab in the Cloud Firestore section of the Firebase console. Write your rules in the online editor, then click Publish.


1 Answers

The following security rules would give read and write access for a project only to users who are in that project's team (assuming you add a /projects node for each user to indicate which projects that user has access to):

"rules": {
  "projects": {
    "$project": {
      ".read": "root.child('users').child(auth.uid).child('projects').val().child($project).exists()" ,
      ".write": "root.child('users').child(auth.uid).child('projects').val().child($project).exists()" 
    }
  }
}

I can't see what data you're storing for each project, but if you store a reference to the project's team you could also use that in your security rules.

like image 77
Sara Avatar answered Sep 29 '22 17:09

Sara