# 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() || isAuthor());
    }
    
    match /articles/{document=**} {
    	allow read : if true;
      allow update: if 
        (request.resource.data.diff(resource.data).affectedKeys().hasOnly(['views'])) || 
        (isUserSignedIn() && (request.resource.data.diff(resource.data).affectedKeys().hasOnly(['likes']))) ||
      	 isAdmin() || isAuthor();
      allow create, delete: if isUserSignedIn() && (isAdmin() || isAuthor());
    }
    
    match /notifications/{document=**} {
    	allow read : if true;
      allow write: if isUserSignedIn() && isAdmin();
    }
    
    match /comments/{document=**} {
    	allow read : if true;
      allow create, update : if isUserSignedIn();
      allow delete: if isUserSignedIn();
    }
    
    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()
      )
      
      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: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
