Firestore security rules with spaces in path

2019-04-16 13:48发布

问题:

I need to create a firestore rule for a sub collection called "Test Cases". Since firestore rules aren't written in javascript, I can't seem to get the path after match to accept a space without an error.

I've tried quotes, backslashes for escape characters, and putting the whole path in quotes. I haven't found anything for this in the firestore documentation or on stack overflow.

How can I allow a spaces in the path after match, in the example below, in the path including "Test Cases"?

service cloud.firestore {

  match /databases/{database}/documents {

    match /companies/{company} {
      allow read: if getUserCompany() == company || userHasAnyRole(['Super', 'Manager']);
      allow write: if getUserCompany() == company || userHasAnyRole(['Super', 'Manager']); 

      match /Test Cases/{tests} {
        allow read, write: if isSignedIn();
      }
    }

回答1:

According to fire base support:

To fix this, you can encode the space within security rules using %20. So the rules would be:

Service cloud.firestore { 

match /databases/{database}/documents { 

match /companies/{company} { 
allow read: if getUserCompany() == company || userHasAnyRole(['Super', 'Manager']); 
allow write: if getUserCompany() == company || userHasAnyRole(['Super', 'Manager']); 

match /Test%20Cases/{tests} {                      <------- 
allow read, write: if isSignedIn(); 
} 
} 
} 

I tried it and worked for me. Please give it a try and let us know if you have any issues.