MData
MData

Reputation: 15

Security rules at Firestore in Map within another Map

I'm making the security rules for Firebase Firestore, but I can't reference the map inside another map, as shown in the image below. I need to reference the type and whether it is active.

Document with Map inside another Map:

enter image description here

I tried to use the following command:

get (/databases/$(database)/documents/after_sales/{after_salesUID}/users_type).data.type in ['admin', 'approver'] &&

get (/databases/$(database)/documents/after_sales/{after_salesUID} /users_type.data.active == true;

When trying to test access to the document, I receive the following error message "Error while running the simulation: An unknown error has occurred".

Upvotes: 1

Views: 291

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50930

I am unsure of your complete database structure but I created a collection named 'companies' and a document in it.

enter image description here

These rules worked for me:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /companies/{companyID} {
      allow read: if get(/databases/$(database)/documents/companies/$(companyID)).data.user_types[request.auth.uid].admin == true;
    }
  }
}

I am not sure which location you are trying to access in your db but that's how you can read values in nested maps. Apparently it's after_sales ID in your case that I made user UID in my test.

Upvotes: 1

Related Questions