       instance        :
         delete        :
           (signature Function)
-        shareWith     :
-          (signature Object, Function)
-        fetchUsers    :
-          (signature Function)
-        fetchData     :
-          (signature Function)
+        setAccess     :
+          (signature String, Function)
         update        :
           (signature Object, Function)
 

        type          : Object
        default       : -> { }

      originId        :
        type          : ObjectId
        required      : yes

      meta            : require 'bongo/bundles/meta'

  @getName = -> 'JProvisioner'

  checkContent = (type, content)->


          callback null, provisioner


  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: (client, options, callback)->

      {content, label} = options
