| 1 | <h2>Full example</h2> |
| 2 | <pre><code>rules_version = '2'; |
| 3 | service cloud.firestore { |
| 4 | |
| 5 | match /databases/{database}/documents { |
| 6 | |
| 7 | // Returns `true` if the requested post is 'published' |
| 8 | // or the user authored the post |
| 9 | function authorOrPublished() { |
| 10 | return resource.data.published == true || request.auth.uid == resource.data.author; |
| 11 | } |
| 12 | |
| 13 | match /{path=**}/posts/{post} { |
| 14 | |
| 15 | // Anyone can query published posts |
| 16 | // Authors can query their unpublished posts |
| 17 | allow list: if authorOrPublished(); |
| 18 | |
| 19 | // Anyone can retrieve a published post |
| 20 | // Authors can retrieve an unpublished post |
| 21 | allow get: if authorOrPublished(); |
| 22 | } |
| 23 | |
| 24 | match /forums/{forumid}/posts/{postid} { |
| 25 | // Only a post's author can write to a post |
| 26 | allow write: if request.auth.uid == resource.data.author; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | match /databases/{database}/reviews { |
| 31 | // Assign roles to all users and refine access based on user roles |
| 32 | match /some_collection/{document} { |
| 33 | allow read: if get(/databases/$(database)/reviews/users/$(request.auth.uid)).data.role == "Reader" |
| 34 | allow write: if get(/databases/$(database)/reviews/users/$(request.auth.uid)).data.role == "Writer" |
| 35 | } |
| 36 | } |
| 37 | }</code></pre> |