【问题标题】:Firestore rule to write access based on property bool value基于属性 bool 值写入访问的 Firestore 规则
【发布时间】:2025-12-06 20:15:01
【问题描述】:

我有一个像上面这样的结构。我想要实现的是在聊天室用户只能在用户数组中写入这个 uid.isActive == true

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
     match /users/{userId} {
      allow update, delete: if request.auth != null && request.auth.uid == userId;
      allow create, read: if request.auth != null;
    }
    match /chats/{chatId} {
      allow read, write: if request.auth.uid != null;
      match /chat_room/{roomId} {
        allow read, write: if request.auth.uid != null;
      }
    }
  }
}

【问题讨论】:

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


    【解决方案1】:

    听起来像你想要的:

    match /chat_room/{roomId} {
      allow read, write: if get(/databases/$(database)/documents/chats/$(chatId)).data.users[request.auth.uid].IsActive == true;
    }
    

    因此,当写入 chat_room 文档时,它会加载父文档,然后检查用户是否在其中被标记为活动。

    【讨论】: