Firebase rules - Simulator says yes, code says no











up vote
2
down vote

favorite
1












I'm working on a Flutter app using Firebase as a backed. I've set up group based roles in Firebase and the rules simulator in Firebase tells me the user I'm testing has access to the document. When I do a query in my Flutter code, I can see it finds the document and I can see it for a split second before it changes it mind and I get a "Listen for query at students failed: Missing or insufficient permissions." and the document is removed from the snapshot.



The query I use in the Flutter code is as follows:



Firestore.instance.collection('students').where('test', arrayContains: userID).orderBy('name').snapshots()


I have been playing with the document and tried different approaches for the current user to query for the document, and just to test it out I created an array with the userId and look for that.



If I completely skip the rules and just put the "need to be logged in" as requirement then I get a document back but as soon as I use the role based one then it's back to the drawing board. The rules I've set up are:



service cloud.firestore {
match /databases/{database}/documents {
match /students/{student} {
function isSignedIn() {
return request.auth != null;
}
function getRole(rsc) {
return rsc.data.roles[request.auth.uid];
}
function isOneOfRoles(rsc, array) {
return isSignedIn() && (getRole(rsc) in array);
}
allow read, write : if isOneOfRoles(resource,['teacher', 'student', 'parent']);
}
}
}


Any idea what's causing this?










share|improve this question




























    up vote
    2
    down vote

    favorite
    1












    I'm working on a Flutter app using Firebase as a backed. I've set up group based roles in Firebase and the rules simulator in Firebase tells me the user I'm testing has access to the document. When I do a query in my Flutter code, I can see it finds the document and I can see it for a split second before it changes it mind and I get a "Listen for query at students failed: Missing or insufficient permissions." and the document is removed from the snapshot.



    The query I use in the Flutter code is as follows:



    Firestore.instance.collection('students').where('test', arrayContains: userID).orderBy('name').snapshots()


    I have been playing with the document and tried different approaches for the current user to query for the document, and just to test it out I created an array with the userId and look for that.



    If I completely skip the rules and just put the "need to be logged in" as requirement then I get a document back but as soon as I use the role based one then it's back to the drawing board. The rules I've set up are:



    service cloud.firestore {
    match /databases/{database}/documents {
    match /students/{student} {
    function isSignedIn() {
    return request.auth != null;
    }
    function getRole(rsc) {
    return rsc.data.roles[request.auth.uid];
    }
    function isOneOfRoles(rsc, array) {
    return isSignedIn() && (getRole(rsc) in array);
    }
    allow read, write : if isOneOfRoles(resource,['teacher', 'student', 'parent']);
    }
    }
    }


    Any idea what's causing this?










    share|improve this question


























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I'm working on a Flutter app using Firebase as a backed. I've set up group based roles in Firebase and the rules simulator in Firebase tells me the user I'm testing has access to the document. When I do a query in my Flutter code, I can see it finds the document and I can see it for a split second before it changes it mind and I get a "Listen for query at students failed: Missing or insufficient permissions." and the document is removed from the snapshot.



      The query I use in the Flutter code is as follows:



      Firestore.instance.collection('students').where('test', arrayContains: userID).orderBy('name').snapshots()


      I have been playing with the document and tried different approaches for the current user to query for the document, and just to test it out I created an array with the userId and look for that.



      If I completely skip the rules and just put the "need to be logged in" as requirement then I get a document back but as soon as I use the role based one then it's back to the drawing board. The rules I've set up are:



      service cloud.firestore {
      match /databases/{database}/documents {
      match /students/{student} {
      function isSignedIn() {
      return request.auth != null;
      }
      function getRole(rsc) {
      return rsc.data.roles[request.auth.uid];
      }
      function isOneOfRoles(rsc, array) {
      return isSignedIn() && (getRole(rsc) in array);
      }
      allow read, write : if isOneOfRoles(resource,['teacher', 'student', 'parent']);
      }
      }
      }


      Any idea what's causing this?










      share|improve this question















      I'm working on a Flutter app using Firebase as a backed. I've set up group based roles in Firebase and the rules simulator in Firebase tells me the user I'm testing has access to the document. When I do a query in my Flutter code, I can see it finds the document and I can see it for a split second before it changes it mind and I get a "Listen for query at students failed: Missing or insufficient permissions." and the document is removed from the snapshot.



      The query I use in the Flutter code is as follows:



      Firestore.instance.collection('students').where('test', arrayContains: userID).orderBy('name').snapshots()


      I have been playing with the document and tried different approaches for the current user to query for the document, and just to test it out I created an array with the userId and look for that.



      If I completely skip the rules and just put the "need to be logged in" as requirement then I get a document back but as soon as I use the role based one then it's back to the drawing board. The rules I've set up are:



      service cloud.firestore {
      match /databases/{database}/documents {
      match /students/{student} {
      function isSignedIn() {
      return request.auth != null;
      }
      function getRole(rsc) {
      return rsc.data.roles[request.auth.uid];
      }
      function isOneOfRoles(rsc, array) {
      return isSignedIn() && (getRole(rsc) in array);
      }
      allow read, write : if isOneOfRoles(resource,['teacher', 'student', 'parent']);
      }
      }
      }


      Any idea what's causing this?







      firebase flutter google-cloud-firestore firebase-security-rules






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 21:39









      Doug Stevenson

      65.2k77897




      65.2k77897










      asked Nov 12 at 21:28









      Joe

      113




      113





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270365%2ffirebase-rules-simulator-says-yes-code-says-no%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270365%2ffirebase-rules-simulator-says-yes-code-says-no%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?