title: How to write this in SQL?
body: <p>I've been trying to find this answer by myself, but i'm just too bad for that, so i come here hoping you can help me.</p>
  
  <p>I have the following database tables :</p>
  
  <pre><code>user(
      int unsigned id,
      varchar      login
  )
  group(
      int unsigned id,
      varchar      label
  )
  right(
      int unsigned id,
      varchar      label
  )
  user_group(
      int unsigned userId,
      int unsigned groupId
  )
  user_right(
      int unsigned userId,
      int unsigned rightId,
      boolean      granted
  )
  group_right(
      int unsigned groupId,
      int unsigned rightId,
      boolean      granted
  )
  </code></pre>
  
  <p>And 3 configuration parameters (PHP defines) :</p>
  
  <ul>
  <li><p><strong><code>GRANTED_BY_DEFAULT</code></strong> that can be <code>TRUE</code> or <code>FALSE</code>.
  It indicates, when a right is not specified for a group or a user (i.e not in the association table), if the right is granted or not.</p></li>
  <li><p><strong><code>RESULTING_RIGHTS</code></strong> that can be <strong><code>MIN_RIGHTS</code></strong> or <strong><code>MAX_RIGHTS</code></strong>.
  It indicates whether to use "AND" or "OR" operator when determining resulting rights. For exemple, if for right "r1", the group "g1" has TRUE and the groups "g2" and "g3" have FALSE, with "MIN_RIGHTS" you get FALSE, and with "MAX_RIGHTS" you get TRUE.</p></li>
  <li><p><strong><code>RIGHTS_PRIORITY</code></strong> that can be <strong><code>USER_OVER_GROUP</code></strong> or <strong><code>GROUP_OVER_USER</code></strong>.
  It indicates if the user's own rights override his groups's rights or if the groups's rights override the user's own rights.</p></li>
  </ul>
  
  <p>I would like to come out with a single query that would find the rights a user effectivly has, considering these 3 parameters and his groups, but i really have no idea about how to do it. I could also make multiple queries and do a part of the work on PHP side, but i would like to avoid having to many queries as i want my app to be fast enough for things like AJAX auto completions and other stuff like that.</p>
  
  <p>I'd understand if no one wants or has the time to help me :p Thanks for reading.</p>
  
  <p>EDIT :
  Some sample datas :</p>
  
  <pre><code>user (id, login) :
  1 "Admin"
  
  group (id, label) :
  1 "g1"
  2 "g2"
  3 "g3"
  
  right (id, label) :
  1 "r1"
  2 "r2"
  3 "r3"
  
  user_group (userId, groupId) :
  1 1
  1 2
  
  user_right (userId, rightId) :
  1 1 true
  1 2 false
  
  group_rights (groupId, rightId) :
  1 1 true
  1 2 false
  2 1 false
  2 3 true
  </code></pre>
  
  <p>And the output would be a list of (right_id, right_label) rows depending on the parameters and the rows of other tables.</p>
  
  <p>The user ID and / or login is considered as known when retrieving the rights.</p>