簡體   English   中英

如何調試 firestore.rules 變量和函數?

[英]How to debug firestore.rules variables and functions?

我在嘗試診斷 firestore.rules 文件中的特定規則時遇到困難。 在此處查看該問題以獲取上下文。

有沒有辦法調試 firestore.rules 文件和/或函數? 我正在使用單元測試和模擬器來測試我的規則,但我真的很想看看規則引擎正在評估哪些值。

例如,這是我的 firestore.rules 文件:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /organizations/{orgId} {
      allow read: if isAdmin();
      allow create, update: if isAdmin();

      match /classes/{classId} {
        allow read: if request.auth.uid != null;
        allow create, update: if isAdmin();

        match /students/{studentId} {
          allow read: if isAdmin() || belongsToCurrentClass();
          allow create, update: if isAdmin();
        }
      }
    }
  }
}

function isAdmin() {
  // removed for security
}

function belongsToCurrentClass() {
  // retuns true if the authenticated user is the teacher of the requested class
  return get(/databases/$(database)/documents/organizations/$(orgId)/classes/$(classId)).data.teacherUid == request.auth.uid;
}

我喜歡做的是設置斷點或逐步執行代碼。 當嘗試對 organizations/{orgId}/classes/{classId}/students/{studentId} 路徑進行 CRUD 操作時,我很想檢查 orgId、classId 和 studentId 變量所持有的確切值,以及資源和請求參數。 我想檢查 belongsToCurrentClass 中的 get 請求究竟返回了哪個文檔(如果有的話)以及返回值是什么。

有誰知道有什么辦法可以做到這一點? 如果我能看到正在評估的數據,我想我會在 10 秒內回答上面提到的問題。

Cloud Firestore 安全規則有一個本地模擬器。 這是您深入研究安全規則執行的最佳(也是唯一)工具。 沒有單步調試,但是在控制台可以看到很多debug output。

https://firebase.google.com/docs/rules/emulator-setup

我們可以將內置調試 function添加到規則中。 如評論中所述,您會在瀏覽器中看到這樣一條無用的消息:

Received: [path] Expected: [bool]. for 'list' @ L6

從好的方面來說,我們不會忘記刪除調試消息。 tail日志文件看到output: tail -f firestore-debug.log

例如,查看調用了哪些路徑:

match /databases/{database}/documents {
  match /{document=**} {
    allow create, read, update, delete: if debug(request.path);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM