Firestore 对子集合的规则

Chr*_*ais 4 firebase firebase-security google-cloud-firestore

我在 firestore 上有一个“游戏”集合,其中包含一个“级别”子集合。我正在尝试设置安全规则,以便您只能访问您创建的游戏或关卡。

所有文档(游戏或关卡)都有一个authorId字段,其中包含创建它们的用户的 uid。我已经尝试过这个规则,但仍然出现Missing or insufficient permissions错误:

service cloud.firestore {
  match /databases/{database}/documents {    
    match /games/{document=**} {
        allow read, write: if document.data.authorId == request.auth.uid;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

我也尝试了以下规则但没有成功:

service cloud.firestore {
  match /databases/{database}/documents {    
    match /games/{game}/levels/{level} {
        allow read, write: if level.data.authorId == request.auth.uid;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)
service cloud.firestore {
   match /games/{game} {
     allow read, write: if game.data.authorId == request.auth.uid;     

       match /levels/{level} {
          allow read, write: if level.data.authorId == request.auth.uid;
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 7

根据参考文档,资源是包含用户尝试编写的文档数据的对象。您可以使用其数据属性来获取其字段值。

service cloud.firestore {
  match /databases/{database}/documents {    
    match /games/{document=**} {
        allow read, write: if resource.data.authorId == request.auth.uid;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)