这里有下面的示例应用程序:Github回购
它在呜呜火中使用ChatList.vue。
// vuefire firestore component manages the real-time stream to that reactive data property.
firestore() {
return {
chats: db.collection('chats').where('members', 'array-contains', this.uid)
}
},我现在编写了安全规则来保护数据,但似乎无法将vuefire和安全规则结合起来:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
// THIS IS THE PART I'D LIKE TO REMOVE
match /chats/{chatId=**} {
allow read: if request.auth.uid != null;
}
// THIS WORKS AS INTENDED, AND I'D LIKE TO INCLUDE "READ"
match /chats/{chatId}/{documents=**} {
allow write: if chatRoomPermission(chatId)
}
function chatRoomPermission(chatId) {
return request.auth.uid in get(/databases/$(database)/documents/chats/$(chatId)).data.members;
}
}
}因此,其目标是:使单个聊天仅可读和可写的用户是在成员数组中的防火墙。(目前,我部分地实现了这一点,因为所有的聊天都是任何人都能读到的,但只能写给成员数组中的用户。)
我是否必须重写vuefire组件,以便有以下安全规则?(它提供了一条错误消息:由于缺少权限而无法列出聊天)
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
match /chats/{chatId}/{documents=**} {
allow read, write: if chatRoomPermission(chatId)
}
function chatRoomPermission(chatId) {
return request.auth.uid in get(/databases/$(database)/documents/chats/$(chatId)).data.members;
}
}
}为了完整,工作解决方案是(给Renaud Tarnec的学分):
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
match /chats/{chatId=**} {
allow read: if request.auth.uid in resource.data.members;
}
match /chats/{chatId}/{documents=**} {
allow read, write: if chatRoomPermission(chatId)
}
function chatRoomPermission(chatId) {
return request.auth.uid in get(/databases/$(database)/documents/chats/$(chatId)).data.members;
}
}
}发布于 2020-05-25 08:56:44
由于要在安全性规则中检查给定值(本例中为用户uid )是否包含在文档中的类型数组字段中,因此可以使用List类型的in运算符。
因此,以下几点应该起到了作用:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
// THIS IS THE PART I'D LIKE TO REMOVE
match /chats/{chatId=**} {
allow read: if request.auth.uid in resource.data.members;
}
// ....
}
}https://stackoverflow.com/questions/61997570
复制相似问题