KevinB
KevinB

Reputation: 2484

Rules Cloud Firestore

I was wondering if it’s possible to cross collections to set a rule. Maybe by mixing exists() and get() ?

My Collection is like that :

- BusinessOwners
      |
      |_ autoID ___ businessID: String
                 |_ userID: String
      |
      |_ autoID ___ businessID: String
                 |_ userID: String

I would like to check (with my words) if:

  1. Exists a Document in BusinessOwners
  2. With userID == request.auth.uid && businessID == IDspecified

My match part would be like:

function isOwnerOfTheBusiness(IDspecified){
    return ???;
}

match /userresults/{docID} {
        allow write, read: if isOwnerOfTheBusiness(request.resource.data.businessID);
}

Can I do that? If yes, how?

Upvotes: 0

Views: 25

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317322

This is not possible exactly as you describe, because you can't perform queries in security rules. The only way you can access documents other than the one being accessed is if you know the full path, including the document ID. If you know the path, you can use get() and exists() with that path. But you just can't query a collection with conditions.

Upvotes: 1

Related Questions