Patches: Browse | Submit New | Admin

[#29483] Extend Sys::Admin class to work with group membership

Date:
2011-12-29 06:44
Priority:
3
Submitted By:
Alexey Kolyanov (baldr)
Assigned To:
Daniel Berger (djberg96)
Category:
sys-admin
State:
Closed
Summary:
Extend Sys::Admin class to work with group membership

Detailed description
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}

Add A Comment: Notepad

Please login


Followup

Message
Date: 2014-01-24 02:49
Sender: Daniel Berger

This looks good. I'll include it, perhaps with modified method
names, in the next release.

Attached Files:

Name Description Download
admin_mix.rb Code Download

Changes:

Field Old Value Date By
close_date2014-01-24 04:142014-01-24 04:14djberg96
status_idOpen2014-01-24 04:14djberg96
resolution_idNone2014-01-24 02:49djberg96
File Added5446: admin_mix.rb2011-12-29 06:44baldr