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.



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

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

Instance Attribute Details

#nameObject (readonly)



376
377
378
# File 'lib/puppet/util/windows/adsi.rb', line 376

def name
  @name
end

#native_groupObject



375
376
377
# File 'lib/puppet/util/windows/adsi.rb', line 375

def native_group
  @native_group
end

#sidObject (readonly)



376
377
378
# File 'lib/puppet/util/windows/adsi.rb', line 376

def sid
  @sid
end

Class Method Details

.create(name) ⇒ Object

Raises:



458
459
460
461
462
# File 'lib/puppet/util/windows/adsi.rb', line 458

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



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

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

.each(&block) ⇒ Object



472
473
474
475
476
477
478
479
480
481
# File 'lib/puppet/util/windows/adsi.rb', line 472

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)


464
465
466
# File 'lib/puppet/util/windows/adsi.rb', line 464

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

Instance Method Details

#add_member_sids(*sids) ⇒ Object



411
412
413
414
415
# File 'lib/puppet/util/windows/adsi.rb', line 411

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

#commitObject



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

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



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

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

#membersObject



423
424
425
426
427
428
# File 'lib/puppet/util/windows/adsi.rb', line 423

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



417
418
419
420
421
# File 'lib/puppet/util/windows/adsi.rb', line 417

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



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

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



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

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