Class: Serverspec::Type::SudoUser

Inherits:
Base
  • Object
show all
Defined in:
lib/serverspec_extra_types/types/sudo_user.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SudoUser

Returns a new instance of SudoUser.



10
11
12
13
# File 'lib/serverspec_extra_types/types/sudo_user.rb', line 10

def initialize(name)
  super
  @user = name
end

Instance Method Details

#allowed_to_run_command?(command, user = nil, checkpw = false) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/serverspec_extra_types/types/sudo_user.rb', line 23

def allowed_to_run_command?(command, user = nil, checkpw = false)
  perm = permission(command)
  if user
    if checkpw
      perm[:user] == user && perm[:nopasswd]
    else
      perm[:user] == user
    end
  else
    checkpw ? perm && perm[:nopasswd] : perm
  end
end

#exist?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/serverspec_extra_types/types/sudo_user.rb', line 15

def exist?
  if get_inspection.success? && /User #{@user} may run the following commands/.match(@get_inspection.stdout)
    true
  else
    false
  end
end

#has_sudo_disabled?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/serverspec_extra_types/types/sudo_user.rb', line 44

def has_sudo_disabled?
  /User #{@user} is not allowed to run sudo/.match(@get_inspection.stdout)
end

#inspectionObject



48
49
50
# File 'lib/serverspec_extra_types/types/sudo_user.rb', line 48

def inspection
  @inspection ||= get_sudo_perms(get_inspection.stdout)
end

#permission(command) ⇒ Object



36
37
38
# File 'lib/serverspec_extra_types/types/sudo_user.rb', line 36

def permission(command)
  permissions.find { |x| x[:command] == command }
end

#permissionsObject



40
41
42
# File 'lib/serverspec_extra_types/types/sudo_user.rb', line 40

def permissions
  inspection[:permissions]
end