Class: Chef::Util::Windows::NetUser

Inherits:
Chef::Util::Windows show all
Defined in:
lib/chef/util/windows/net_user.rb

Overview

wrapper around a subset of the NetUser* APIs. nothing Chef specific, but not complete enough to be its own gem, so util for now.

Constant Summary collapse

LOGON32_PROVIDER_DEFAULT =
Security::LOGON32_PROVIDER_DEFAULT
LOGON32_LOGON_NETWORK =
Security::LOGON32_LOGON_NETWORK

Instance Method Summary collapse

Constructor Details

#initialize(username) ⇒ NetUser

Returns a new instance of NetUser.



90
91
92
# File 'lib/chef/util/windows/net_user.rb', line 90

def initialize(username)
  @username = username
end

Instance Method Details

#add(args) ⇒ Object



116
117
118
119
120
# File 'lib/chef/util/windows/net_user.rb', line 116

def add(args)
  transformed_args = transform_usri3(args)
  NetUser.net_user_add_l3(nil, transformed_args)
  NetUser.net_local_group_add_member(nil, "Users", args[:name])
end

#check_enabledObject



167
168
169
# File 'lib/chef/util/windows/net_user.rb', line 167

def check_enabled
  (get_info()[:flags] & NetUser::UF_ACCOUNTDISABLE) != 0
end

#deleteObject



139
140
141
142
143
144
145
# File 'lib/chef/util/windows/net_user.rb', line 139

def delete
  begin
    NetUser.net_user_del(nil, @username)
  rescue Chef::Exceptions::Win32APIError => e
    raise ArgumentError, e
  end
end

#disable_accountObject



147
148
149
150
151
152
153
154
155
# File 'lib/chef/util/windows/net_user.rb', line 147

def 
  user_modify do |user|
    user[:flags] |= NetUser::UF_ACCOUNTDISABLE
    #This does not set the password to nil. It (for some reason) means to ignore updating the field.
    #See similar behavior for the logon_hours field documented at
    #http://msdn.microsoft.com/en-us/library/windows/desktop/aa371338%28v=vs.85%29.aspx
    user[:password] = nil
  end
end

#enable_accountObject



157
158
159
160
161
162
163
164
165
# File 'lib/chef/util/windows/net_user.rb', line 157

def 
  user_modify do |user|
    user[:flags] &= ~NetUser::UF_ACCOUNTDISABLE
    #This does not set the password to nil. It (for some reason) means to ignore updating the field.
    #See similar behavior for the logon_hours field documented at
    #http://msdn.microsoft.com/en-us/library/windows/desktop/aa371338%28v=vs.85%29.aspx
    user[:password] = nil
  end
end

#get_infoObject



107
108
109
110
111
112
113
114
# File 'lib/chef/util/windows/net_user.rb', line 107

def get_info
  begin
    ui3 = NetUser.net_user_get_info_l3(nil, @username)
  rescue Chef::Exceptions::Win32APIError => e
    raise ArgumentError, e
  end
  usri3_to_hash(ui3)
end

#update(args) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/chef/util/windows/net_user.rb', line 131

def update(args)
  user_modify do |user|
    args.each do |key, val|
      user[key] = val
    end
  end
end

#user_modify {|user| ... } ⇒ Object

FIXME: yard with @yield

Yields:

  • (user)


123
124
125
126
127
128
129
# File 'lib/chef/util/windows/net_user.rb', line 123

def user_modify
  user = get_info
  user[:last_logon] = user[:units_per_week] = 0 #ignored as per USER_INFO_3 doc
  user[:logon_hours] = nil #PBYTE field; \0 == no changes
  yield(user)
  set_info(user)
end

#validate_credentials(passwd) ⇒ Object

XXX for an extra painful alternative, see: support.microsoft.com/kb/180548



97
98
99
100
101
102
103
104
105
# File 'lib/chef/util/windows/net_user.rb', line 97

def validate_credentials(passwd)
  begin
    token = Security.logon_user(@username, nil, passwd,
               LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT)
    return true
  rescue Chef::Exceptions::Win32APIError
    return false
  end
end