Mic*_*uer 34
Firebase中没有对"群组"的明确支持,因为您可以很容易地自己代表他们.根据您的具体情况,这里有两个选项.
以下数据可用于表示2组('alpha'和'beta')和3条受保护数据('thing1','thing2'和'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 */
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我们可以使用以下规则来强制执行安全性:
{
"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)"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么如果我使用{id:'sally'}作为我的auth对象进行身份验证,我将可以访问thing1和thing3,但不能访问thing2.
如果您正在生成自己的身份验证令牌,并且您知道用户在授权时所在的群组,则可以将群组列表存储在您生成的身份验证令牌中.例如,当您为用户'fred'生成身份验证令牌时,请包含"{id:'fred',groups:{alpha:true,beta:true}}"
然后您可以通过以下方式强制执行组成员身份
{
"rules": {
"data": {
"$thing": {
".read": "auth[data.child('group').val()] != null",
".write": "auth[data.child('group').val()] != null"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7262 次 |
| 最近记录: |