 #---------------------------------------------------------------------
 
 rollingSnapshot = $coroutine ({vm, tag, depth}) ->
+  yield checkPermissionsForSnapshot.call(this, vm)
+
   snapshot = yield @getXAPI(vm).rollingSnapshotVm(vm.ref, tag, depth)
   return snapshot.$id
 

#---------------------------------------------------------------------

snapshot = $coroutine ({vm, name}) ->
  snapshot = yield @getXAPI(vm).snapshotVm(vm.ref, name)
  return snapshot.$id


{coroutine: $coroutine} = require 'bluebird'
{format} = require 'json-rpc-peer'

{
  JsonRpcError,
  Unauthorized
} = require('../api-errors')
{
  formatXml: $js2xml,
  parseXml,
  pFinally
} = require '../utils'
{isVmRunning: $isVMRunning} = require('../xapi')

#=====================================================================

checkPermissionsForSnapshot = (vm) -> (
  permissions = []
  $forEach(vm.$VBDs, (vbdId) =>
    vbd = @getObject(vbdId, 'VBD')
    vdiId = vbd.VDI

    if vbd.is_cd_drive or not vdiId
      return

    permissions.push([
      @getObject(vdiId, 'VDI').$SR,
      'operate'
    ])
  )

  return @hasPermissions(@session.get('user_id'), permissions).then((success) => (
    throw new Unauthorized() unless success
  ))
)

#=====================================================================

