Firestore security rule logging of variable values

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

   match /companies/{companyName=**}{
        allow read,write: if isUserAuthorized(companyName,get(/databases/$(database)/documents/companies/$(companyName)/users/$(request.auth.uid)).data.company) == true;
   }
  }
}
function isUserAuthorized(requestedCompanyName,userCompanyName) {
  return requestedCompanyName == userCompanyName;
}

In the above rule of firestore I am trying to make sure that only employees from a company can access data of that company. The data structure on firestore looks like the following screenshot. where data is saved in this path /companies/{companyName}/users/{uid}/company

enter image description here

But when I try to save I get Error: Missing or insufficient permissions. js error.

  • Is there anything apparent I am doing wrong?
  • Is there any logging that happens on firestore side where I can see what request was made and what were the values of variables used in the rules.
  • Or is there a way to test the rules?
2 Answers

For now there is no way to test the rules.

you can try this

match /companies/{companyName=**}{
    allow read,write: if get(/databases/$(database)/documents/companies/$(companyName)/users/$(request.auth.uid)).company in [$(companyName)] || get(/databases/$(database)/documents/companies/$(companyName)/users/$(request.auth.uid)).data.company in [$(companyName)];
} 

which says write if path/users/$(request.auth.uid)).data.company has the companyName

try this for your current problem

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

   match /companies/{companyName=**}{
        allow read,write: if isUserAuthorized(companyName,get(/databases/$(database)/documents/companies/$(companyName)/users/$(request.auth.uid)).data.company) ;
   }
  }
}
function isUserAuthorized(requestedCompanyName,userCompanyName) {
  return requestedCompanyName == userCompanyName;
}
Related