首先,在实时数据库中,avoid using arrays 并改用地图。
改变这个:
"Allowed": {
"0": "8ZiQGBPFkiZOLgLJBgDeLw9ie9D3",
"1": "KEuhrxnAWXS0dnotjhjFAYUOcm42",
"2": "48yULftKSxgyS84ZJC4hs4ug4Ei2"
}
到这里:
"Allowed": {
"8ZiQGBPFkiZOLgLJBgDeLw9ie9D3": true,
"KEuhrxnAWXS0dnotjhjFAYUOcm42": true,
"48yULftKSxgyS84ZJC4hs4ug4Ei2": true
}
阅读链接的博客文章了解更多信息,但简而言之,它使添加/删除用户变得非常简单:
const groupRef = firebase.database.ref(`Groups/${groupId}`);
// add a user
groupRef.child("E04HLbIjGDRUQxsRReHSKifaXIr2").set(true);
// remove a user
groupRef.child("KEuhrxnAWXS0dnotjhjFAYUOcm42").remove();
您还可以将true 更改为您想要的任何内容。下面是一些例子:
-
false = 参与者,true = 主持人
-
false = 只读,true = 可以编辑
- 角色名称:
"member"、"admin"、"moderator"等
- 权限级别:
0(成员)、500(版主)、1000(所有者)等(确保将它们分开,您不希望在两者之间添加一个级别0 和 1 并且必须编辑您的整个数据库)。
不过,最重要的一点是实时数据库安全规则不了解数组。 data.val() 不会返回数组,它只会返回一个标记值,上面写着“非空对象在这里!”。这意味着安全规则需要地图。
This reference document 涵盖了您可以在实时数据库安全规则中使用的结构和变量。
根据您提议的规则,您尝试允许组中的任何用户都能够写入组的数据 - 但您无法管理他们可以写入和不可以写入的内容。群组中的任何恶意成员都可以添加/删除其他任何人,使自己成为所有者,甚至完全删除群组。
{
"rules": {
"Groups" : {
"$group": {
// If this group doesn't exist, allow the read.
// If the group does exist, only the owner & it's members
// can read this group's entire data tree.
".read": "!data.exists() || (auth != null && (data.child('Owner').val() === auth.uid || data.child('Allowed').child(auth.uid).val() === true))",
"Owner": {
// Only the current owner can write data to this key if it exists.
// If the owner is not yet set, they can only claim it for themselves.
".write": "auth != null && (data.val() === auth.uid || (!data.exists() && newData.val() === auth.uid))",
// Force this value to be a string
".validate": "newData.isString()"
},
"Allowed": {
// Only the owner can edit the entire member list
// For a new group, the owner is also granted write access
// for it's creation
".write": "auth != null && (data.parent().child('Owner').val() === auth.uid || (!data.exists() && newData.parent().child('Owner').val() === auth.uid))",
"$member": {
// Allows the user to remove themselves from the group
".write": "auth != null && auth.uid === $member && !newData.exists()",
// Force this value to be a boolean
".validate": "newData.isBoolean()"
}
},
"Data": {
// The owner and members can edit anything under "Data"
// Currently this includes deleting everything under it!
// For a new group, the owner is also granted write access
// for it's creation
// TODO: tighten structure of "Data" like above
".write": "auth != null && (data.parent().child('Owner').val() === auth.uid || data.parent().child('Allowed').child(auth.uid).val() === true || (!data.exists() && newData.parent().child('Owner').val() === auth.uid))"
}
}
}
}
}