在Firebase中定义用户角色 [英] Defining the User roles in Firebase

查看:369
本文介绍了在Firebase中定义用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中, Firebase作为后端 ,我打算根据安全性设置三个角色。 用户编辑,管理员



使用Facebook注册的用户默认被授予用户角色。




  • 用户可以读取任何内容但是写权限非常有限(提供评论)。
  • 编辑器可以读取任何内容,并具有比用户更多的写入权限(创建文章,提供评论

  • 管理员可以创建/删除编辑



管理员可以将用户角色升级到编辑器,反之亦然。
因此,创建用户角色和编辑器角色是照顾。 但是,如何创建一个管理员呢?



问候

解决方案

Firebase刚刚通过ID令牌上的自定义用户声明启动了对任何用户的基于角色访问的支持: https://firebase.google.com/docs/auth/admin/custom-claims



您可以定义管理员权限规则:

  {
rules:{
adminContent: {
.read:auth.token.role ==='admin',
.write:auth.token.role ==='admin',





$ p $使用Admin SDK设置用户角色:

  //设置与uid对应的用户的管理员权限。 
admin.auth()。setCustomUserClaims(uid,{role:'admin'})。then(()=> {
//新的自定义声明将传播给用户的ID令牌
//下一次发行新的
});

这将传播到相应用户的ID令牌声明。



要从客户端的令牌解析它,您需要base64解码ID令牌的有效负载。



您可以根据需要升级/降级用户。设置编辑器角色遵循相同的概念。


In my Android app with Firebase as backend, I am planning to have three roles based on Security. User, Editor, Administrator

Anyone signing-up using Facebook is granted the User role by default.

  • The User can read anything but has very limited write permissions (provide reviews).
  • The Editor can read anything and has more write permissions than the user (create articles, provide reviews).
  • The Administrator can create/Delete Editors.

The Administrator can upgrade the role of an User to an Editor and vice-versa. Hence creating the User roles and Editor roles is taken care of. But, how to create an Administrator in the first place?

Regards

解决方案

Firebase just launched support for role based access on any user via custom user claims on the ID token: https://firebase.google.com/docs/auth/admin/custom-claims

You would define the admin access rule:

{
  "rules": {
    "adminContent": {
      ".read": "auth.token.role === 'admin'",
      ".write": "auth.token.role === 'admin'",
    }
  }
}

Set the user role with the Admin SDK:

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

This will propagate to the corresponding user's ID token claims.

To parse it from the token on the client, you need to base64 decode the ID token's payload.

You can upgrade/downgrade users as needed. Setting editor role follows the same concept.

这篇关于在Firebase中定义用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆