首页 > 解决方案 > 读取、更新和删除的 Firestore 规则

问题描述

我是一名非开发人员,正在寻找有关 Firestore 项目安全规则的指导。

Firestore 数据库截图:

在此处输入图像描述

Swaps集合中的每个文档应该只能由两个用户访问,一个提供者和一个接收者。

要允许在Swaps集合中创建新文档,用户需要将自己指定为接收者,这意味着经过身份验证的用户 ID 必须与请求的ReceiverID匹配。

但是,发送者和接收者都应该被允许更新、阅读和删除文档。为此,我想将接收者与交换文档的ReceiverID匹配,将给予者与交换文档的GiverID 匹配

rules_version = '2';
service cloud.firestore {
 match /databases/{database}/documents {
    match /Swaps/{document=**} {
      allow read: if request.auth.uid == resource.data.GiverID;
      allow update: if request.auth.uid == resource.data.GiverID;
      allow delete: if request.auth.uid ==resource.data.GiverID;
      allow read: if request.auth.uid == resource.data.ReceiverID;
      allow update: if request.auth.uid == resource.data.ReceiverID;
      allow delete: if request.auth.uid == resource.data.ReceiverID;
      allow create: if request.auth.uid == request.resource.data.ReceiverID; 
    }

目前,唯一有效的接缝规则是创建。读取、更新和删除对提供者或接收者都不起作用。

标签: google-cloud-functionsfirebase-security

解决方案


试试这样:

rules_version = '2';
service cloud.firestore {
 match /databases/{database}/documents {
    match /Swaps/{Swap} {
      allow read: if request.auth.uid == resource.data.GiverID || request.auth.uid == resource.data.ReceiverID;
      allow update: if request.auth.uid == resource.data.GiverID || request.auth.uid == resource.data.ReceiverID;
      allow delete: if request.auth.uid == resource.data.GiverID || request.auth.uid == resource.data.ReceiverID;
      allow create: if request.auth.uid == request.resource.data.ReceiverID; 
    }

推荐阅读