       unless typeof selector is 'object'
         return callback new KodingError "Invalid query"
 
-      selector.$or = [
-        { accessLevel   : 'public' }
-        { originId      : delegate.getId() }
-        {
-          $and          : [
-            accessLevel : 'group'
-            group       : client.context.group
-          ]
-        }
-      ]
+      selector.$and ?= []
+      selector.$and.push
+        $or : [
+          { originId      : delegate.getId() }
+          { accessLevel   : 'public' }
+          {
+            $and          : [
+              accessLevel : 'group'
+              group       : client.context.group
+            ]
+          }
+        ]
 
       @some selector, options, (err, templates)->
         callback err, templates

        required      : yes

      description     : String
      config          : Object  # A JSON String?

      accessLevel     :
        type          : String

JName           = require '../name'
JUser           = require '../user'
JGroup          = require '../group'

# TODO Credential relations ~g

module.exports = class JStackTemplate extends jraphical.Module

