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

Inherits:
Object
  • Object
show all
Extended by:
Enumerable, Shared
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

Methods included from Shared

get_sids, name_sid_hash, parse_name

Constructor Details

#initialize(name, native_group = nil) ⇒ Group

Returns a new instance of Group.



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

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

Instance Attribute Details

#nameObject (readonly)



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

def name
  @name
end

#native_groupObject



378
379
380
# File 'lib/puppet/util/windows/adsi.rb', line 378

def native_group
  @native_group
end

#sidObject (readonly)



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

def sid
  @sid
end

Class Method Details

.create(name) ⇒ Object

Raises:



461
462
463
464
465
# File 'lib/puppet/util/windows/adsi.rb', line 461

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



471
472
473
# File 'lib/puppet/util/windows/adsi.rb', line 471

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

.each(&block) ⇒ Object



475
476
477
478
479
480
481
482
483
484
# File 'lib/puppet/util/windows/adsi.rb', line 475

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



467
468
469
# File 'lib/puppet/util/windows/adsi.rb', line 467

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

Instance Method Details

#add_member_sids(*sids) ⇒ Object



414
415
416
417
418
# File 'lib/puppet/util/windows/adsi.rb', line 414

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

#commitObject



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/puppet/util/windows/adsi.rb', line 397

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



433
434
435
# File 'lib/puppet/util/windows/adsi.rb', line 433

def member_sids
  self.class.get_sids(native_group.Members)
end

#membersObject



426
427
428
429
430
431
# File 'lib/puppet/util/windows/adsi.rb', line 426

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



420
421
422
423
424
# File 'lib/puppet/util/windows/adsi.rb', line 420

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

#set_members(desired_members, inclusive = true) ⇒ Object



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/puppet/util/windows/adsi.rb', line 437

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



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

def uri
  self.class.uri(sid., sid.domain)
end