gpt4 book ai didi

firebase - 如何为普通成员编写安全规则

转载 作者:行者123 更新时间:2023-12-03 17:49:11 25 4
gpt4 key购买 nike

我正在尝试创建一个安全规则,允许组中的任何用户读取同一组中任何其他用户的信息。换句话说,一个用户应该能够读取属于一个公共(public)组的任何用户的用户信息。

这就是我所拥有的:

{
"rules": {
"users": {
"$user_id": {
// Any user beloging to at least one group in common should be able to read
".read": "$user_id === auth.uid || root.child('users/' + $user_id + '/groups').hasAny(root.child('users/' + auth.uid + '/groups'))",
".write": "$user_id === auth.uid",

"groups": {
"$group_id": {
".validate": "root.child('groups/' + $group_id).exists() && newData.isBoolean()"
}
}
}
},

"groups": {
"$group_id": {
"name": { ".validate": "newData.isString() && newData.val().length > 0 && newData.val().length < 50" }
}
},

"members": {
"$group_id": {
".read": "root.child('members/' + $group_id + '/' + auth.uid).exists()",
".validate": "root.child('groups/' + $group_id).exists()",
"$user_id": {
".write": true, // Skipped for brevity
".validate": "root.child('users/' + $user_id).exists() && newData.isBoolean()"
}
}
},
}
}
}

当然, hasAny函数不是 API 的一部分。有没有办法用现有的 API 做到这一点?有没有计划添加这样的东西?

最佳答案

维护用户的好友列表。

您必须保留对用户 friend 列表的引用。当用户加入群组时,添加群组成员的auth.uid给用户的friends列表。然后,只有他的 friend 可以阅读他的个人资料。

{"rules":{
// $user_id == current user's auth.uid
// $friend_id == his friend's auth.uid
// $member_id == group member's auth.uid
"users":{"$user_id":{
"friends":{"$friend_id":{
}},

// readable by my friends:
".read":"auth =! null && data.child('friends').hasChild(auth.uid)"

}},
"groups":{"$group_id":{
"members":{"$member_id":{
}}
}}
}}

关于firebase - 如何为普通成员编写安全规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25374984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com