Class: CFA::LoginDefs

Inherits:
BaseModel
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/general/src/lib/cfa/login_defs.rb

Overview

Model to handle login.defs configuration files

Examples:

Reading a value

file = LoginDefs.new(file_path: "/etc/login.defs")
file.load
file.encrypt_method #=> "SHA512"

Writing a value

file = LoginDefs.new(file_path: "/etc/login.defs.d/70-yast.defs")
file.encrypt_method = "DES"
file.save

Loading shortcut

file = LoginDefs.load(file_path: "/etc/login.defs.d/70-yast.defs")
file.encrypt_method #=> "SHA512"

Constant Summary collapse

KNOWN_ATTRIBUTES =

Returns List of known login.defs attributes.

Returns:

  • (Array<Symbol>)

    List of known login.defs attributes

[
  :character_class,
  :encrypt_method,
  :fail_delay,
  :gid_max,
  :gid_min,
  :groupadd_cmd,
  :pass_max_days,
  :pass_min_days,
  :pass_warn_age,
  :sys_gid_max,
  :sys_gid_min,
  :sys_uid_max,
  :sys_uid_min,
  :uid_max,
  :uid_min,
  :useradd_cmd,
  :userdel_postcmd,
  :userdel_precmd
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path:, file_handler: Yast::TargetFile) ⇒ LoginDefs

Constructor

Parameters:

  • file_handler (#read, #write) (defaults to: Yast::TargetFile)

    something able to read/write a string (like File)

  • file_path (String)

    File path

See Also:

  • BaseModel#initialize

101
102
103
# File 'library/general/src/lib/cfa/login_defs.rb', line 101

def initialize(file_path:, file_handler: Yast::TargetFile)
  super(AugeasParser.new("login_defs.lns"), file_path, file_handler: file_handler)
end

Instance Attribute Details

#file_pathString (readonly)

Returns File path.

Returns:

  • (String)

    File path


93
94
95
# File 'library/general/src/lib/cfa/login_defs.rb', line 93

def file_path
  @file_path
end

Class Method Details

.known_attributesArray<Symbol>

Returns the list of known attributes

Returns:

  • (Array<Symbol>)

72
73
74
# File 'library/general/src/lib/cfa/login_defs.rb', line 72

def known_attributes
  KNOWN_ATTRIBUTES
end

.load(file_path:, file_handler: Yast::TargetFile) ⇒ LoginDefs

Instantiates and loads a file

This method is basically a shortcut to instantiate and load the content in just one call.

Parameters:

  • file_handler (#read, #write) (defaults to: Yast::TargetFile)

    something able to read/write a string (like File)

  • file_path (String)

    File path

Returns:

  • (LoginDefs)

    File with the already loaded content


83
84
85
# File 'library/general/src/lib/cfa/login_defs.rb', line 83

def load(file_path:, file_handler: Yast::TargetFile)
  new(file_path: file_path, file_handler: file_handler).tap(&:load)
end

Instance Method Details

#conflicts(other) ⇒ Array<Symbol>

Determines the list of conflicting attributes for two files

Two attributes are conflicting when both of them are defined with different values.

Parameters:

  • other (BaseModel)

    The file to compare with

Returns:

  • (Array<Symbol>)

    List of conflicting attributes


127
128
129
130
# File 'library/general/src/lib/cfa/login_defs.rb', line 127

def conflicts(other)
  conflicting_attrs = present_attributes & other.present_attributes
  conflicting_attrs.reject { |a| public_send(a) == other.public_send(a) }
end

#loadObject

Loads the file content

If the file does not exist, consider the file as empty.

See Also:

  • BaseModel#load

137
138
139
140
141
142
# File 'library/general/src/lib/cfa/login_defs.rb', line 137

def load
  super
rescue Errno::ENOENT # PATH does not exist yet
  self.data = @parser.empty
  @loaded = true
end

#present?(attr) ⇒ Boolean

Determines whether an attribute has a value

Returns:

  • (Boolean)

    +true+ if it is defined; +false+ otherwise.


108
109
110
# File 'library/general/src/lib/cfa/login_defs.rb', line 108

def present?(attr)
  !public_send(attr).nil?
end

#present_attributesArray<Symbol>

Returns the list of attributes with a value

Returns:

  • (Array<Symbol>)

    List of attribute names

See Also:


116
117
118
# File 'library/general/src/lib/cfa/login_defs.rb', line 116

def present_attributes
  self.class.known_attributes.select { |a| present?(a) }
end

#saveObject

Saves the file content

It creates the directory if it does not exist.

See Also:

  • BaseModel#save

149
150
151
152
153
# File 'library/general/src/lib/cfa/login_defs.rb', line 149

def save
  directory = File.dirname(file_path)
  Yast::Execute.on_target("/usr/bin/mkdir", "--parents", directory) if !Yast::FileUtils.IsDirectory(directory)
  super
end