Nitesh
Nitesh

Reputation: 2024

Firestore rule to write access based on property bool value

enter image description here

I have a structure like above. What I want to achieve is in chat_room user can write only if in users array this uid.isActive == true

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
     match /users/{userId} {
      allow update, delete: if request.auth != null && request.auth.uid == userId;
      allow create, read: if request.auth != null;
    }
    match /chats/{chatId} {
      allow read, write: if request.auth.uid != null;
      match /chat_room/{roomId} {
        allow read, write: if request.auth.uid != null;
      }
    }
  }
}

Upvotes: 0

Views: 104

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599196

It sounds like you want:

match /chat_room/{roomId} {
  allow read, write: if get(/databases/$(database)/documents/chats/$(chatId)).data.users[request.auth.uid].IsActive == true;
}

So when writing to a chat_room documents, this loads the parent document and then checks if the user is marked as active in there.

Upvotes: 1

Related Questions