Firestore Database Rules and Structure for sharing Documents between users

后端 未结 1 841
无人共我
无人共我 2020-12-30 13:44

I\'m trying to create an application which allows users to collaborate on lists. Every user needs to be invited in order to be able to work on the list.

I structured

相关标签:
1条回答
  • 2020-12-30 14:23

    I was able to figure it out.

    I changed the data structure to this:

    list
      list_1
        owner: owner@company.com
        writeAccess: [user1@company.com, user2@company.com]
        id
        name
        items:
          item_1:
            id:
            name:
          ...
    

    Then the database rules like this are working:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /lists/{listId} {
            // Allow RW on lists for owner, shared user or for everyone if it's a new list
          allow read, write: if resource.data.owner == request.auth.token.email ||
                                request.auth.token.email in resource.data.writeAccess ||
                                !exists(/databases/$(database)/documents/lists/$(listId))
        }
        match /lists/{listId}/items/{itemId} {
            // Allow RW on item for owner or shared user of parent list
            allow read, write: if get(/databases/$(database)/documents/lists/$(listId)).data.owner == request.auth.token.email ||
                                  request.auth.token.email in get(/databases/$(database)/documents/lists/$(listId)).data.writeAccess ||
                                 !exists(/databases/$(database)/documents/lists/$(listId)) // Needed for new lists. Because lists and items are created in a batch
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题