我正在使用Cloud Firestore,我似乎无法让“IN”运算符使用安全规则。我尝试过使用数组和地图,但都没有工作。当然,当我将它设置为allow read, write;
时,它可以正常工作。我做错了什么?
规则:
service cloud.firestore {
match /databases/{database}/documents {
match /rooms/{roomId=**} {
allow read, write: if request.auth.uid in resource.data.users;
allow read, write: if request.auth.uid in resource.data.users2;
allow create: if request.auth != null;
}
match /user-rooms/{userId} {
allow read, write: if userId == request.auth.uid;
}
match /users/{userId} {
allow read, write: if request.auth.uid == userId;
allow get, create: if request.auth != null;
}
}
}
客户:
db.collection("rooms")
.document(self.room.getRoomId())
.collection("messages")
.addSnapshotListener { .....
//Room is: d6l946swspNSouANzVdZ
//Username is: eX8gkxJNDREv
答案 0 :(得分:2)
答案 1 :(得分:1)
您正在尝试访问名为"用户"的变量。在resource.data
里面并不存在。 resource变量包含当前正在写入数据库的对象的数据。
您可能尝试做的是检查users
和users2
字段中是否存在此用户,这可以通过以下规则实现:
match /rooms/{roomId=**}{
allow read, write: if (exists(/databases/$(database)/documents/rooms/$(roomId)/users2/$(request.auth.uid)) ||
request.auth.uid in get(/databases/$(database)/documents/rooms/$(roomId)).data.users);
allow create: if request.auth!=null;
}