> For the complete documentation index, see [llms.txt](https://doc-primelms.mrb-lab.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc-primelms.mrb-lab.com/admin-setup/3.-firebase-setup/3.2-database-security-rules.md).

# 3.2 Database Security Rules

From the Firestore Database, Click on the **Rules** tab and copy and paste the following code below:

{% code title="Firestore Security Rules" fullWidth="false" %}

```firestore-security-rules
rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {
  
    match /categories/{document=**} {
    	allow read : if true;
      allow write: if isUserSignedIn() && isAdmin();
    }
    
    match /tags/{document=**} {
    	allow read : if true;
      allow write: if isUserSignedIn() && isAdmin();
    }
    
    match /courses/{document=**} {
    	allow read : if true;
      allow update: if isUserSignedIn() && (
      	isAdmin() || isAuthor() || 
        (request.resource.data.diff(resource.data).affectedKeys().hasOnly(['students'])) || 
        (request.resource.data.diff(resource.data).affectedKeys().hasOnly(['rating']))
      )
      allow create, delete: if isUserSignedIn() && (isAdmin() || isAuthor());
    }
    
    match /notifications/{document=**} {
    	allow read : if true;
      allow write: if isUserSignedIn() && isAdmin();
    }
    
    match /reviews/{document=**} {
    	allow read : if true;
      allow create, update : if isUserSignedIn();
      allow delete: if isUserSignedIn() && isAdmin();
    }
    
    match /purchases/{document=**} {
    	allow read : if true;
      allow create: if isUserSignedIn();
    }
    
    match /settings/{document=**} {
    	allow read : if true;
      allow write: if isUserSignedIn() && isAdmin();
    }
    
    match /user_stats/{document=**} {
    	allow read : if true;
      allow write: if isUserSignedIn();
    }
    
    match /purchase_stats/{document=**} {
    	allow read : if true;
      allow write: if isUserSignedIn();
    }
    
    match /users/{document=**} {
    	allow read : if true;
      allow create: if isUserSignedIn() && request.auth.uid == request.resource.id;
      allow update: if isUserSignedIn() && (
      	request.auth.uid == request.resource.id ||
      	isAdmin() || 
        (request.resource.data.diff(resource.data).affectedKeys().hasOnly(['author_info']))
      )
      
      allow delete: if isUserSignedIn() && request.auth.uid == resource.id;
    }
		
  
  	function isUserSignedIn (){
    	return request.auth != null;
    }
    
    function isAdmin (){
    	return "admin" in get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role;
    }
    
    function isAuthor (){
    	return "author" in get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role;
    }
  }
}
```

{% endcode %}

Click on the **Publish** button to publish the security rules. That's it.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://doc-primelms.mrb-lab.com/admin-setup/3.-firebase-setup/3.2-database-security-rules.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
