Firestore规则根据属性布尔值写入访问权限

时间:2020-09-30 13:55:55

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

enter image description here

我有一个像上面的结构。我要实现的是在chat_room用户中只能在用户数组中将此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;
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

听起来像你想要的

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

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