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 Shared
get_sids, name_sid_hash, parse_name
attach_function_private
Constructor Details
#initialize(name, native_user = nil) ⇒ User
Returns a new instance of User.
165
166
167
168
|
# File 'lib/puppet/util/windows/adsi.rb', line 165
def initialize(name, native_user = nil)
@name = name
@native_user = native_user
end
|
Instance Attribute Details
164
165
166
|
# File 'lib/puppet/util/windows/adsi.rb', line 164
def name
@name
end
|
#native_user ⇒ Object
163
164
165
|
# File 'lib/puppet/util/windows/adsi.rb', line 163
def native_user
@native_user
end
|
164
165
166
|
# File 'lib/puppet/util/windows/adsi.rb', line 164
def sid
@sid
end
|
Class Method Details
.current_user_name ⇒ Object
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
# File 'lib/puppet/util/windows/adsi.rb', line 305
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
351
352
353
354
355
356
357
358
359
360
|
# File 'lib/puppet/util/windows/adsi.rb', line 351
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/puppet/util/windows/adsi.rb', line 323
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
186
187
188
|
# File 'lib/puppet/util/windows/adsi.rb', line 186
def [](attribute)
native_user.Get(attribute)
end
|
#[]=(attribute, value) ⇒ Object
190
191
192
|
# File 'lib/puppet/util/windows/adsi.rb', line 190
def []=(attribute, value)
native_user.Put(attribute, value)
end
|
#add_flag(flag_name, value) ⇒ Object
215
216
217
218
219
220
221
|
# File 'lib/puppet/util/windows/adsi.rb', line 215
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
256
257
258
259
|
# File 'lib/puppet/util/windows/adsi.rb', line 256
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/puppet/util/windows/adsi.rb', line 194
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 )
end
self
end
|
#group_sids ⇒ Object
266
267
268
|
# File 'lib/puppet/util/windows/adsi.rb', line 266
def group_sids
self.class.get_sids(native_user.Groups)
end
|
233
234
235
236
237
238
239
|
# File 'lib/puppet/util/windows/adsi.rb', line 233
def groups
groups = []
native_user.Groups.each {|g| groups << g.Name} rescue nil
groups
end
|
#password=(password) ⇒ Object
223
224
225
226
227
228
229
230
231
|
# File 'lib/puppet/util/windows/adsi.rb', line 223
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
211
212
213
|
# File 'lib/puppet/util/windows/adsi.rb', line 211
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
261
262
263
264
|
# File 'lib/puppet/util/windows/adsi.rb', line 261
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/puppet/util/windows/adsi.rb', line 270
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
|
178
179
180
|
# File 'lib/puppet/util/windows/adsi.rb', line 178
def uri
self.class.uri(sid.account, sid.domain)
end
|