Firestore安全规则检查引用是否存在

时间:2018-01-05 17:27:44

标签: firebase firebase-security google-cloud-firestore

我想知道如何检查文档值是否是对另一个文档的引用,并且该文档是使用firebase安全规则存在的。

我尝试了什么:

public class Demo {
    public static void main(String[] args) {
        Set<Double> tempSet = new HashSet<Double>();
        tempSet.add(11.0);
        tempSet.add(22.0);

        // Below both lines not working
        Set<Double> tempSet1 = new HashSet<Double>({11.0, 22.0});
        Set<Double> tempSet1 = new HashSet<Double>(){11.0, 22.0};
    }
}

由于这不起作用,我试图找出function validate(document) { return exists(document.reference) } match /collection/{document} { allow read: if request.auth != null; allow create, update: if isAdmin(request.auth.uid) && validate(request.resource.data); } 的类型。 不幸的是,它似乎没有任何类型的列出的: https://firebase.google.com/docs/firestore/reference/security/?authuser=0#data_types

我尝试了document.ref,因为它是最明显的合作方式。 我没想到它会起作用。 另一个猜测可能是pathmap。 两者都不正确。

由于我不知道这可能是什么,并且没有任何记录我如何将引用转换为路径,我现在必须在这里问。

有没有人找到解决方案?

TL; DR:

我需要使用Firestore安全规则检查文档中保存的引用是否存在且是否存在于数据库中。

谢谢, 丹尼斯

3 个答案:

答案 0 :(得分:5)

在这一行:

allow create, update: if isAdmin(request.auth.uid) && validate(request.resource.data)

而不是调用“request.resource.data”,你应该只调用“resource.data”:

allow create, update: if isAdmin(request.auth.uid) && validate(resource.data)

如上所述here,资源变量表示Firestore文档,而“request”变量表示在该路径上发出的请求,因此不包含有关文档中实际值的信息。

尝试一下,让我知道它是否适用于你。

答案 1 :(得分:2)

根据exists() documentation

  

提供的路径必须以/databases/$(database)/documents开头。

因此,您应该将验证功能更改为:

function validate(document) {
    return exists(/databases/$(database)/documents/$(document.reference))
}

答案 2 :(得分:2)

您可以使用resource == nullresource != null

检查资源是否已经存在。

示例:

allow write: if resource == null //Only can create, not update
相关问题