Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
881 views
in Technique[技术] by (71.8m points)

firebase - Firestore security rules with reference fields

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

I want to access:

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

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

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);
      }
    }
  }

enter image description here

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

Edit

Questions

  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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...