Class: Wright::Provider::User::DarwinDirectoryService

Inherits:
Wright::Provider::User show all
Defined in:
lib/wright/provider/user/darwin_directory_service.rb

Overview

Darwin DirectoryService user provider. Used as a provider for Resource::User on OS X systems.

Constant Summary

Constants inherited from Wright::Provider

PROVIDER_DIR

Instance Method Summary collapse

Methods inherited from Wright::Provider::User

#attributes_uptodate?, #create, #full_name, #full_name_uptodate?, #groups, #groups_uptodate?, #home, #home_uptodate?, #primary_group, #primary_group_uptodate?, #remove, #shell, #shell_uptodate?, #system_user?, #uid, #uid_uptodate?, #uptodate?, #user_data, #user_exists?, #user_name

Methods inherited from Wright::Provider

#env, #exec_or_fail, #initialize, #unless_dry_run, #unless_uptodate, #updated?

Constructor Details

This class inherits a constructor from Wright::Provider

Instance Method Details

#create_userObject (private)


12
13
14
15
16
17
18
# File 'lib/wright/provider/user/darwin_directory_service.rb', line 12

def create_user
  attributes = default_attributes.merge(resource_attributes)
  attributes.each do |k, v|
    args = dscl_args(:create, k, v)
    exec_or_fail('dscl', args, "cannot create user '#{user_name}'")
  end
end

#default_attributesObject (private)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wright/provider/user/darwin_directory_service.rb', line 37

def default_attributes
  uid_range = system_user? ? 1...500 : 500...1000
  {
    'UniqueID' => Wright::Util::User.next_free_uid(uid_range),
    'UserShell' => '/bin/bash',
    'RealName' => '',
    'NFSHomeDirectory' => "/Users/#{user_name}",
    'PrimaryGroupID' => Wright::Util::User.group_to_gid('staff'),
    'Password' => '*'
  }
end

#dscl_args(cmd, key, value) ⇒ Object (private)


33
34
35
# File 'lib/wright/provider/user/darwin_directory_service.rb', line 33

def dscl_args(cmd, key, value)
  %W(. -#{cmd} /Users/#{user_name} #{key} #{value})
end

#remove_userObject (private)


27
28
29
30
31
# File 'lib/wright/provider/user/darwin_directory_service.rb', line 27

def remove_user
  exec_or_fail('dscl',
               %W(. -delete /Users/#{user_name}),
               "cannot remove user '#{user_name}'")
end

#resource_attributesObject (private)


49
50
51
52
53
54
55
56
57
58
# File 'lib/wright/provider/user/darwin_directory_service.rb', line 49

def resource_attributes
  gid = Wright::Util::User.group_to_gid(primary_group)
  {
    'UniqueID' => uid,
    'UserShell' => shell,
    'RealName' => full_name,
    'NFSHomeDirectory' => home,
    'PrimaryGroupID' => gid
  }.reject { |_k, v| v.nil? }
end

#update_userObject (private)


20
21
22
23
24
25
# File 'lib/wright/provider/user/darwin_directory_service.rb', line 20

def update_user
  resource_attributes.each do |k, v|
    args = dscl_args(:create, k, v)
    exec_or_fail('dscl', args, "cannot create user '#{user_name}'")
  end
end