firebase - Firebase Firestore 安全规则:在安全规则中使用查询?
问题描述
我正在 Firebase 和 Firestore 上开发一个网络应用程序,这就是我想要做的事情:
用户可以上传自己的帖子,并将其可见性设置为公开或私密。
任何登录用户都可以查看公开帖子,但只有订阅作者的用户才能查看私人帖子。我正在尝试为此编写安全规则。
下面是数据库结构:
db.collection('posts').doc({postid})
//When a user writes a post, a new document is created. Includes the boolean 'public' field and the 'uid' field, which stores the writer's uid.
db.collection('subscription').doc({viewer_user_id})
//Once the logged-in user subscribes to another user, a document is created under the user's UID. The doc includes array of the UID of the users the viewer is subscribing.
以下是“posts”和“subscription”集合中每个文档下相关字段的描述:
posts/{postid}: {
'uid': Writer's uid (String)
'public': Boolean value to reflect visibility. If false, it is private.
}
subscription/{viewer's uid}: {
'subscription': Array of uids of the users the viewer is subscribing.
}
所以,对于私有文档,基本思路是在订阅集合中查看查看者的文档,获取数据,并查看那里是否包含了作者的uid。这将需要一些 Javascript 代码,但我不知道 Firestore 安全规则中的语法是什么,或者一开始是否有可能。
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function publicOrSubscribed() {
return request.auth.uid!=null&&(resource.data.public==true|| /*What could be the syntax here??*/)
}
match /posts/{postid} {
allow read: if publicOrSubscribed();
allow create: if request.auth.uid!=null;
allow update, delete: if request.auth.uid==resource.data.userid;
}
}
}
有什么建议么?如果在安全规则中不可能,解决方法可能是什么?提前致谢!
解决方案
推荐阅读
- scala - 将scala并行集合转换回常规序列
- php - Laravel:咨询队列失败,因为今天的日志文件无法打开
- python-3.7 - 如何在python中为二维列表中的元素赋值
- vue.js - 在 Vuetify 1.5 中,如何让 v-menu 在 v-toolbar 中看起来不错?
- promise - 承诺未决
- python - Pandas 错误:字符串索引必须是整数
- node.js - 从服务器获取数据库名称
- javascript - array.every() 不会影响数组的任何元素?
- r - 拆分列中的值并将其重新分配给新列
- python - 使用图遍历(AQL 和 Python)删除文档/边的最佳实践