Hi, I've extended Sys:Admin class for my needs and I think it would be useful for all:
* Add/Remove user in group
* Overload self.configure_user() method to fix password set: with using adsi.SetPassword() function you do not need
to know old password.
Works for me with Ruby 1.8.6 and 1.9.2 ..
Code below:
{code}
# Extend Sys::Admin class to work with group membership
class Sys::Admin
def self.add_groupmember(user, group, domain=nil)
domain = domain || Socket.gethostname
adsi = WIN32OLE.connect("WinNT://#{domain}/#{group},group")
adsi.Add("WinNT://#{domain}/#{user}")
rescue WIN32OLERuntimeError => err
raise Error, err
end
def self.remove_groupmember(user, group, domain=nil)
domain = domain || Socket.gethostname
adsi = WIN32OLE.connect("WinNT://#{domain}/#{group},group")
adsi.Remove("WinNT://#{domain}/#{user}")
rescue WIN32OLERuntimeError => err
raise Error, err
end
class << self
# WARNING! Here we re-define function from original class
alias old_configure_user configure_user
def configure_user(options = {})
options=munge_options(options)
pass=options.delete(:password)
v=old_configure_user(options)
if pass
domain = options[:domain] || Socket.gethostname
adsi = WIN32OLE.connect("WinNT://#{domain}/#{options[:name]},user")
adsi.SetPassword(pass)
end
return v
end
end
end
{code} |