컨텐츠 소유자에게만 허용


service cloud.firestore {
  match /databases/{database}/documents {
    // Allow only authenticated content owners access
    match /some_collection/{userId}/{documents=**} {
      allow read, write: if request.auth != null && request.auth.uid == userId
    }
  }
}

이 규칙을 이용해 컨텐츠 소유자인지 확인하고 소유자가 맞다면 데이터 수정을 허용한다.

공개 또는 비공개 설정


블로그 같은 앱이라면 비공개 글인지 공개글인지 확인하고 이것을 모든 사용자가 읽게 할 수 있거나 없게 설정해야하고, 또한 읽을 수 는 있지만 쓰는 것을 블로그 주인만 해야한다.

원하는 collection과 Document 주소를 적고 allow read: if true로 모든 읽기는 허용하지만 글을 생성하거나 수정 또는 삭제는 해당 글 주인만 허용하도록 한다.

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /some_collection/{document} {
      allow read: if true
      allow create: if request.auth.uid == request.resource.data.author_uid;
      allow update, delete: if request.auth.uid == resource.data.author_uid;
    }
  }
}

역할 지정


만약 노션과 비슷한 팀으로 글 작성하는 블로그를 운영한다면 여러 주인이 있다. 이럴경우에는 여러명한테 관리자 역할을 주어야한다. 아래와 같이 원하는 데이터베이스 주소를 적고 역할을 여러명에게 지정할 수 있다.

service cloud.firestore {
  match /databases/{database}/documents {
    // For attribute-based access control, Check a boolean `admin` attribute
    allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;
    allow read: true;

    // Alterntatively, for role-based access, assign specific roles to users
    match /some_collection/{document} {
     allow read: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Reader"
     allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Writer"
   }
  }
}