Class: Puppet::Util::Windows::ADSI::Group

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/puppet/util/windows.rb,
lib/puppet/util/windows/adsi.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, native_group = nil) ⇒ Group

Returns a new instance of Group.



313
314
315
316
# File 'lib/puppet/util/windows/adsi.rb', line 313

def initialize(name, native_group = nil)
  @name = name
  @native_group = native_group
end

Instance Attribute Details

#nameObject (readonly)



312
313
314
# File 'lib/puppet/util/windows/adsi.rb', line 312

def name
  @name
end

#native_groupObject



311
312
313
# File 'lib/puppet/util/windows/adsi.rb', line 311

def native_group
  @native_group
end

#sidObject (readonly)



312
313
314
# File 'lib/puppet/util/windows/adsi.rb', line 312

def sid
  @sid
end

Class Method Details

.create(name) ⇒ Object

Raises:



430
431
432
433
434
# File 'lib/puppet/util/windows/adsi.rb', line 430

def self.create(name)
  # Windows error 2224: The account already exists.
  raise Puppet::Error.new( "Cannot create group if user '#{name}' exists." ) if Puppet::Util::Windows::ADSI::User.exists? name
  new(name, Puppet::Util::Windows::ADSI.create(name, 'group'))
end

.delete(name) ⇒ Object



440
441
442
# File 'lib/puppet/util/windows/adsi.rb', line 440

def self.delete(name)
  Puppet::Util::Windows::ADSI.delete(name, 'group')
end

.each(&block) ⇒ Object



444
445
446
447
448
449
450
451
452
453
# File 'lib/puppet/util/windows/adsi.rb', line 444

def self.each(&block)
  wql = Puppet::Util::Windows::ADSI.execquery( 'select name from win32_group where localaccount = "TRUE"' )

  groups = []
  wql.each do |g|
    groups << new(g.name)
  end

  groups.each(&block)
end

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


436
437
438
# File 'lib/puppet/util/windows/adsi.rb', line 436

def self.exists?(name)
  Puppet::Util::Windows::ADSI.connectable?(Group.uri(name))
end

.name_sid_hash(names) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
# File 'lib/puppet/util/windows/adsi.rb', line 353

def self.name_sid_hash(names)
  return [] if names.nil? or names.empty?

  sids = names.map do |name|
    sid = Puppet::Util::Windows::SID.name_to_sid_object(name)
    raise Puppet::Error.new( "Could not resolve username: #{name}" ) if !sid
    [sid.to_s, sid]
  end

  Hash[ sids ]
end

.uri(name, host = '.') ⇒ Object



322
323
324
325
326
# File 'lib/puppet/util/windows/adsi.rb', line 322

def self.uri(name, host = '.')
  if sid_uri = Puppet::Util::Windows::ADSI.sid_uri_safe(name) then return sid_uri end

  Puppet::Util::Windows::ADSI.uri(name, 'group', host)
end

Instance Method Details

#add_member_sids(*sids) ⇒ Object



379
380
381
382
383
# File 'lib/puppet/util/windows/adsi.rb', line 379

def add_member_sids(*sids)
  sids.each do |sid|
    native_group.Add(Puppet::Util::Windows::ADSI.sid_uri(sid))
  end
end

#add_members(*names) ⇒ Object Also known as: add_member



365
366
367
368
369
# File 'lib/puppet/util/windows/adsi.rb', line 365

def add_members(*names)
  Puppet.deprecation_warning('Puppet::Util::Windows::ADSI::Group#add_members is deprecated; please use Puppet::Util::Windows::ADSI::Group#add_member_sids')
  sids = self.class.name_sid_hash(names)
  add_member_sids(*sids.values)
end

#commitObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/puppet/util/windows/adsi.rb', line 336

def commit
  begin
    native_group.SetInfo unless native_group.nil?
  rescue WIN32OLERuntimeError => e
    # ERROR_BAD_USERNAME 2202L from winerror.h
    if e.message =~ /8007089A/m
      raise Puppet::Error.new(
        "Puppet is not able to create/delete domain groups with the group resource.",
        e
      )
    end

    raise Puppet::Error.new( "Group update failed: #{e}", e )
  end
  self
end

#member_sidsObject



398
399
400
401
402
403
404
# File 'lib/puppet/util/windows/adsi.rb', line 398

def member_sids
  sids = []
  native_group.Members.each do |m|
    sids << Puppet::Util::Windows::SID.octet_string_to_sid_object(m.objectSID)
  end
  sids
end

#membersObject



391
392
393
394
395
396
# File 'lib/puppet/util/windows/adsi.rb', line 391

def members
  # WIN32OLE objects aren't enumerable, so no map
  members = []
  native_group.Members.each {|m| members << m.Name}
  members
end

#remove_member_sids(*sids) ⇒ Object



385
386
387
388
389
# File 'lib/puppet/util/windows/adsi.rb', line 385

def remove_member_sids(*sids)
  sids.each do |sid|
    native_group.Remove(Puppet::Util::Windows::ADSI.sid_uri(sid))
  end
end

#remove_members(*names) ⇒ Object Also known as: remove_member



372
373
374
375
376
# File 'lib/puppet/util/windows/adsi.rb', line 372

def remove_members(*names)
  Puppet.deprecation_warning('Puppet::Util::Windows::ADSI::Group#remove_members is deprecated; please use Puppet::Util::Windows::ADSI::Group#remove_member_sids')
  sids = self.class.name_sid_hash(names)
  remove_member_sids(*sids.values)
end

#set_members(desired_members, inclusive = true) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/puppet/util/windows/adsi.rb', line 406

def set_members(desired_members, inclusive = true)
  return if desired_members.nil?

  current_hash = Hash[ self.member_sids.map { |sid| [sid.to_s, sid] } ]
  desired_hash = self.class.name_sid_hash(desired_members)

  # First we add all missing members
  if !desired_hash.empty?
    members_to_add = (desired_hash.keys - current_hash.keys).map { |sid| desired_hash[sid] }
    add_member_sids(*members_to_add)
  end

  # Then we remove all extra members if inclusive
  if inclusive
    if desired_hash.empty?
      members_to_remove = current_hash.values
    else
      members_to_remove = (current_hash.keys - desired_hash.keys).map { |sid| current_hash[sid] }
    end

    remove_member_sids(*members_to_remove)
  end
end

#uriObject



318
319
320
# File 'lib/puppet/util/windows/adsi.rb', line 318

def uri
  self.class.uri(name)
end