@@ -116,26 +116,39 @@ REACT_APP_CONFIRMATION_EMAIL_REDIRECT=https://mydomain.com
116116### Security Rules
117117
118118```
119- {
120- "rules": {
121- ".read": false,
122- ".write": false,
123- "users": {
124- "$uid": {
125- ".read": "$uid === auth.uid || root.child('users/'+auth.uid).child('roles').hasChildren(['ADMIN'])",
126- ".write": "$uid === auth.uid || root.child('users/'+auth.uid).child('roles').hasChildren(['ADMIN'])"
127- },
128- ".read": "root.child('users/'+auth.uid).child('roles').hasChildren(['ADMIN'])",
129- ".write": "root.child('users/'+auth.uid).child('roles').hasChildren(['ADMIN'])"
130- },
131- "messages": {
132- ".indexOn": ["createdAt"],
133- "$uid": {
134- ".write": "data.exists() ? data.child('userId').val() === auth.uid : newData.child('userId').val() === auth.uid"
135- },
136- ".read": "auth != null",
137- ".write": "auth != null",
138- },
119+ service cloud.firestore {
120+ match /databases/{database}/documents {
121+
122+ // Custom functions
123+ function signedIn() {
124+ return request.auth != null;
125+ }
126+
127+ function isAdmin() {
128+ return signedIn() &&
129+ 'ADMIN'in get(/databases/$(database)/documents/users/$(request.auth.uid)).data.roles.values();
130+ }
131+
132+ function ownsMessage() {
133+ return signedIn() && request.auth.uid == resource.data.userId;
134+ }
135+
136+ function isSelf() {
137+ return signedIn() && request.auth.uid == resource.id;
138+ }
139+
140+ // Rules
141+ match /users/{userId} {
142+ allow get: if isSelf();
143+ allow list: if isAdmin();
144+ allow write: if isSelf() || isAdmin();
145+ }
146+
147+ match /messages/{messageId} {
148+ allow read: if signedIn();
149+ allow create: if signedIn();
150+ allow update, delete: if signedIn() && ownsMessage();
151+ }
139152 }
140153}
141154```
0 commit comments