Parent

Sys::Admin

The Admin class provides a unified, cross platform replacement for the Etc module.

Public Class Methods

add_group(options = {}) click to toggle source

Create a new group using options. If no domain option is specified then a local group is created instead.

Examples:

# Create a local group with no options
Sys::Admin.add_group(:name => 'Dudes')

# Create a local group with options
Sys::Admin.add_group(:name => 'Dudes', :description => 'Boys')

# Create a group on a specific domain
Sys::Admin.add_group(
   :name        => 'Ladies',
   :domain      => 'XYZ',
   :description => 'Girls'
)
# File lib/windows/sys/admin.rb, line 539
def self.add_group(options = {})
  options = munge_options(options)

  group  = options.delete(:name) or raise ArgumentError, 'No name given'
  domain = options[:domain]

  if domain.nil?
    domain  = Socket.gethostname
    moniker = "WinNT://#{domain},Computer"
  else
    moniker = "WinNT://#{domain}"
  end

  begin
    adsi = WIN32OLE.connect(moniker)
    group = adsi.create('group', group)
    group.setinfo
    configure_group(options) unless options.empty?
  rescue WIN32OLERuntimeError => err
    raise Error, err
  end
end
add_user(options = {}) click to toggle source

Creates the given user. If no domain option is specified, then it defaults to your local host, i.e. a local account is created.

Any options provided are treated as IADsUser interface methods and are called before SetInfo is finally called.

Examples:

# Create a local user with no options
Sys::Admin.add_user(:name => 'asmith')

# Create a local user with options
Sys::Admin.add_user(
   :name        => 'asmith',
   :description => 'Really cool guy',
   :password    => 'abc123'
)

# Create a user on a specific domain
Sys::Admin.add_user(
   :name     => 'asmith',
   :domain   => 'XX',
   :fullname => 'Al Smith'
)
# File lib/windows/sys/admin.rb, line 417
def self.add_user(options = {})
  options = munge_options(options)

  name   = options.delete(:name) or raise ArgumentError, 'No user given'
  domain = options[:domain]

  if domain.nil?
    domain  = Socket.gethostname
    moniker = "WinNT://#{domain},Computer"
  else
    moniker = "WinNT://#{domain}"
  end

  begin
    adsi = WIN32OLE.connect(moniker)
    user = adsi.create('user', name)

    options.each{ |option, value|
      if option.to_s == 'password'
        user.setpassword(value)
      else
        user.put(option.to_s, value)
      end
    }

    user.setinfo
  rescue WIN32OLERuntimeError => err
    raise Error, err
  end
end
configure_group(options = {}) click to toggle source

Configures the group using options. If no domain option is specified then your local host is used, i.e. you are configuring a local group.

See tinyurl.com/cjkzl for a list of valid options.

Examples:

# Configure a local group.
Sys::Admin.configure_group(:name => 'Abba', :description => 'Swedish')

# Configure a group on a specific domain.
Sys::Admin.configure_group(
   :name        => 'Web Team',
   :domain      => 'Foo',
   :description => 'Web programming cowboys'
)
# File lib/windows/sys/admin.rb, line 580
def self.configure_group(options = {})
  options = munge_options(options)

  group  = options.delete(:name) or raise ArgumentError, 'No name given'
  domain = options[:domain] || Socket.gethostname

  begin
    adsi = WIN32OLE.connect("WinNT://#{domain}/#{group},group")
    options.each{ |option, value| adsi.put(option.to_s, value) }
    adsi.setinfo
  rescue WIN32OLERuntimeError => err
    raise Error, err
  end
end
configure_user(options = {}) click to toggle source

Configures the user using options. If no domain option is specified then your local host is used, i.e. you are configuring a local user account.

See tinyurl.com/3hjv9 for a list of valid options.

In the case of a password change, pass a two element array as the old value and new value.

Examples:

# Configure a local user
Sys::Admin.configure_user(
   :name        => 'djberge',
   :description => 'Awesome'
)

# Change the password
Sys::Admin.configure_user(
   :name     => 'asmith',
   :password => [old_pass, new_pass]
)

# Configure a user on a specific domain
Sys::Admin.configure_user(
   :name      => 'jsmrz',
   :domain    => 'XX',
   :firstname => 'Jo'
)
# File lib/windows/sys/admin.rb, line 478
def self.configure_user(options = {})
  options = munge_options(options)

  name   = options.delete(:name) or raise ArgumentError, 'No name given'
  domain = options[:domain] || Socket.gethostname

  begin
    adsi = WIN32OLE.connect("WinNT://#{domain}/#{name},user")

    options.each{ |option, value|
      if option.to_s == 'password'
        adsi.changepassword(value[0], value[1])
      else
        adsi.put(option.to_s, value)
      end
    }

    adsi.setinfo
  rescue WIN32OLERuntimeError => err
    raise Error, err
  end
end
delete_group(group, domain = nil) click to toggle source

Delete the group from domain. If no domain is specified, then you are deleting a local group.

# File lib/windows/sys/admin.rb, line 598
def self.delete_group(group, domain = nil)
  if domain.nil?
    domain = Socket.gethostname
    moniker = "WinNT://#{domain},Computer"
  else
    moniker = "WinNT://#{domain}"
  end

  begin
    adsi = WIN32OLE.connect(moniker)
    adsi.delete('group', group)
  rescue WIN32OLERuntimeError => err
    raise Error, err
  end
end
delete_user(user, domain = nil) click to toggle source

Deletes the given user on domain. If no domain is specified, then it defaults to your local host, i.e. a local account is deleted.

# File lib/windows/sys/admin.rb, line 505
def self.delete_user(user, domain = nil)
  if domain.nil?
    domain  = Socket.gethostname
    moniker = "WinNT://#{domain},Computer"
  else
    moniker = "WinNT://#{domain}"
  end

  begin
    adsi = WIN32OLE.connect(moniker)
    adsi.delete('user', user)
  rescue WIN32OLERuntimeError => err
    raise Error, err
  end
end
get_group(gid) click to toggle source

Returns a Group object for the given name or uid. Raises an error if a group cannot be found.

Examples:

Sys::Admin.get_group('admin')
Sys::Admin.get_group(101)
# File lib/unix/sys/admin.rb, line 80
def self.get_group(gid)
  if gid.is_a?(String)
    grp = GroupStruct.new(getgrnam(gid))
  else
    grp = GroupStruct.new(getgrgid(gid))
  end

  if grp.null?
    raise Error, "no group found for: #{gid}"
  end

  Group.new do |g|
    g.name    = grp[:gr_name]
    g.passwd  = grp[:gr_passwd]
    g.gid     = grp[:gr_gid]
    g.members = grp[:gr_mem].read_array_of_string
  end
end
get_login() click to toggle source

Returns the login for the current process.

# File lib/unix/sys/admin.rb, line 36
def self.get_login
  getlogin()
end
get_user(uid) click to toggle source

Returns a User object for the given name or uid. Raises an error if a user cannot be found.

Examples:

Sys::Admin.get_user('joe')
Sys::Admin.get_user(501)
# File lib/unix/sys/admin.rb, line 48
def self.get_user(uid)
  if uid.is_a?(String)
    pwd = PasswdStruct.new(getpwnam(uid))
  else
    pwd = PasswdStruct.new(getpwuid(uid))
  end

  if pwd.null?
    raise Error, "no user found for: #{uid}"
  end

  user = User.new do |u|
    u.name   = pwd[:pw_name]
    u.passwd = pwd[:pw_passwd]
    u.uid    = pwd[:pw_uid]
    u.gid    = pwd[:pw_gid]
    u.gecos  = pwd[:pw_gecos]
    u.dir    = pwd[:pw_dir]
    u.shell  = pwd[:pw_shell]
  end

  user
end
groups() click to toggle source

Returns an array of Group objects for each user on the system.

# File lib/unix/sys/admin.rb, line 120
def self.groups
  groups = []

  begin
    setgrent()

    until (ptr = getgrent()).null?
      grp = GroupStruct.new(ptr)
      groups << get_group_from_struct(grp)
    end
  ensure
    endgrent()
  end

  groups
end
users() click to toggle source

Returns an array of User objects for each user on the system.

# File lib/unix/sys/admin.rb, line 101
def self.users
  users = []

  begin
    setpwent()

    until (ptr = getpwent()).null?
      pwd = PasswdStruct.new(ptr)
      users << get_user_from_struct(pwd)
    end
  ensure
    endpwent()
  end

  users
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.