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

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/vendor/puppet/util/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.



210
211
212
213
# File 'lib/vendor/puppet/util/adsi.rb', line 210

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



209
210
211
# File 'lib/vendor/puppet/util/adsi.rb', line 209

def name
  @name
end

#native_groupObject

Returns the value of attribute native_group.



208
209
210
# File 'lib/vendor/puppet/util/adsi.rb', line 208

def native_group
  @native_group
end

Class Method Details

.create(name) ⇒ Object

Raises:



271
272
273
274
275
# File 'lib/vendor/puppet/util/adsi.rb', line 271

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::ADSI::User.exists? name
  new(name, Puppet::Util::ADSI.create(name, 'group'))
end

.delete(name) ⇒ Object



281
282
283
# File 'lib/vendor/puppet/util/adsi.rb', line 281

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

.each(&block) ⇒ Object



285
286
287
288
289
290
291
292
293
294
# File 'lib/vendor/puppet/util/adsi.rb', line 285

def self.each(&block)
  wql = Puppet::Util::ADSI.execquery( "select * from win32_group" )

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

  groups.each(&block)
end

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/vendor/puppet/util/adsi.rb', line 277

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

.uri(name) ⇒ Object



219
220
221
# File 'lib/vendor/puppet/util/adsi.rb', line 219

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

Instance Method Details

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



236
237
238
239
240
# File 'lib/vendor/puppet/util/adsi.rb', line 236

def add_members(*names)
  names.each do |name|
    native_group.Add(Puppet::Util::ADSI::User.uri(name))
  end
end

#commitObject



227
228
229
230
231
232
233
234
# File 'lib/vendor/puppet/util/adsi.rb', line 227

def commit
  begin
    native_group.SetInfo unless native_group.nil?
  rescue Exception => e
    raise Puppet::Error.new( "Group update failed: #{e}" )
  end
  self
end

#membersObject



250
251
252
253
254
255
# File 'lib/vendor/puppet/util/adsi.rb', line 250

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

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



243
244
245
246
247
# File 'lib/vendor/puppet/util/adsi.rb', line 243

def remove_members(*names)
  names.each do |name|
    native_group.Remove(Puppet::Util::ADSI::User.uri(name))
  end
end

#set_members(desired_members) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/vendor/puppet/util/adsi.rb', line 257

def set_members(desired_members)
  return if desired_members.nil? or desired_members.empty?

  current_members = self.members

  # First we add all missing members
  members_to_add = desired_members - current_members
  add_members(*members_to_add)

  # Then we remove all extra members
  members_to_remove = current_members - desired_members
  remove_members(*members_to_remove)
end

#uriObject



215
216
217
# File 'lib/vendor/puppet/util/adsi.rb', line 215

def uri
  self.class.uri(name)
end