【问题标题】:Firestore rules : check the existence of a value in arrayFirestore 规则:检查数组中是否存在值
【发布时间】:2022-02-20 21:17:14
【问题描述】:

我想只授予成员组中存在的用户读写权限,但我不知道为什么它不起作用?

这是firestore中的规则:

    rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
  match /Users/{uid} {
  allow write: if request.auth != null && request.auth.uid == uid;
  allow read : if request.auth != null;
  }
    
    match /GroupChat/{document=**} {
      allow read,create,update: if request.auth != null && request.auth.uid in request.resource.data.Members;
        
    }
  }
}

这是我的数据库的屏幕

查询:

FirebaseFirestore.instance
          .collection("GroupChat")
          .where("Members",
              arrayContains: FirebaseAuth.instance.currentUser!.uid)
          .snapshots(),

当我想在我的APP中显示信息时,我得到了这个错误:

W/Firestore(14600): (24.0.0) [Firestore]: Listen for Query(target=Query(GroupChat where Members array_contains 0FQhApDgWMVNTpOiDewF3qJX7IA3 order by name);limitType=LIMIT_TO_FIRST) 失败:状态{code=PERMISSION_DENIED, description=缺少权限或权限不足。, cause=null}

【问题讨论】:

  • 你在真机上运行过应用吗?
  • 不,我还没有
  • 错误描述=断开空闲流可能是模拟器的原因,请在真机中检查然后回复。
  • 我认为我在使用 xcode 运行 Iphone 时遇到问题,所以我只是重新启动 IDLE,现在它给了我这个错误:W/Firestore(14600): (24.0.0) [Firestore]:侦听 Query(target=Query(GroupChat where Members array_contains 0FQhApDgWMVNTpOiDewF3qJX7IA3 order by name);limitType=LIMIT_TO_FIRST) 失败:Status{code=PERMISSION_DENIED, description=Missing or enough permissions., cause=null} I在我的问题正文中更改了谢谢@Debjeet
  • 您可以尝试将规则更改为request.auth.uid in resource.data.Members; 而不是request.auth.uid in request.resource.data.Members;

标签: flutter google-cloud-firestore firebase-security


【解决方案1】:

正如documentation中提到的,

resource 变量引用请求的文档,resource.data 是存储在文档中的所有字段和值的映射。

但是,request.resource.data 包含在更新/写入操作中添加到文档中的数据。您应该使用resource.data,因为您想检查现有数据。

match /GroupChat/{document=**} {
  allow read,create,update: if request.auth != null && request.auth.uid in resource.data.Members;        
}

【讨论】:

    猜你喜欢
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    相关资源
    最近更新 更多