Class: MacAdmin::User

Inherits:
DSLocalRecord show all
Defined in:
lib/macadmin/dslocal/user.rb

Overview

User

  • creates and manages Mac OS X User accounts

  • params: :name, :realname, :password, :uid, :gid, :shell, :home, :comment

Constant Summary collapse

MIN_UID =
501

Constants inherited from DSLocalRecord

DSLocalRecord::DSLOCAL_ROOT

Constants included from Common

Common::MAC_OS_X_PRODUCT_VERSION

Instance Attribute Summary

Attributes inherited from DSLocalRecord

#composite, #data, #file, #node, #real, #record

Instance Method Summary collapse

Methods inherited from DSLocalRecord

#[], #[]=, #diff, #eql?, init_with_file

Methods included from MCX

#has_mcx?, #mcx_delete, #mcx_export, #mcx_import, #pretty_mcx

Methods included from Common

#get_primary_mac_address, #load_plist, #restart_directoryservice

Constructor Details

#initialize(args) ⇒ User

Override parent initialization

  • capture the password if it exists

  • if there’s no password object param, try to get one

  • shoehorn the password into the User record



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/macadmin/dslocal/user.rb', line 14

def initialize(args)
  if args.respond_to?(:keys)
    @password = args.delete(:password)
  end
  super(args)
  if @password
    self.send(:password=)
  else
    @password = ShadowHash.create_from_user_record(self)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MacAdmin::DSLocalRecord

Instance Method Details

#create(file = @file) ⇒ Object

Create the record

  • overrides parent method; required for Legacy users

  • creates the password if SHA1 and kicks up to parent



66
67
68
69
70
71
# File 'lib/macadmin/dslocal/user.rb', line 66

def create(file=@file)
  if self.legacy?
    return unless @password.send(:to_file, self)        
  end
  super
end

#destroy(file = @file) ⇒ Object

Delete the record

  • overrides parent method; required for Legacy users

  • destroys the password if SHA1 and kicks up to parent



76
77
78
79
80
81
# File 'lib/macadmin/dslocal/user.rb', line 76

def destroy(file=@file)
  if self.legacy?
    return unless @password.send(:rm_file, self)        
  end
  super
end

#exists?Boolean

Does the specified resource already exist?

  • overrides parent method; required for Legacy users

  • checks the password if SHA1 and kicks up to parent

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/macadmin/dslocal/user.rb', line 54

def exists?
  if self.legacy?
    password_on_disk = SaltedSHA1.create_from_shadowhash_file self.generateduid[0]
    return false unless password_on_disk
    return false unless password_on_disk.password.eql? @password.password
  end
  super
end

#legacy?Boolean

Legacy user records are determined by SHA1 password type

  • returns boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/macadmin/dslocal/user.rb', line 47

def legacy?
  @password.is_a? SaltedSHA1
end

#passwordObject

Generic getter

  • Returns Ruby Hash representation of the User’s password



40
41
42
43
# File 'lib/macadmin/dslocal/user.rb', line 40

def password
  return nil unless @password
  @password.password
end

#password=(password = @password) ⇒ Object

Generic setter

  • Accepts a ShadowHash object

  • delegates the storage operation to the ShadowHash object itself



29
30
31
32
33
34
35
36
# File 'lib/macadmin/dslocal/user.rb', line 29

def password=(password = @password)
  error = 'Argument was not a ShadowHash object'
  unless password.nil? or password.respond_to? :password
    raise ArgumentError.new(error)
  end
  @password = password
  @password.send(:store, self) unless @password.nil?
end

#to_puppetObject

Return a Puppet style resource manifest

  • need to find a sensible way of doing this

  • need to move this method into the Parent class



86
87
88
# File 'lib/macadmin/dslocal/user.rb', line 86

def to_puppet
  puts "not implemented"
end