这是我第一次涉足 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/