title: How can I use a gremlin query to filter based on a users permissions?
body: <p>I am fairly new to graph databases, however I have used SQL Server and document databases (Lucene, DocumentDb, etc.) extensively. It's completely possible that I am approaching this query the wrong way, since I am new to graph databases. I am trying to convert some logic to a graph database (CosmosDB Graph via Gremlins to be specific) that we currently are using SQL Server for. The reason for the change is that this problem set is not really what SQL Server is great at and so our SQL query (which we have optimized as good as we can) is really starting to be the hot spot of our application.</p>
  
  <p>To give a very brief overview of our logic, we run a web shop that allows admins to configure products and users with several levels of granular permissions (described below). Based on these permissions, we show the user only the products they are allowed to see.</p>
  
  <p>Entities:</p>
  
  <ul>
  <li>Region: A region consists of multiple countries</li>
  <li>Country: A country has many markets and many regions</li>
  <li>Market: A market is a group of stores in a single country</li>
  <li>Store: A store is belongs to a single market</li>
  </ul>
  
  <p>Users have the following set of permissions and each set can contain multiple values:</p>
  
  <ul>
  <li>can-view-region</li>
  <li>can-view-country</li>
  <li>can-view-market</li>
  <li>can-view-store</li>
  </ul>
  
  <p>Products have the following set of permissions and each set can contain multiple values:</p>
  
  <ul>
  <li>visible-to-region</li>
  <li>visible-to-country</li>
  <li>visible-to-market</li>
  <li>visible-to-store</li>
  </ul>
  
  <p>After trying for a few days, this is the query that I have come up with. This query does work and returns the correct products for the given user, however it takes about 25 seconds to execute.</p>
  
  <p><pre><code>g.V().has('user','username', 'john.doe').union(
      __.out('can-view-region').out('contains-country').in('in-market').hasLabel('store'),
      __.out('can-view-country').in('in-market').hasLabel('store'),
      __.out('can-view-market').in('in-market').hasLabel('store'),
      __.out('can-view-store')
  ).dedup().union(
      __.out('in-market').in('contains-country').in('visible-to-region').hasLabel('product'),
      __.out('in-market').in('visible-to-country').hasLabel('product'),
      __.out('in-market').in('visible-to-market').hasLabel('product'),
      __.in('visible-to-store').hasLabel('product')
  ).dedup()
  </pre></code></p>
  
  <p>Is there a better way to do this? Is this problem maybe not best suited with a graph database?</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>Thanks,
  Chris</p>