firebase - Firestore 无效权限 : request. auth.uid in

标签 firebase firebase-security google-cloud-firestore

我正在使用 Cloud Firestore,但似乎无法让“IN”运算符使用安全规则。我试过使用数组和 map ,但都不起作用。当然,当我将它设置为 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

enter image description here

最佳答案

data将返回它的直接 child 而不是它的子 child (用户和用户2)所以你应该使用 getexists而不是 in

match /rooms/{roomId=**} { 
  allow read, write: if request.auth.uid in get(/databases/$(database)/documents/rooms/$(roomId)/users/$(request.auth.uid)).data;
  allow read, write: if exists(/databases/$(database)/documents/rooms/$(roomId)/users2/$(request.auth.uid));
  allow create: if request.auth != null;
}

结帐 doc

关于firebase - Firestore 无效权限 : request. auth.uid in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48496288/

相关文章:

Firebase Cloud Functions 部署错误,错误代码为 "NoSuchKey"

android - 如何在 firebase 中检查用户电子邮件和密码验证以登录

firebase - 如何在 Firestore 中强制执行数据库架构?

swift - 如何使用 Firestore 合并两个查询 - Swift

firebase - Flutter 多个 Firestore 查询

angular - 在 Firebase 上托管 Angular 项目

ios - 根据 uid 访问 Firebase 存储镜像

Flutter Firestore 身份验证

android - 无法让安全规则级联到子集合+允许创建不起作用

firebase - 如何在 Firestore onCreate 触发器中中止文档创建