Firestore rules for document field

前端 未结 2 1433
南方客
南方客 2020-12-17 14:40

I\'m struggling within Firestore to set security rules for a document. With the RTDB is was possible to set rules for a specific object property and I\'m trying to do the sa

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 15:00

    You can do this by checking the request.resource.data property. As shown in this section of the documentation. You only need to match the document level. You check the field rules with an if condition.

    However, you are unable to control read access to individual fields. A user can either read a whole document or not. If you need to store private data, consider adding this to a sub-collection of the user document.

    Here is an example

    service cloud.firestore {
      match /databases/{database}/documents {
        // Make sure all cities have a positive population and
        // the name is not changed
        match /cities/{city} {
          allow update: if request.resource.data.population > 0
                        && request.resource.data.name == resource.data.name;
        }
      }
    }
    

提交回复
热议问题