Firestore有很多很多关系

Puc*_*hes 5 firebase google-cloud-firestore

想要与具有角色/权限的多对多结构相关的建议.

我们需要一种允许用户属于许多组织的结构,并且用户拥有每个组织的角色/权限.例如,User1属于ABC COas Admin,User1属于XYZ COasGuest

小智 1

我们已解决此问题如下:

organizations (collection) {
    ABC (doc) {
        permissions (object): {
            User1DocID (object): {
                admin: true
            }
        }
    }
    XYZ (doc) {
        permissions (object): {
            User2DocID (object): {
                guest: true
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这样你就可以像这样配置规则:

match /origanizations/{origanization} {
    allow update, read: if resource.data.permissions[request.auth.uid].admin == true;
    allow read: if resource.data.permissions[request.auth.uid].guest == true;
}
Run Code Online (Sandbox Code Playgroud)

对于组织的资源,您必须将组织 ID 存储在特定文档中(显然)。然后您可以为它们设置规则,如下所示:

match /origanizationRessources/{origanizationRessource} {
    allow update: if get(/databases/$(database)/documents/organizations/$(resource.data.organizationId)).data.permissions[request.auth.uid].admin == true;
}
Run Code Online (Sandbox Code Playgroud)

通过此设计,您还可以轻松查询用户具有特定权限的数据。

请注意:此设计符合我们的目的,因为我们分配给组织的用户数量有限且简单。如果您不确定,请查看文档大小的限制(请参阅https://firebase.google.com/docs/firestore/quotas)以了解是否必须依赖其他设计。如果您碰巧可能会遇到这些限制,请考虑使用单独的映射集合。