首页 > 解决方案 > 带有数组/列表属性的 Firebase 安全规则语法

问题描述

在具有路径“组织”的 Firebase Firestore 集合中,每个文档都包含可以更新或删除该文档的用户的字符串 userID 列表。

export interface Organization{
  name?: string,
  owners: string[]
}

我想创建一个 Firebase 安全规则,以确保只有具有此列表中 uid 的登录用户才能编辑或删除该对象。不确定适当的语法。

service cloud.firestore {
  match /databases/{database}/documents {
    match /organizations/{organization} {
      allow read: if true;
      allow create: if request.auth != null;

      /// What should be the syntax here?
      allow update, delete: if request.auth != null && (request.auth.uid in resource.data.owners); // <--------- What should be the syntax for this line?

    }

标签: firebasegoogle-cloud-firestorefirebase-security

解决方案


好的,在这里回答我自己的问题,以防它对其他人有用。

看起来上面的“in”语法实际上有效——尽管这是一个完整的猜测,我无法在 firebase 安全角色文档中找到任何文档。

最终代码:

service cloud.firestore {
  match /databases/{database}/documents {
    match /organizations/{organization} {
      allow read: if true;
      allow create: if request.auth != null;
      allow update, delete: if (request.auth != null) && (request.auth.uid in resource.data.owners); 
    }

推荐阅读