将访问Firebase位置授予一组用户

编程入门 行业动态 更新时间:2024-10-10 15:27:17
本文介绍了将访问Firebase位置授予一组用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法在文档中找到它,但是有没有办法定义一组用户,并使用该组授予对不同位置的访问权限,而不是授予个人用户访问权限?

问候, LT

解决方案

因为你可以很容易地代表他们。这里有两个选项,取决于你的情况。

在firebase中存储组信息

以下数据可能被用来代表2个组('alpha'和'beta')和3个受保护的数据('thing1','thing2'和'thing3') <$ p groups:{alpha:{joe:true,sally:true beta:{joe:true,fred:true } },data: {thing1:{group:alpha / *只能由alpha组访问数据* / },thing2 :{group:beta / *只能由beta组访问的数据* / },thing3:{ group:alpha / *alpha组可以访问更多的数据* / } } } 然后,我们可以使用以下规则来执行安全性: $ b {rules:{data:{$ thing:{.read :root.child('groups')。child(data.child('group').val())。hasChild(auth.id),.write:root.child hasChild(auth.id)} } } } $ b('group')。child(data.child('group')。那么如果我使用{id:'sally'}进行身份验证作为我的身份验证对象,那么我将会使用{$ b $ / code>

$ b>

有权访问thing1和thing3,而不是thing2。

在auth令牌中存储组信息

您正在生成自己的身份验证令牌,并且您知道用户在进行身份验证时所处的组,您可以将组列表存储在您生成的身份验证令牌中。例如,为用户'fred'生成授权令牌时,请包括{id:'fred',groups:{alpha:true,beta:true}} < $ b

{rules:{数据:{$ thing:{.read:auth [data.child('group').val()]!= null,。写:auth [data.child('group').val()]!= null} } } }

I couldn't find it in the docs but is there a way to define a group of users and use that group to grant access to different locations, rather than granting access to individual users?

Regards, LT

解决方案

There's no explicit support for "groups" in Firebase, because you can represent them yourself quite easily. Here are two options, depending on your situation.

Storing group information in firebase.

The following data could be used to represent 2 groups ('alpha' and 'beta') and 3 pieces of protected data ('thing1', 'thing2', and 'thing3')

{ "groups": { "alpha": { "joe": true, "sally": true }, "beta": { "joe": true, "fred": true } }, "data": { "thing1": { "group": "alpha" /* data accessible only by the "alpha" group */ }, "thing2": { "group": "beta" /* data accessible only by the "beta" group */ }, "thing3": { "group": "alpha" /* more data accessible by the "alpha" group */ } } }

Then we can use the following rules to enforce security:

{ "rules": { "data": { "$thing": { ".read": "root.child('groups').child(data.child('group').val()).hasChild(auth.id)", ".write": "root.child('groups').child(data.child('group').val()).hasChild(auth.id)" } } } }

So then if I'm authenticated with { id: 'sally' } as my auth object, I'll have access to thing1 and thing3, but not thing2.

Storing group information in the auth token.

If you're generating your own auth tokens and you know what groups a user is in at the time they auth, you could store the list of groups in the auth token you generate. For example, when you generate the auth token for user 'fred', include "{ id: 'fred', groups: { alpha: true, beta: true } }"

And then you can enforce group membership with:

{ "rules": { "data": { "$thing": { ".read": "auth[data.child('group').val()] != null", ".write": "auth[data.child('group').val()] != null" } } } }

更多推荐

将访问Firebase位置授予一组用户

本文发布于:2023-11-28 11:55:10,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   用户   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!