title: Firebase rules/secure in collectionGroup use if state
body: <p>I have a problem with firebase rules.</p>
  <p>At this moment i try to set new rules for more security in my db.</p>
  <p>My problem is that in my flutter code I'm using a collectionGroup
  and I don't know how I can specify the rules.</p>
  <p>In firebase rules I have created this code:</p>
  <ul>
  <li>doc -&gt; product_id (contains random number and user_id)</li>
  <li>path -&gt; path to product_details</li>
  </ul>
  <pre><code>//check product details
  match /{path=**}/product_details/{doc}{
        
  //check the user own the product -&gt; is it -&gt; he has all rules
  allow read : if(doc.split('_')[2] == request.auth.uid);
        
  //check the user own the product -&gt; is not -&gt; only can only read
  allow write : if(doc.split('_')[2] != request.auth.uid);
        
  //coming soon -&gt; admin rules -&gt; write/delete/read/update
  }
  </code></pre>
  <p>To check the user is in his own product i split the product_id.
  The product_id contains also the user_id.</p>
  <p>My plan is the user can only read, write, update and delete his own product.</p>
  <p>Products from other user can only read.</p>
  <p>Its just a example.</p>
  <p>If I set the in the code above the state 'allow read' my app works fine.</p>
  <p>But I'm changing the state (for example with a if state) i got an error in my app.</p>
  <p>In firebase debug my code works fine.</p>
  <p>In flutter the collectionGroup looks like this code:</p>
  <pre><code>//collection group all collection with 'product_images'
      Query&lt;Map&lt;String, dynamic&gt;&gt;  collectionGroupImage = FirebaseFirestore.instance.collectionGroup('product_images');
  
  //get data from collection group
  var query_product_images = await collectionGroupImage.get();
  </code></pre>
  <p>The error i got in my app looks like this code:
  <a href="https://i.stack.imgur.com/cYCZu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cYCZu.png" alt="enter image description here" /></a></p>
  <p>My Database:
  Path: /user/user_id/data/product_data/product_details/product_id/paramter(price,discription...)</p>
  <p><a href="https://i.stack.imgur.com/6jiR1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6jiR1.png" alt="enter image description here" /></a></p>
  <p>I add all product from one user into the user (see path above)</p>
  <p>My problem is, if i want show all products in my app
  i need all products id's.
  To solve this problem i make a collectionGroup on my 'product_details'.
  Now i can get all values in my products.
  Until now its work.</p>
  <p>If any questions feel free to ask me.</p>
  <p>Anyone have a idea how i can set a if state in code.
  Many thx</p>