Hyun lively
Hyun lively

Reputation: 31

How to set Firestore security rules where an array contains a specific value

I want to set the rules like this: When user's uid is contained in an array field in the city document, that user has authority.

I made it like this. Please tell me how to do it

allow read : if (city.some-array.contain(uid));

service cloud.firestore {
  match /databases/{database}/documents {

    // Match any document in the 'cities' collection
    match /cities/{city} {
      allow read: ***if <condition>;***
      allow write: if <condition>;
    }
  }
}

Upvotes: 1

Views: 2824

Answers (1)

yoonicode
yoonicode

Reputation: 1477

You can use resource.data, an object containing the current data before the operation, combined with in to achieve this.

match /cities/{city} {
    allow read, write: if request.auth.uid in resource.data.authorizedUsers
    // replace authorizedUsers with the name of your array field
}

Upvotes: 5

Related Questions