firebase - 如何设置 Firebase 安全读取规则
问题描述
我正在创建一个聊天系统,并且有一个搜索功能,如果用户的个人资料是公开的,我可以在其中搜索用户。我的firebase数据库结构如下。
users:{
$uid1:{
name:"John",
created:13739373,
type:"PRIVATE"
},
$uid2:{
name:"Susan",
created:12393930,
type:"PUBLIC"
},
$uid3:{
name:"Titan",
created:12582938,
type:"CLOSED"
}
}
所以,我为这样的用户read
设置了规则。
".read":"data.child('type').val()==='PUBLIC' || //if user is already friend, alse allow type CLOSED"
如果类型是或(如果两个用户已经是朋友),如何为用户read
完美设置规则。我无法设置规则,因为这是搜索功能。PUBLIC
CLOSED
read
$uid
编辑:这是我的friends
道路。
friends:{
$uid1:{
$uid2: 1273738, //friend since timestamp
$uid3: 1274693,
$uid4: 1284869,
...
}
}
Edit2: PUBLIC
每个人都可以看到,PRIVATE
没有人可以看到/搜索,CLOSED
只有朋友可以看到。
解决方案
您必须更新的 Firebase 实时规则是,
"users": {
"$user_id":{
".read": "data.child('type').val() == 'PUBLIC' || (data.child('type').val() == 'CLOSED' && auth != null && root.child('friends').child($user_id).child(auth.uid).val() > 0 ) || (data.child('type').val() == 'PRIVATE' && auth != null && $user_id == auth.uid ) "
}
推荐阅读
- javascript - 在图像上添加产品列表
- ios - Alamofire 不发出 GET 请求,而是使用 Success 调用完成处理程序
- mocking - 如何在没有嵌入式服务器或新集群的情况下模拟 couchbase 以进行验收测试?
- angular-material - 单个模板中的材料角多表数据源 + 材料角多表“找不到具有 id 的列”
- go - Go 变量初始化语法
- java - 如何将多个 Spring Boot 应用程序合并为一个?
- java - 如何验证非必需的 RequestParam 不为空?
- javascript - 输入中的格式化数字反应
- sql - 选择中的条件语句
- reactjs - 使用 AsyncStorage 在 React Native 中执行 CRUD 操作