module Chef::Mixin::UserContext

Protected Instance Methods

with_user_context(user, password, domain = nil, authentication = :remote) { || ... } click to toggle source

valid values for authentication => :remote, :local When authentication = :local, we use the credentials to create a logon session against the local system, and then try to access the files. When authentication = :remote, we continue with the current user but pass the provided credentials to the remote system.

# File lib/chef/mixin/user_context.rb, line 28
def with_user_context(user, password, domain = nil, authentication = :remote, &block)
  unless Chef::Platform.windows?
    raise Exceptions::UnsupportedPlatform, "User context impersonation is supported only on the Windows platform"
  end

  if ! block_given?
    raise ArgumentError, "You must supply a block to `with_user_context`"
  end

  logon_session = nil

  begin
    if user
      logon_session = Chef::Util::Windows::LogonSession.new(user, password, domain, authentication)
      logon_session.open
      logon_session.set_user_context
    end
    yield
  ensure
    logon_session.close if logon_session
  end
end