           callback null, provisioner
 
 
-  @fetchBySlug = (client, slug, callback)->
-
-    options =
-      limit         : 1
-      targetOptions : selector : { slug }
-
-    {delegate} = client.connection
-    delegate.fetchProvisioner { }, options, (err, res)->
-
-      return callback err        if err?
-      return callback null, res  if res?
-
-      { group } = client.context
-      JGroup.one slug: group, (err, group)->
-        return callback err  if err?
-
-        group.fetchProvisioner { }, options, (err, res)->
-          callback err, res
-
-
-  @one$ = permit 'list provisioners',
-
-    success: (client, slug, callback)->
-
-      @fetchBySlug client, slug, callback
-
-
-  @some$ = permit 'list provisioners', success: JCredential.someHelper
-
-
-  fetchUsers: JCredential::fetchUsers
-
-
-  setPermissionFor: (target, {user, owner}, callback)->
-
-    Relationship.remove
-      targetId : @getId()
-      sourceId : target.getId()
-    , (err)=>
-
-      if user
-        as = if owner then 'owner' else 'user'
-        target.addProvisioner this, { as }, (err)-> callback err
-      else
-        callback err
-
-  # .share can be used like this:
-  #
-  # JProvisionerInstance.share { user: yes, owner: no, target: "gokmen"}, cb
-  #                                       group or user slug -> ^^^^^^
-
-  shareWith$: permit
+  someHelper = (client, selector, options, callback)->
+
+    [options, callback] = [callback, options]  unless callback
+    options ?= {}
+
+    { delegate } = client.connection
+
+    unless typeof selector is 'object'
+      return callback new KodingError "Invalid query"
+
+    selector.$and ?= []
+    selector.$and.push
+      $or : [
+        { originId      : delegate.getId() }
+        { accessLevel   : 'public' }
+        {
+          $and          : [
+            accessLevel : 'group'
+            group       : client.context.group
+          ]
+        }
+      ]
+
+    JProvisioner.some selector, options, (err, templates)->
+      callback err, templates
+
+
+  @some$ = permit 'list provisioners', success: someHelper
+
+  @one$  = permit 'list provisioners',
+
+    success: (client, selector, callback)->
+
+      someHelper client, selector, limit: 1, (err, provisioners)->
+        callback err, (provisioners[0]  if provisioners?)
+
+
+  delete: permit
+
+    advanced: [
+      { permission: 'delete own provisioner', validateWith: Validators.own }
+      # { permission: 'delete provisioner' }
+    ]
+
+    success: (client, callback)-> @remove callback
+
+
+  setAccess: permit
+
+    advanced: [
+      { permission: 'update own provisioner', validateWith: Validators.own }
+      # { permission: 'update provisioner' }
+    ]
+
+    success: (client, accessLevel, callback)->
+
+      @update $set: { accessLevel }, callback
+
+
+  update$: permit
 
     advanced: [
       { permission: 'update provisioner', validateWith: Validators.own }
     ]
 
-    success: JCredential::shareWith
-
-
-  delete: permit 'delete provisioner',
-
-    success: (client, callback)->
-
-      { delegate } = client.connection
-
-      Relationship.one
-        targetId : @getId()
-        sourceId : delegate.getId()
-      , (err, rel)=>
-
-        return callback err   if err?
-        return callback null  unless rel?
-
-        if rel.data.as is 'owner'
-          @remove callback
-        else
-          rel.remove callback
-
-
-  update$: permit
-
-    advanced: [
-      { permission: 'update provisioner', validateWith: Validators.own }
-    ]
-
     success: (client, options, callback)->
 
       {content, label} = options

        type          : Object
        default       : -> { }

      originId        :
        type          : ObjectId
        required      : yes

      meta            : require 'bongo/bundles/meta'

  @getName = -> 'JProvisioner'

  checkContent = (type, content)->



jraphical   = require 'jraphical'
crypto      = require 'crypto'

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

module.exports = class JProvisioner extends jraphical.Module

