Class: Puppet::Util::Windows::ADSI::User
Constant Summary
collapse
- MAX_USERNAME_LENGTH =
256
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Enumerable
uniq
Methods included from Shared
get_sids, localized_domains, name_sid_hash, parse_name
attach_function_private
Constructor Details
#initialize(name, native_user = nil) ⇒ User
Returns a new instance of User.
176
177
178
179
|
# File 'lib/puppet/util/windows/adsi.rb', line 176
def initialize(name, native_user = nil)
@name = name
@native_user = native_user
end
|
Instance Attribute Details
Returns the value of attribute name.
175
176
177
|
# File 'lib/puppet/util/windows/adsi.rb', line 175
def name
@name
end
|
#native_user ⇒ Object
Returns the value of attribute native_user.
174
175
176
|
# File 'lib/puppet/util/windows/adsi.rb', line 174
def native_user
@native_user
end
|
Returns the value of attribute sid.
175
176
177
|
# File 'lib/puppet/util/windows/adsi.rb', line 175
def sid
@sid
end
|
Class Method Details
.current_user_name ⇒ Object
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
# File 'lib/puppet/util/windows/adsi.rb', line 320
def self.current_user_name
user_name = ''
max_length = MAX_USERNAME_LENGTH + 1 FFI::MemoryPointer.new(max_length * 2) do |buffer| FFI::MemoryPointer.new(:dword, 1) do |buffer_size|
buffer_size.write_dword(max_length)
if GetUserNameW(buffer, buffer_size) == FFI::WIN32_FALSE
raise Puppet::Util::Windows::Error.new(_("Failed to get user name"))
end
user_name = buffer.read_wide_string(buffer_size.read_dword - 1)
end
end
user_name
end
|
.each(&block) ⇒ Object
370
371
372
373
374
375
376
377
378
379
380
381
|
# File 'lib/puppet/util/windows/adsi.rb', line 370
def self.each(&block)
wql = Puppet::Util::Windows::ADSI.execquery('select name from win32_useraccount where localaccount = "TRUE"')
users = []
wql.each do |u|
users << new(u.name)
end
users.each(&block)
end
|
.exists?(name_or_sid) ⇒ Boolean
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
# File 'lib/puppet/util/windows/adsi.rb', line 342
def self.exists?(name_or_sid)
well_known = false
if (sid = Puppet::Util::Windows::SID.name_to_sid_object(name_or_sid))
return true if sid.account_type == :SidTypeUser
well_known = sid.account_type == :SidTypeWellKnownGroup
return false if sid.account_type != :SidTypeAlias && !well_known
name_or_sid = "#{sid.domain}\\#{sid.account}"
end
user = Puppet::Util::Windows::ADSI.connect(User.uri(*User.parse_name(name_or_sid)))
user.Class == 'User'
rescue
well_known
end
|
.logon(name, password) ⇒ Object
Instance Method Details
#[](attribute) ⇒ Object
197
198
199
200
201
|
# File 'lib/puppet/util/windows/adsi.rb', line 197
def [](attribute)
native_user.Get(attribute)
end
|
#[]=(attribute, value) ⇒ Object
203
204
205
|
# File 'lib/puppet/util/windows/adsi.rb', line 203
def []=(attribute, value)
native_user.Put(attribute, value)
end
|
#add_flag(flag_name, value) ⇒ Object
228
229
230
231
232
233
234
|
# File 'lib/puppet/util/windows/adsi.rb', line 228
def add_flag(flag_name, value)
flag = native_user.Get(flag_name) rescue 0
native_user.Put(flag_name, flag | value)
commit
end
|
#add_group_sids(*sids) ⇒ Object
271
272
273
274
|
# File 'lib/puppet/util/windows/adsi.rb', line 271
def add_group_sids(*sids)
group_names = sids.map { |s| s.domain_account }
add_to_groups(*group_names)
end
|
#add_to_groups(*group_names) ⇒ Object
Also known as:
add_to_group
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/puppet/util/windows/adsi.rb', line 207
def commit
begin
native_user.SetInfo unless native_user.nil?
rescue WIN32OLERuntimeError => e
if e.message =~ /8007089A/m
raise Puppet::Error.new(
_("Puppet is not able to create/delete domain users with the user resource."),
e
)
end
raise Puppet::Error.new( _("User update failed: %{e}") % { e: e }, e )
end
self
end
|
#group_sids ⇒ Object
281
282
283
|
# File 'lib/puppet/util/windows/adsi.rb', line 281
def group_sids
self.class.get_sids(native_user.Groups)
end
|
246
247
248
249
250
251
252
253
254
|
# File 'lib/puppet/util/windows/adsi.rb', line 246
def groups
groups = []
native_user.Groups.each {|g| groups << g.Name} rescue nil
groups
end
|
#password=(password) ⇒ Object
236
237
238
239
240
241
242
243
244
|
# File 'lib/puppet/util/windows/adsi.rb', line 236
def password=(password)
if !password.nil?
native_user.SetPassword(password)
commit
end
fADS_UF_DONT_EXPIRE_PASSWD = 0x10000
add_flag("UserFlags", fADS_UF_DONT_EXPIRE_PASSWD)
end
|
#password_is?(password) ⇒ Boolean
224
225
226
|
# File 'lib/puppet/util/windows/adsi.rb', line 224
def password_is?(password)
self.class.logon(name, password)
end
|
#remove_from_groups(*group_names) ⇒ Object
Also known as:
remove_from_group
#remove_group_sids(*sids) ⇒ Object
276
277
278
279
|
# File 'lib/puppet/util/windows/adsi.rb', line 276
def remove_group_sids(*sids)
group_names = sids.map { |s| s.domain_account }
remove_from_groups(*group_names)
end
|
#set_groups(desired_groups, minimum = true) ⇒ Object
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
# File 'lib/puppet/util/windows/adsi.rb', line 285
def set_groups(desired_groups, minimum = true)
return if desired_groups.nil?
desired_groups = desired_groups.split(',').map(&:strip)
current_hash = Hash[ self.group_sids.map { |sid| [sid.sid, sid] } ]
desired_hash = self.class.name_sid_hash(desired_groups)
if !desired_groups.empty?
groups_to_add = (desired_hash.keys - current_hash.keys).map { |sid| desired_hash[sid] }
add_group_sids(*groups_to_add)
end
if !minimum
if desired_hash.empty?
groups_to_remove = current_hash.values
else
groups_to_remove = (current_hash.keys - desired_hash.keys).map { |sid| current_hash[sid] }
end
remove_group_sids(*groups_to_remove)
end
end
|
189
190
191
|
# File 'lib/puppet/util/windows/adsi.rb', line 189
def uri
self.class.uri(sid.account, sid.domain)
end
|