firebase - 我如何在 firebase 中实现基于角色的访问控制

标签 firebase firebase-security

这是我第一次涉足 Firebase 和 nosql,我来自 SQL 背景。

使用简单登录安全电子邮件/密码,如何限制对 Firebase 中数据的访问?例如,某些用户将有权创建业务对象(用户、客户、类别等),而其他用户则不能。有没有办法将权限列表附加到“auth”变量?

最佳答案

没有办法将权限直接附加到 auth 变量(或者至少这似乎不是预期的策略)。我建议创建由 auth.uid 组织的用户集合并且您可以在其中保留您想要的任何类型的权限属性,这样您的安全规则可能看起来像这样(未经测试):

{
  "rules": {
    ".read": true,
    "users": {
      ".write": "root.child('users').child(auth.uid).child('role').val() == 'admin'"
    }
  }
}

哪里role是属于您 users 中所有对象的属性收藏。

更新

请参阅下面的评论:

"There isn't a way to attach permissions directly to the auth variable" This changed in 2017. You can now attach custom claims to an auth profile, which are available in security rules. See bojeil's answer and the Firebase documentation for custom claims. – Frank van Puffelen

关于firebase - 我如何在 firebase 中实现基于角色的访问控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19520615/

相关文章:

firebase - Firebase 可以支持反向 DNS 记录吗?

javascript - 为什么我的函数不返回组件? react

java - Android 上的 Firebase 自动重新连接

google-cloud-firestore - 比较 Firestore 规则中的文档时间戳

Firebase Storage uid 列表的自定义元数据安全规则

javascript - React - 即使刷新了页面,如何保持在同一页面上?

javascript - 无法使用未定义的 Firestore 时间戳 ('toDate')

firebase - 验证 Firestore 规则中的映射键

google-cloud-firestore - 在 Firestore 安全规则 str.matches(regex) 中将 "i"标志放在哪里?

Firebase 安全规则