The Firebase 规则文档 https://firebase.google.com/docs/firestore/security/rules-conditions建议比较经过身份验证的用户令牌的构建条件(即request.auth
) 与目标 Firestore 文档。就像是:
match /posts/{postId} {
allow read, write: if (request.auth.uid != null) &&
(resource.data.tenantId == request.auth.token.tenantId);
}
然而,tenantId
似乎不可用Firebase 规则 https://firebase.google.com/docs/reference/rules/rules.firestore.Request#auth与其他相关的身份验证字段一样(例如,uid
, email
, email_verified
, etc.).
一种选择似乎是添加tenantId
分别作为定制索赔 https://firebase.google.com/docs/auth/admin/custom-claims使用firebase-admin
SDK。但这会在用户对象上创建重复信息:
{
uid: 'nzjNp3QIfSR6uWy',
emailVerified: true,
displayName: 'pickleR'
...
tenantId: 'wubalubadubdub',
customClaims: { tenantId: 'wubalubadubdub' },
}
An 替代方案 https://groups.google.com/forum/#!topic/firebase-talk/z_j9Z2HNjb4似乎是要创建一个tenants
Firestore 中的集合。然而,这种方法似乎引入了不必要的复杂性并增加了所需的 Firestore 查询数量。
是否有其他方法可以访问tenantId
是否了解 Firestore 规则和/或使用 Firestore 进行多租户的替代最佳实践?