Firebase Firestore rules: will get()'ing the document being checked counted as another read?

纵饮孤独 提交于 2021-02-08 19:32:07

问题


I have a code in Firestore security rules like this (showing only relevant parts):

match /users/{userId} {
    function isUserDisabled(userId) {
        return get(/databases/$(database)/documents/users/$(userId))
                    .data.admin == true;
    }

    allow read:  if true;
    allow write: if request.auth.uid == userId
                 && !isUserDisabled(userId);

    match /posts/{postId} {
        allow read:  if true;
        allow write: if request.auth.uid == userId
                     && !isUserDisabled(userId);
    }
}

isUserDislabled() is a function that will be called in multiple places. It will get() the user's document to see if the user is disabled or not.

The question is, will the first call to isUserDislabled(), which cause a get() to the checking document, cost an additional read? I understand that multiple get() to the same document will be billed only once. However, in this case, it's the only get() for the particular document.

Edit 1: Seems like my question isn't exactly what I meant. (I'm not a native English speaker.) The question is, when the get() to /users/{userId} happens in the match /users/{userId} block (and not the subblock), will the get() cause additional read or not? In other words, between using get() and using resource variable (when it refers to the same document, of course), will it cost the same or not?


回答1:


Yes, that will count as a document read. Any time a document needs to be read it counts as such, and reading it from security rules is no different than reading it from other code.



来源:https://stackoverflow.com/questions/50294547/firebase-firestore-rules-will-geting-the-document-being-checked-counted-as-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!