【问题标题】:Firestore rules to Read, Update and Delete读取、更新和删除的 Firestore 规则
【发布时间】:2020-01-10 13:00:57
【问题描述】:

我是一名非开发人员,正在寻找有关 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; 
    }

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

【问题讨论】:

  • 您应该做的第一件事是删除read 规则。您拥有它们两次,并且由于您想使用细化规则,因此不需要它们(实际上可能会导致问题)。如果在那之后您仍然有问题,请编辑您的问题以更新规则,并包含不起作用的操作代码(因为规则只能从您针对它们运行的​​代码中获得意义)。

标签: google-cloud-functions firebase-security


【解决方案1】:

试试这样:

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; 
    }

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 2021-09-10
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多