Firebase 云存储规则可以根据 Firestore 数据进行验证吗?

2024-05-02

我们可以使用 Firestore 数据授予或限制对 Firebase 云存储上托管的文件的访问权限吗?

我想用作 Firebase 安全规则的示例

allow write: if get(/databases/mydbname/documents/guilds/$(guildID)).data.users[(request.auth.uid)] in ["Admin", "Member"];

更新(2022 年 10 月):it is现在可以使用新的 Cloud Storage 安全规则从 Cloud Firestore 访问 Cloud Firestorefirestore.get() and firestore.exists()功能。请参阅博客文章公布跨服务安全规则 https://firebase.blog/posts/2022/09/announcing-cross-service-security-rules以及有关的文档使用 Cloud Firestore 增强 Cloud Storage 安全规则 https://firebase.google.com/docs/storage/security/rules-conditions#enhance_with_firestore.

之前的回答如下,供参考:

目前无法从另一个产品的安全规则内访问不同的 Firebase 产品。看:有没有办法在 firebase 存储规则中验证用户角色? https://stackoverflow.com/questions/39187424/is-there-a-way-to-authenticate-user-role-in-firebase-storage-rules

但您似乎正在尝试检查用户的组成员身份。我建议您将其建模为所谓的自定义声明,而不是在数据库中查找该组成员身份。您无需(或除了)将成员资格写入数据库之外,使用 Firebase Admin SDK 将声明“用户 {uid} 是组 {guild1} 的成员”设置到用户个人资料中 https://firebase.google.com/docs/auth/admin/custom-claims#set_and_validate_custom_user_claims_via_the_admin_sdk:

admin.auth().setCustomUserClaims(uid, {guild1: true}).then(() => {
  // The new custom claims will propagate to the user's ID token the
  // next time a new one is issued.
});

完成后,您可以在安全规则中检查公会成员身份:

allow read: if request.auth.token.guild1 == true;

(我不确定是否/如何将公会成员资格建模为地图,如果情况确实如此,我将更新此答案。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Firebase 云存储规则可以根据 Firestore 数据进行验证吗? 的相关文章

随机推荐