带有参考字段的 Firestore 安全规则 [英] Firestore security rules with reference fields

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

问题描述

我有点卡在这里,因为无法调试这些规则.我很感激以下规则的帮助.

I am a bit stuck here as there is no way to debug those rules. I'd appreciate help with below rules.

我要访问:

/modules/module-id/sessions/session-id/parts/

hasCompletedPrerequisiteSession()第一部分与null的比较效果很好,第二部分不行!

The comparison with null in the first part of hasCompletedPrerequisiteSession() works well, the second part doesn't!

路径 /modules/moduleId/sessions/sessionId/prerequisite 指向引用字段.

The path /modules/moduleId/sessions/sessionId/prerequisite points to a reference field.

service cloud.firestore {
    match /databases/{database}/documents {

      function hasCompletedPrerequisiteSession(moduleId,sessionId) {
                // this part works well                                   
        return getPrerequisiteSession(moduleId,sessionId) == null ||
           // !!! this part does not work !!!
           hasCompleted(getPrerequisiteSession(moduleId,sessionId).id);
      }

      function getPrerequisiteSession(moduleId,sessionId) {
        return get(/databases/$(database)/documents/modules/$(moduleId)/sessions/$(sessionId)).data.prerequisite;
      }

      function hasCompleted(sessionId) {
        return exists(/databases/$(database)/documents/progress/$(request.auth.uid)/sessions/$(sessionId));
      }

      match /modules/{moduleId}/sessions/{sessionId}/parts/{partId} {
        allow read: if hasCompletedPrerequisiteSession(moduleId,sessionId);
      }
    }
  }

(如果我将会话 ID 存储为字符串而不是对会话的引用,则它可以正常工作.)

(If I store the session ID as a string instead of a reference to the session, it works fine.)

编辑

问题

  1. 安全规则中的引用字段. 假设 modules/moduleId/owner 指向 reference 类型的字段.获取引用文档的 id 的正确方法是什么?get(../modules/moduleId).data.owner.data.idget(../modules/moduleId).data.owner 还是别的什么?
  1. Reference field in security rules. Assuming modules/moduleId/owner points to a field of the type reference. What is the proper way to get the id of the referenced document?get(../modules/moduleId).data.owner.data.id or get(../modules/moduleId).data.owner or something else?

推荐答案

来自 Firebase 支持:

From Firebase support:

似乎在您的用例中,您想从参考字段的值(先决条件)中获取文档名称 (sessionId),不幸的是,Firestore 安全规则目前不支持此功能.我建议您只将 sessionId 作为字符串存储在 prerequisite 字段中,或者您也可以为 sessionId 添加字符串字段.请记住,exists() 和 get() 函数仅允许您检查文档是否存在,或检索给定路径中的文档.

It seems that in your use case, you want to get the document name (sessionId) from the value of your reference field (prerequisite), unfortunately, this is not currently supported by Firestore security rules. I would suggest that you store only the sessionId as String on your prerequisite field, or you can also add String field for the sessionId. Keep in mind that the exists() and get() functions only allow you to check if a document exists, or retrieve the document at the given path.

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

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