Firestore安全规则获取参考字段/标识 [英] Firestore security rules get field/id of reference

查看:86
本文介绍了Firestore安全规则获取参考字段/标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个集合-租约和用户.

I have two collections - tenancies and users.

租户文档的字段名为"landlordID",类型为REFERENCE(不是String).

A tenancy doc has a field called "landlordID" and is of type REFERENCE (not String).

现在,在我的Firestore安全规则中,仅当租约的landlordID字段与发出请求的用户的uid(即request.auth.uid)匹配时,我才希望允许更新租约.

Now in my Firestore Security Rules I want to allow a tenancy to be updated ONLY IF the landlordID field of that tenancy matches with the uid of the user making the request, namely request.auth.uid.

将其读取为如果使用户身份经过认证的租户文档得以更新,则request.auth.uid!= null,并且landlordID字段的ID应与request.auth.uid的ID相等.

Read it as " allow a tenancy document to be updated if the user making the user is authenticated, hence request.auth.uid != null, and the landlordID field's ID should be equal to that of the request.auth.uid.

因此,代码应该是这样的:

Hence the code should me something like this:

    service cloud.firestore {

      match /databases/{database}/documents {

        match /tenancies/{tenancyID}{

            allow update: if request.auth.uid != null && 
                        request.auth.uid == get(resource.data.landlordID).id
    }

}

我也尝试过get(/databases/$(database)/documents/users/$(resource.data.landlordID)).data.id

支持我的数据库的屏幕截图

Supporting screenshot of my database

这应该非常简单,但是get()根本不起作用. Firebase文档,滚动到访问其他文档" 根本没有帮助对于我的情况,我不确定如何使它正常工作.

This should be very simple but get() simply does not work. Firebase Docs, scroll to "Access other documents" was not helpful at all for my situation and I am not sure how to get it working.

如果引用不能像文档的任何其他字段那样使用,将是一种耻辱.

It would be a shame if references can't be used like this as they are just like any other field of a document.

推荐答案

我在这里看到了几个问题.第一个问题是get()函数需要一个完全指定的文件路径,例如:

I see a couple of issues here. A first problem is that the get() function expects a fully specified ducument path, something like:

get(/databases/$(database)/documents/users/$(resource.data.landlordID)).data.id

第二个问题是您正在尝试在规则中使用引用类型,不幸的是,我认为这是不可能的.

A second problem is that you are trying to use the reference type in your rules, I do not think that is possible unfortunately.

Firestore中的引用类型不是非常有用(还),我认为您应该将地主ID存储为字符串,然后可以简单地执行以下操作:

The reference type in Firestore is not very helpfull (yet), I think you should store the landlordID as a string, then you can simply do something like:

service cloud.firestore {

      match /databases/{database}/documents {

        match /tenancies/{tenancyID}{

            allow update: if request.auth.uid != resource.data.landlordID;

    }

}

这篇关于Firestore安全规则获取参考字段/标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆