Class: Wright::Provider::User

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

Overview

User provider. Used as a base class for Resource::User providers.

Direct Known Subclasses

DarwinDirectoryService, GnuPasswd

Defined Under Namespace

Classes: DarwinDirectoryService, GnuPasswd

Constant Summary

Constants inherited from Wright::Provider

PROVIDER_DIR

Instance Method Summary collapse

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

#attributes_uptodate?Boolean (private)

Returns:

  • (Boolean)

90
91
92
93
94
95
96
97
# File 'lib/wright/provider/user.rb', line 90

def attributes_uptodate?
  uid_uptodate? &&
    full_name_uptodate? &&
    groups_uptodate? &&
    shell_uptodate? &&
    home_uptodate? &&
    primary_group_uptodate?
end

#createvoid

This method returns an undefined value.

Creates or updates the user.


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wright/provider/user.rb', line 13

def create
  unless_uptodate(:create, "user already created: '#{user_name}'") do
    unless_dry_run("create user: '#{user_name}'") do
      if user_exists?
        update_user
      else
        create_user
      end
    end
  end
end

#create_userObject (private)


135
136
137
# File 'lib/wright/provider/user.rb', line 135

def create_user
  fail NotImplementedError
end

#full_nameObject (private)


50
51
52
# File 'lib/wright/provider/user.rb', line 50

def full_name
  @resource.full_name
end

#full_name_uptodate?Boolean (private)

Returns:

  • (Boolean)

107
108
109
# File 'lib/wright/provider/user.rb', line 107

def full_name_uptodate?
  full_name.nil? || user_data.gecos.split(',').first == full_name
end

#groupsObject (private)


54
55
56
# File 'lib/wright/provider/user.rb', line 54

def groups
  @resource.groups
end

#groups_uptodate?Boolean (private)

Returns:

  • (Boolean)

111
112
113
114
115
116
# File 'lib/wright/provider/user.rb', line 111

def groups_uptodate?
  return true if groups.nil?
  target_groups = []
  Etc.group { |g| target_groups << g.name if g.mem.include?(user_name) }
  target_groups.sort.uniq == groups.sort.uniq
end

#homeObject (private)


62
63
64
# File 'lib/wright/provider/user.rb', line 62

def home
  @resource.home
end

#home_uptodate?Boolean (private)

Returns:

  • (Boolean)

122
123
124
# File 'lib/wright/provider/user.rb', line 122

def home_uptodate?
  home.nil? || user_data.dir == home
end

#primary_groupObject (private)


46
47
48
# File 'lib/wright/provider/user.rb', line 46

def primary_group
  @resource.primary_group
end

#primary_group_uptodate?Boolean (private)

Returns:

  • (Boolean)

126
127
128
129
# File 'lib/wright/provider/user.rb', line 126

def primary_group_uptodate?
  return true if primary_group.nil?
  user_data.gid == Wright::Util::User.group_to_gid(primary_group)
end

#removevoid

This method returns an undefined value.

Removes the user.


28
29
30
31
32
33
34
# File 'lib/wright/provider/user.rb', line 28

def remove
  unless_uptodate(:remove, "user already removed: '#{user_name}'") do
    unless_dry_run("remove user: '#{user_name}'") do
      remove_user
    end
  end
end

#remove_userObject (private)


143
144
145
# File 'lib/wright/provider/user.rb', line 143

def remove_user
  fail NotImplementedError
end

#shellObject (private)


58
59
60
# File 'lib/wright/provider/user.rb', line 58

def shell
  @resource.shell
end

#shell_uptodate?Boolean (private)

Returns:

  • (Boolean)

118
119
120
# File 'lib/wright/provider/user.rb', line 118

def shell_uptodate?
  shell.nil? || user_data.shell == shell
end

#system_user?Boolean (private)

Returns:

  • (Boolean)

66
67
68
# File 'lib/wright/provider/user.rb', line 66

def system_user?
  @resource.system
end

#uidObject (private)


42
43
44
# File 'lib/wright/provider/user.rb', line 42

def uid
  @resource.uid
end

#uid_uptodate?Boolean (private)

Returns:

  • (Boolean)

103
104
105
# File 'lib/wright/provider/user.rb', line 103

def uid_uptodate?
  uid.nil? || user_data.uid == uid
end

#update_userObject (private)


139
140
141
# File 'lib/wright/provider/user.rb', line 139

def update_user
  fail NotImplementedError
end

#uptodate?(action) ⇒ Bool (private)

Checks if the user is up-to-date for a given action.

Parameters:

  • action (Symbol)

    the action. Currently supports :create and :remove.

Returns:

  • (Bool)

    true if the user is up-to-date and false otherwise

Raises:

  • (ArgumentError)

    if the action is invalid


79
80
81
82
83
84
85
86
87
88
# File 'lib/wright/provider/user.rb', line 79

def uptodate?(action)
  case action
  when :create
    user_exists? && attributes_uptodate?
  when :remove
    !user_exists?
  else
    fail ArgumentError, "invalid action '#{action}'"
  end
end

#user_dataObject (private)


99
100
101
# File 'lib/wright/provider/user.rb', line 99

def user_data
  Wright::Util::User.safe_getpwnam(user_name)
end

#user_exists?Boolean (private)

Returns:

  • (Boolean)

131
132
133
# File 'lib/wright/provider/user.rb', line 131

def user_exists?
  !user_data.nil?
end

#user_nameObject (private)


38
39
40
# File 'lib/wright/provider/user.rb', line 38

def user_name
  @resource.name
end