如何以编程方式修改Firebase中的安全规则? [英] How to programmatically modify security rules in Firebase?

查看:103
本文介绍了如何以编程方式修改Firebase中的安全规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase文档中的示例假设手动更新了Firebase安全规则。如何以编程方式修改安全规则以支持真实世界的协作应用程序?在我正在考虑的用例中,要求用户能够邀请其他用户协作/共享选定的数据,并能够授予/撤销对协作者的访问权限。如何用firebase实现这个功能?

解决方案

您实际上不应该以编程方式更改您的安全规则。您应该将它们视为代码,并且只在部署时更改它们。



如果您要做的是更改用户实际上允许这样做,您应该通过编写依赖Firebase中数据的安全规则来实现这一点。

数据仅限于特定组中的用户。在每次更改组成员身份之前,您只需将该组存储在Firebase中,然后在安全规则检查之前查看当前用户是否在该组中,然后再允许访问。


$ b ($ group $).child(auth.userid).exists()
$。

通过这种方式,只要组成员资格发生变化,用户就会被自动授予对允许查看的数据的访问权限。 / p>

有关安全规则的更复杂示例,请查看 Firefeed


The examples in the firebase documents assume manual update of firebase security rules. How can security rules be modified programmatically to support real-world collaborative applications? In the use case that I am considering, the requirement is for a user to be able to invite other users to collaborate/share selected data and to be able to grant/revoke access to the collaborators. How can this be accomplished with firebase?

解决方案

You actually shouldn't programmatically change your security rules. You should think of them as code and only change them at deploy time.

If what you're trying to do is change what users are actually allowed to do, you should do this by writing security rules that depend on data in your Firebase.

For example, lets say you wanted to restrict access to a piece of data to only users in a specific group. Rather than modifying the security rules everytime the group membership changed, you would simply store the group in Firebase and have your security rules check to see if the current user is in that group before allowing access.

".read" : "root.child('groups').child($groupID).child(auth.userid).exists()"

That way, anytime group membership changes, users will automatically be granted access to the data they should be allowed to see.

For a more complex example of security rules, take a look at the rules.json file in Firefeed.

这篇关于如何以编程方式修改Firebase中的安全规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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