简体   繁体   English

在Cloud Firestore中如何制作,只创建一次安全规则

[英]In Cloud Firestore how to make an, create only once security rule

I just stared migrate to Cloud Firestore and wonder about this security rule. 我只是盯着迁移到Cloud Firestore并想知道这个安全规则。 In the Firebase realtime databas this rule: 在Firebase实时数据库中这条规则:

Evaluates to true if one operand in the rules expression is true. 如果rules表达式中的一个操作数为true,则求值为true。

In this example, we can write as long as old data or new data does not exist. 在此示例中,只要旧数据或新数据不存在,我们就可以编写。 In other words, we can write if we're deleting or creating data, but not updating data. 换句话说,如果我们删除或创建数据,我们可以写,但不能更新数据。

".write": "!data.exists() || !newData.exists()" “。write”:“!data.exists()||!newData.exists()”

I´m trying to replicate in the Cloud Firestore like 我试图在Cloud Firestore中复制

 match /USER_ID/{Id} { allow create: if resource.data.id != exist allow read: if request.auth.uid != null; } 

What I want is that if the Document exist in the USER_ID Collection then the Transaction this is happening in must fail. 我想要的是,如果Document存在于USER_ID Collection那么发生的事务必须失败。 But this is not working. 但这不起作用。 I have read the doc a few times but cant get it to work 我已经阅读了几次这个文档但是无法让它工作

You'll want to use the exist function, for which you need to pass it the path of the document you are testing if it exists. 您将需要使用exists函数,您需要将其传递给您正在测试的文档的路径(如果存在)。 Below is an example of how you'd check if the document doesn't exist. 下面是如何检查文档是否不存在的示例。

service cloud.firestore {
   match /databases/{database}/documents/ {
      match /USER_ID/{Id} {
         allow create: if !exists(/databases/$(database)/documents/USER_ID/$(Id))
         allow read: if request.auth.uid != null;
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM