【问题标题】:Cloud Firestore security rules "if exists" not workingCloud Firestore 安全规则“如果存在”不起作用
【发布时间】:2019-09-27 20:17:41
【问题描述】:

我在 Cloud Firestore 上有一个简单的聊天应用程序,它具有以下安全规则,仅允许用户在应用程序本身“加入”聊天室时在聊天室中发送消息:

match /hangouts/{hangout}/messages/{message} {
        allow create, write, update, read: if exists(/databases/$(database)/documents/users/{userId}/hangout/{hangout});
    }

如果用户在 Firestore 上的收藏中有一个 hangoutID,则应该允许他们将消息发送到该 hangoutID。但是,我仍然收到错误消息:

Write at hangouts/ChIJPRVm2R7H54kRKLP2ttsuUko/messages/17225E70-B708-4033-AE5A-D0CBBD1BC69F failed: Missing or insufficient permissions.

我还有 2 条与环聊相关的其他规则。有没有可能这些干扰?

match /hangouts/{hangout} {
    allow read, update, write, create: if request.auth.uid != null;
}

match /hangouts/{hangout}/members/{userId} {
    allow read: if request.auth.uid != null;
  allow create, update, write, delete: if request.auth.uid == userId;
}

第一个是人们可以创建和查看聊天室环聊,第二个是允许人们加入他们。

我做错了什么?我对这个概念有点陌生。

谢谢!!

【问题讨论】:

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


    【解决方案1】:

    您似乎没有正确地对文档路径进行变量替换。在匹配部分,您使用 {花括号} 来指示通配符的位置,但在路径表达式中,您使用通配符的名称,如 $(this)。

    /databases/$(database)/documents/users/$(userId)/hangout/$(hangout)
    

    【讨论】:

    • 非常感谢您的解释!我还必须将$(userId) 更改为$(request.auth.uid),然后它就完美运行了。
    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 2021-05-12
    • 2019-09-06
    相关资源
    最近更新 更多